youtube_explode/lib/src/reverse_engineering/responses/watch_page.dart

232 lines
6.5 KiB
Dart
Raw Normal View History

2021-03-11 14:20:10 +01:00
import 'dart:convert';
import 'package:collection/collection.dart';
2020-05-31 23:36:23 +02:00
import 'package:html/dom.dart';
2020-06-03 13:18:37 +02:00
import 'package:html/parser.dart' as parser;
import '../../../youtube_explode_dart.dart';
import '../../extensions/helpers_extension.dart';
import '../../retry.dart';
2020-06-03 23:02:21 +02:00
import '../../videos/video_id.dart';
import '../youtube_http_client.dart';
import 'player_config_base.dart';
2020-06-03 13:18:37 +02:00
import 'player_response.dart';
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-06-03 13:18:37 +02:00
class WatchPage {
static final RegExp _videoLikeExp =
RegExp(r'"label"\s*:\s*"([\d,\.]+) likes"');
static final RegExp _videoDislikeExp =
RegExp(r'"label"\s*:\s*"([\d,\.]+) dislikes"');
static final RegExp _visitorInfoLiveExp =
RegExp('VISITOR_INFO1_LIVE=([^;]+)');
static final RegExp _yscExp = RegExp('YSC=([^;]+)');
static final RegExp _playerResponseExp =
RegExp(r'var\s+ytInitialPlayerResponse\s*=\s*(\{.*\})');
static final _xsfrTokenExp = RegExp(r'"XSRF_TOKEN"\s*:\s*"(.+?)"');
2020-05-31 23:36:23 +02:00
2021-03-11 14:20:10 +01:00
final Document root;
2020-07-16 20:02:54 +02:00
///
final String visitorInfoLive;
2020-07-16 20:02:54 +02:00
///
final String ysc;
2021-03-11 14:20:10 +01:00
_InitialData? _initialData;
2020-10-27 14:44:11 +01:00
///
2021-03-11 14:20:10 +01:00
String? get sourceUrl {
var url = root
.querySelectorAll('script')
.map((e) => e.attributes['src'])
2021-03-11 14:20:10 +01:00
.whereNotNull()
.firstWhereOrNull((e) => e.contains('player_ias') && e.endsWith('.js'));
2020-10-27 14:44:11 +01:00
if (url == null) {
return null;
}
return 'https://youtube.com$url';
}
2021-03-11 14:20:10 +01:00
late final _InitialData initialData = getInitialData();
2020-07-16 20:02:54 +02:00
///
2021-03-11 14:20:10 +01:00
_InitialData getInitialData() {
2020-12-25 23:29:01 +01:00
if (_initialData != null) {
2021-03-11 14:20:10 +01:00
return _initialData!;
2020-12-25 23:29:01 +01:00
}
2021-03-11 14:20:10 +01:00
final scriptText = root
2020-12-25 23:29:01 +01:00
.querySelectorAll('script')
.map((e) => e.text)
.toList(growable: false);
2021-03-11 14:20:10 +01:00
var initialDataText = scriptText
.firstWhereOrNull((e) => e.contains('window["ytInitialData"] ='));
2020-12-25 23:29:01 +01:00
if (initialDataText != null) {
2021-03-11 14:20:10 +01:00
return _initialData = _InitialData(json
.decode(_extractJson(initialDataText, 'window["ytInitialData"] =')));
2020-12-25 23:29:01 +01:00
}
2021-03-11 14:20:10 +01:00
initialDataText =
scriptText.firstWhereOrNull((e) => e.contains('var ytInitialData = '));
2020-12-25 23:29:01 +01:00
if (initialDataText != null) {
2021-03-11 14:20:10 +01:00
return _initialData = _InitialData(
json.decode(_extractJson(initialDataText, 'var ytInitialData = ')));
2020-12-25 23:29:01 +01:00
}
throw TransientFailureException(
'Failed to retrieve initial data from the watch page, please report this to the project GitHub page.'); // ignore: lines_longer_than_80_chars
}
2021-03-11 14:20:10 +01:00
late final String xsfrToken = getXsfrToken()!;
2020-07-16 20:02:54 +02:00
///
2021-03-11 14:20:10 +01:00
String? getXsfrToken() {
return _xsfrTokenExp
.firstMatch(root
.querySelectorAll('script')
.firstWhere((e) => _xsfrTokenExp.hasMatch(e.text))
.text)
?.group(1);
}
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2021-03-11 14:20:10 +01:00
bool get isOk => root.body?.querySelector('#player') != null;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
2020-05-31 23:36:23 +02:00
bool get isVideoAvailable =>
2021-03-11 14:20:10 +01:00
root.querySelector('meta[property="og:url"]') != null;
2020-05-31 23:36:23 +02:00
2020-07-16 20:02:54 +02:00
///
int get videoLikeCount => int.parse(_videoLikeExp
2021-03-11 14:20:10 +01:00
.firstMatch(root.outerHtml)
?.group(1)
?.stripNonDigits()
2021-03-11 14:20:10 +01:00
.nullIfWhitespace ??
root
2020-06-05 16:17:08 +02:00
.querySelector('.like-button-renderer-like-button')
?.text
2021-03-11 14:20:10 +01:00
.stripNonDigits()
.nullIfWhitespace ??
2020-06-05 16:17:08 +02:00
'0');
2020-07-16 20:02:54 +02:00
///
int get videoDislikeCount => int.parse(_videoDislikeExp
2021-03-11 14:20:10 +01:00
.firstMatch(root.outerHtml)
?.group(1)
?.stripNonDigits()
2021-03-11 14:20:10 +01:00
.nullIfWhitespace ??
root
2020-06-05 16:17:08 +02:00
.querySelector('.like-button-renderer-dislike-button')
?.text
2021-03-11 14:20:10 +01:00
.stripNonDigits()
.nullIfWhitespace ??
2020-06-05 16:17:08 +02:00
'0');
2020-10-27 14:44:11 +01:00
static final _playerConfigExp = RegExp(r'ytplayer\.config\s*=\s*(\{.*\})');
2021-03-11 14:20:10 +01:00
late final WatchPlayerConfig playerConfig = WatchPlayerConfig(json.decode(
_playerConfigExp
.firstMatch(root.getElementsByTagName('html').first.text)
?.group(1)
?.extractJson() ??
'a'));
late final PlayerResponse? playerResponse = getPlayerResponse();
2020-06-05 16:17:08 +02:00
2020-12-25 23:29:01 +01:00
///
2021-03-11 14:20:10 +01:00
PlayerResponse? getPlayerResponse() {
final val = root
.querySelectorAll('script')
.map((e) => e.text)
.map((e) => _playerResponseExp.firstMatch(e)?.group(1))
.firstWhereOrNull((e) => !e.isNullOrWhiteSpace)
?.extractJson();
if (val == null) {
return null;
}
return PlayerResponse.parse(val);
}
2020-05-31 23:36:23 +02:00
String _extractJson(String html, String separator) =>
html.substring(html.indexOf(separator) + separator.length).extractJson();
2020-06-03 13:18:37 +02:00
2020-07-16 20:02:54 +02:00
///
2021-03-11 14:20:10 +01:00
WatchPage(this.root, this.visitorInfoLive, this.ysc);
2020-06-22 17:40:57 +02:00
2020-07-16 20:02:54 +02:00
///
WatchPage.parse(String raw, this.visitorInfoLive, this.ysc)
2021-03-11 14:20:10 +01:00
: root = parser.parse(raw);
2020-06-03 13:18:37 +02:00
2020-07-16 20:02:54 +02:00
///
2020-06-03 13:18:37 +02:00
static Future<WatchPage> get(YoutubeHttpClient httpClient, String videoId) {
final url = 'https://youtube.com/watch?v=$videoId&bpctr=9999999999&hl=en';
return retry(() async {
var req = await httpClient.get(url, validate: true);
2020-06-03 13:18:37 +02:00
2021-03-11 14:20:10 +01:00
var cookies = req.headers['set-cookie']!;
var visitorInfoLive = _visitorInfoLiveExp.firstMatch(cookies)!.group(1)!;
var ysc = _yscExp.firstMatch(cookies)!.group(1)!;
var result = WatchPage.parse(req.body, visitorInfoLive, ysc);
2020-06-03 13:18:37 +02:00
if (!result.isOk) {
2020-07-16 20:02:54 +02:00
throw TransientFailureException('Video watch page is broken.');
2020-06-03 13:18:37 +02:00
}
if (!result.isVideoAvailable) {
throw VideoUnavailableException.unavailable(VideoId(videoId));
}
return result;
});
}
2020-05-31 23:36:23 +02:00
}
/// Used internally
2021-03-11 14:20:10 +01:00
class WatchPlayerConfig implements PlayerConfigBase<Map<String, dynamic>> {
@override
2021-03-11 14:20:10 +01:00
final Map<String, dynamic> root;
2020-05-31 23:36:23 +02:00
///
WatchPlayerConfig(this.root);
2020-05-31 23:36:23 +02:00
@override
2021-03-11 14:20:10 +01:00
late final String sourceUrl =
'https://youtube.com${root.get('assets')!.getT<String>('js')}';
2020-06-03 13:18:37 +02:00
///
2021-03-11 14:20:10 +01:00
late final PlayerResponse playerResponse =
PlayerResponse.parse(root.get('args')!.getT<String>('playerResponse')!);
2020-06-03 13:18:37 +02:00
}
2020-06-17 22:14:27 +02:00
class _InitialData {
// Json parsed map
2021-03-11 14:20:10 +01:00
final Map<String, dynamic> root;
2020-06-17 22:14:27 +02:00
2020-07-14 14:16:52 +02:00
_InitialData(this.root);
2020-06-17 22:14:27 +02:00
2021-03-11 14:20:10 +01:00
Map<String, dynamic>? getContinuationContext() {
if (root['contents'] != null) {
return root
.get('contents')
?.get('twoColumnWatchNextResults')
?.get('results')
?.get('results')
?.getList('contents')
?.firstWhere((e) => e['itemSectionRenderer'] != null)
.get('itemSectionRenderer')
?.getList('continuations')
?.firstOrNull
?.get('nextContinuationData');
2020-06-17 22:14:27 +02:00
}
return null;
}
2021-03-11 14:20:10 +01:00
late final String continuation =
getContinuationContext()?.getT<String>('continuation') ?? '';
2020-06-17 22:14:27 +02:00
2021-03-11 14:20:10 +01:00
late final String clickTrackingParams =
getContinuationContext()?.getT<String>('clickTrackingParams') ?? '';
2020-06-17 22:14:27 +02:00
}