2021-03-11 14:20:10 +01:00
|
|
|
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';
|
2021-02-26 16:08:48 +01:00
|
|
|
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 {
|
2021-03-18 22:22:34 +01:00
|
|
|
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=([^;]+)');
|
2020-06-21 16:23:19 +02:00
|
|
|
static final RegExp _yscExp = RegExp('YSC=([^;]+)');
|
2021-03-18 22:22:34 +01:00
|
|
|
static final RegExp _playerResponseExp = RegExp(r'var\s+ytInitialPlayerResponse\s*=\s*(\{.*\})');
|
2020-12-02 12:05:45 +01:00
|
|
|
|
2020-06-21 16:23:19 +02:00
|
|
|
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
|
|
|
|
|
|
|
///
|
2020-06-21 16:23:19 +02:00
|
|
|
final String visitorInfoLive;
|
2020-07-16 20:02:54 +02:00
|
|
|
|
|
|
|
///
|
2020-06-21 16:23:19 +02:00
|
|
|
final String ysc;
|
|
|
|
|
2021-03-11 14:20:10 +01:00
|
|
|
_InitialData? _initialData;
|
2020-06-21 16:23:19 +02:00
|
|
|
|
2020-10-27 14:44:11 +01:00
|
|
|
///
|
2021-03-11 14:20:10 +01:00
|
|
|
String? get sourceUrl {
|
|
|
|
var url = root
|
2020-12-02 12:05:45 +01:00
|
|
|
.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-18 22:22:34 +01:00
|
|
|
final scriptText = root.querySelectorAll('script').map((e) => e.text).toList(growable: false);
|
|
|
|
return scriptText.extractGenericData(
|
|
|
|
(obj) => _InitialData(obj),
|
|
|
|
() => TransientFailureException(
|
|
|
|
'Failed to retrieve initial data from the watch page, please report this to the project GitHub page.'));
|
2020-12-25 23:29:01 +01:00
|
|
|
}
|
2020-06-21 16:23:19 +02:00
|
|
|
|
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() {
|
2021-03-18 22:22:34 +01:00
|
|
|
return _xsfrTokenExp.firstMatch(root.querySelectorAll('script').firstWhere((e) => _xsfrTokenExp.hasMatch(e.text)).text)?.group(1);
|
2021-03-11 14:20:10 +01:00
|
|
|
}
|
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
|
|
|
///
|
2021-03-18 22:22:34 +01:00
|
|
|
bool get isVideoAvailable => root.querySelector('meta[property="og:url"]') != null;
|
2020-05-31 23:36:23 +02:00
|
|
|
|
2020-07-16 20:02:54 +02:00
|
|
|
///
|
2021-03-18 22:22:34 +01:00
|
|
|
int get videoLikeCount => int.parse(_videoLikeExp.firstMatch(root.outerHtml)?.group(1)?.stripNonDigits().nullIfWhitespace ??
|
|
|
|
root.querySelector('.like-button-renderer-like-button')?.text.stripNonDigits().nullIfWhitespace ??
|
2020-06-05 16:17:08 +02:00
|
|
|
'0');
|
|
|
|
|
2020-07-16 20:02:54 +02:00
|
|
|
///
|
2021-03-18 22:22:34 +01:00
|
|
|
int get videoDislikeCount => int.parse(_videoDislikeExp.firstMatch(root.outerHtml)?.group(1)?.stripNonDigits().nullIfWhitespace ??
|
|
|
|
root.querySelector('.like-button-renderer-dislike-button')?.text.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*(\{.*\})');
|
2020-10-01 18:04:56 +02:00
|
|
|
|
2021-03-18 22:22:34 +01:00
|
|
|
late final WatchPlayerConfig? playerConfig = getPlayerConfig();
|
2021-03-11 14:20:10 +01:00
|
|
|
|
|
|
|
late final PlayerResponse? playerResponse = getPlayerResponse();
|
2020-06-05 16:17:08 +02:00
|
|
|
|
2021-03-18 22:22:34 +01:00
|
|
|
///
|
|
|
|
WatchPlayerConfig? getPlayerConfig() {
|
|
|
|
final jsonMap = _playerConfigExp.firstMatch(root.getElementsByTagName('html').first.text)?.group(1)?.extractJson();
|
|
|
|
if (jsonMap == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return WatchPlayerConfig(jsonMap);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2021-03-18 22:22:34 +01:00
|
|
|
return PlayerResponse(val);
|
2021-03-11 14:20:10 +01:00
|
|
|
}
|
2020-05-31 23:36:23 +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
|
|
|
///
|
2021-03-18 22:22:34 +01:00
|
|
|
WatchPage.parse(String raw, this.visitorInfoLive, this.ysc) : 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 {
|
2020-06-21 16:23:19 +02:00
|
|
|
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']!;
|
2021-03-18 22:22:34 +01:00
|
|
|
var visitorInfoLive = _visitorInfoLiveExp.firstMatch(cookies)?.group(1)!;
|
2021-03-11 14:20:10 +01:00
|
|
|
var ysc = _yscExp.firstMatch(cookies)!.group(1)!;
|
2021-03-18 22:22:34 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-28 10:32:09 +01:00
|
|
|
/// Used internally
|
2021-03-11 14:20:10 +01:00
|
|
|
class WatchPlayerConfig implements PlayerConfigBase<Map<String, dynamic>> {
|
2021-01-28 10:32:09 +01:00
|
|
|
@override
|
2021-03-11 14:20:10 +01:00
|
|
|
final Map<String, dynamic> root;
|
2020-05-31 23:36:23 +02:00
|
|
|
|
2021-01-28 10:32:09 +01:00
|
|
|
///
|
|
|
|
WatchPlayerConfig(this.root);
|
2020-05-31 23:36:23 +02:00
|
|
|
|
2021-01-28 10:32:09 +01:00
|
|
|
@override
|
2021-03-18 22:22:34 +01:00
|
|
|
late final String sourceUrl = 'https://youtube.com${root.get('assets')!.getT<String>('js')}';
|
2020-06-03 13:18:37 +02:00
|
|
|
|
2021-01-28 10:32:09 +01:00
|
|
|
///
|
2021-03-18 22:22:34 +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-18 22:22:34 +01:00
|
|
|
late final String continuation = getContinuationContext()?.getT<String>('continuation') ?? '';
|
2020-06-17 22:14:27 +02:00
|
|
|
|
2021-03-18 22:22:34 +01:00
|
|
|
late final String clickTrackingParams = getContinuationContext()?.getT<String>('clickTrackingParams') ?? '';
|
2020-06-17 22:14:27 +02:00
|
|
|
}
|