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

172 lines
5.1 KiB
Dart
Raw Normal View History

2020-07-10 22:28:19 +02:00
import 'dart:convert';
2021-03-11 14:20:10 +01:00
import 'package:collection/collection.dart';
2020-07-10 22:28:19 +02:00
import 'package:html/dom.dart';
import 'package:html/parser.dart' as parser;
import '../../channels/channel_video.dart';
2020-07-16 20:02:54 +02:00
import '../../exceptions/exceptions.dart';
2021-03-11 14:20:10 +01:00
import '../../extensions/helpers_extension.dart';
2020-07-10 22:28:19 +02:00
import '../../retry.dart';
import '../../videos/videos.dart';
import '../youtube_http_client.dart';
2020-07-16 20:02:54 +02:00
///
2020-07-12 18:24:22 +02:00
class ChannelUploadPage {
2020-07-16 20:02:54 +02:00
///
2020-07-10 22:28:19 +02:00
final String channelId;
2021-03-11 14:20:10 +01:00
final Document? _root;
2020-07-10 22:28:19 +02:00
2021-03-11 14:20:10 +01:00
late final _InitialData initialData = _getInitialData();
_InitialData? _initialData;
2020-07-10 22:28:19 +02:00
2020-07-16 20:02:54 +02:00
///
2021-03-04 12:20:00 +01:00
_InitialData _getInitialData() {
2021-03-11 14:20:10 +01:00
if (_initialData != null) {
return _initialData!;
}
final scriptText = _root!
2020-12-25 23:29:01 +01:00
.querySelectorAll('script')
.map((e) => e.text)
.toList(growable: false);
2021-03-18 22:22:55 +01:00
return scriptText.extractGenericData(
(obj) => _InitialData(obj),
() => TransientFailureException(
'Failed to retrieve initial data from the channel upload page, please report this to the project GitHub page.'));
2020-07-10 22:28:19 +02:00
}
2021-03-18 22:22:55 +01:00
///
ChannelUploadPage(this._root, this.channelId, [_InitialData? initialData])
: _initialData = initialData;
2020-07-10 22:28:19 +02:00
2021-03-18 22:22:55 +01:00
///
Future<ChannelUploadPage?> nextPage(YoutubeHttpClient httpClient) {
2020-07-12 18:24:22 +02:00
if (initialData.continuation.isEmpty) {
2021-03-18 22:22:55 +01:00
return Future.value(null);
2020-07-12 18:24:22 +02:00
}
var url =
2021-03-18 22:22:55 +01:00
'https://www.youtube.com/browse_ajax?ctoken=${initialData.continuation}&continuation=${initialData.continuation}&itct=${initialData.clickTrackingParams}';
2020-07-12 18:24:22 +02:00
return retry(() async {
2021-03-18 22:22:55 +01:00
var raw = await httpClient.getString(url);
return ChannelUploadPage(
null, channelId, _InitialData(json.decode(raw)[1]));
2020-07-12 18:24:22 +02:00
});
2021-03-18 22:22:55 +01:00
}
2020-07-10 22:28:19 +02:00
2021-03-18 22:22:55 +01:00
///
static Future<ChannelUploadPage> get(
YoutubeHttpClient httpClient, String channelId, String sorting) {
2020-07-10 22:28:19 +02:00
var url =
2021-03-18 22:22:55 +01:00
'https://www.youtube.com/channel/$channelId/videos?view=0&sort=$sorting&flow=grid';
2020-07-10 22:28:19 +02:00
return retry(() async {
2021-03-18 22:22:55 +01:00
var raw = await httpClient.getString(url);
return ChannelUploadPage.parse(raw, channelId);
2020-07-10 22:28:19 +02:00
});
}
2020-07-10 22:28:19 +02:00
2021-03-18 22:22:55 +01:00
///
ChannelUploadPage.parse(String raw, this.channelId)
: _root = parser.parse(raw);
}
class _InitialData {
2020-07-10 22:28:19 +02:00
// Json parsed map
2021-03-11 14:20:10 +01:00
final Map<String, dynamic> root;
2020-07-10 22:28:19 +02:00
_InitialData(this.root);
2020-07-10 22:28:19 +02:00
2021-03-11 14:20:10 +01:00
late final Map<String, dynamic>? continuationContext =
2021-03-18 22:22:55 +01:00
getContinuationContext();
2021-03-11 14:20:10 +01:00
late final String clickTrackingParams =
2021-03-18 22:22:55 +01:00
continuationContext?.getT<String>('continuationContext') ?? '';
2021-03-11 14:20:10 +01:00
late final List<ChannelVideo> uploads =
2021-03-18 22:22:55 +01:00
getContentContext().map(_parseContent).whereNotNull().toList();
2021-03-11 14:20:10 +01:00
late final String continuation =
2021-03-18 22:22:55 +01:00
continuationContext?.getT<String>('continuation') ?? '';
2021-03-11 14:20:10 +01:00
List<Map<String, dynamic>> getContentContext() {
2021-03-18 22:22:55 +01:00
List<Map<String, dynamic>>? context;
if (root.containsKey('contents')) {
context = root
.get('contents')
?.get('twoColumnBrowseResultsRenderer')
?.getList('tabs')
?.map((e) => e['tabRenderer'])
.cast<Map<String, dynamic>>()
.firstWhereOrNull((e) => e['selected'] as bool)
?.get('content')
?.get('sectionListRenderer')
?.getList('contents')
?.firstOrNull
?.get('itemSectionRenderer')
?.getList('contents')
?.firstOrNull
?.get('gridRenderer')
?.getList('items')
?.cast<Map<String, dynamic>>();
}
if (context == null && root.containsKey('response')) {
context = root
.get('response')
?.get('continuationContents')
?.get('gridContinuation')
?.getList('items')
?.cast<Map<String, dynamic>>();
}
if (context == null) {
throw FatalFailureException('Failed to get initial data context.');
}
return context;
2020-07-10 22:28:19 +02:00
}
2021-03-11 14:20:10 +01:00
Map<String, dynamic>? getContinuationContext() {
2021-03-18 22:22:55 +01:00
if (root.containsKey('contents')) {
return root
.get('contents')
?.get('twoColumnBrowseResultsRenderer')
?.getList('tabs')
?.map((e) => e['tabRenderer'])
.cast<Map<String, dynamic>>()
.firstWhereOrNull((e) => e['selected'] as bool)
?.get('content')
?.get('sectionListRenderer')
?.getList('contents')
?.firstOrNull
?.get('itemSectionRenderer')
?.getList('contents')
?.firstOrNull
?.get('gridRenderer')
?.getList('continuations')
?.firstOrNull
?.get('nextContinuationData');
}
if (root.containsKey('response')) {
return root
.get('response')
?.get('continuationContents')
?.get('gridContinuation')
?.getList('continuations')
?.firstOrNull
?.get('nextContinuationData');
}
return null;
2020-07-10 22:28:19 +02:00
}
2021-03-11 14:20:10 +01:00
ChannelVideo? _parseContent(Map<String, dynamic>? content) {
2021-03-18 22:22:55 +01:00
if (content == null || !content.containsKey('gridVideoRenderer')) {
return null;
}
2021-03-11 14:20:10 +01:00
2021-03-18 22:22:55 +01:00
var video = content.get('gridVideoRenderer')!;
return ChannelVideo(
VideoId(video.getT<String>('videoId')!),
video.get('title')?.getT<String>('simpleText') ??
video.get('title')?.getList('runs')?.map((e) => e['text']).join() ??
'');
2020-07-10 22:28:19 +02:00
}
2021-03-18 22:22:55 +01:00
}