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

172 lines
5.4 KiB
Dart
Raw Normal View History

2021-03-11 14:20:10 +01:00
import 'package:collection/collection.dart';
2020-07-10 22:28:19 +02:00
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';
2021-07-21 02:06:02 +02:00
import '../models/initial_data.dart';
2021-07-23 12:54:29 +02:00
import '../models/youtube_page.dart';
import '../youtube_http_client.dart';
2020-07-10 22:28:19 +02:00
2020-07-16 20:02:54 +02:00
///
2021-07-21 02:06:02 +02:00
class ChannelUploadPage extends YoutubePage<_InitialData> {
2020-07-16 20:02:54 +02:00
///
2020-07-10 22:28:19 +02:00
final String channelId;
2021-07-21 02:06:02 +02:00
late final List<ChannelVideo> uploads = initialData.uploads;
2020-07-10 22:28:19 +02:00
2021-07-21 02:06:02 +02:00
/// InitialData
ChannelUploadPage.id(this.channelId, _InitialData? initialData)
: super(null, null, initialData);
2020-07-10 22:28:19 +02:00
2021-03-18 22:22:55 +01:00
///
2021-07-21 02:06:02 +02:00
Future<ChannelUploadPage?> nextPage(YoutubeHttpClient httpClient) async {
2021-05-01 00:23:27 +02:00
if (initialData.token.isEmpty) {
2021-07-21 02:06:02 +02:00
return null;
2020-07-12 18:24:22 +02:00
}
2021-05-01 00:23:27 +02:00
2021-07-21 02:06:02 +02:00
final data = await httpClient.sendPost('browse', initialData.token);
return ChannelUploadPage.id(channelId, _InitialData(data));
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';
return retry(httpClient, () 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)
2021-07-21 02:06:02 +02:00
: super(parser.parse(raw), (root) => _InitialData(root));
2021-03-18 22:22:55 +01:00
}
2021-07-21 02:06:02 +02:00
class _InitialData extends InitialData {
_InitialData(JsonMap root) : super(root);
2020-07-10 22:28:19 +02:00
2021-07-21 02:06:02 +02:00
late final JsonMap? continuationContext = getContinuationContext();
2021-03-11 14:20:10 +01:00
2021-05-01 00:23:27 +02:00
late final String token = continuationContext?.getT<String>('token') ?? '';
2021-03-11 14:20:10 +01:00
2021-06-28 11:54:17 +02:00
late final List<ChannelVideo> uploads = _getUploads();
List<ChannelVideo> _getUploads() {
final content = getContentContext();
if (content.isEmpty) {
return const <ChannelVideo>[];
}
return content.map(_parseContent).whereNotNull().toList();
}
2021-03-11 14:20:10 +01:00
2021-07-21 02:06:02 +02:00
List<JsonMap> getContentContext() {
List<JsonMap>? context;
2021-03-18 22:22:55 +01:00
if (root.containsKey('contents')) {
2021-06-28 11:54:17 +02:00
final render = root
2021-03-18 22:22:55 +01:00
.get('contents')
?.get('twoColumnBrowseResultsRenderer')
?.getList('tabs')
?.map((e) => e['tabRenderer'])
2021-07-21 02:06:02 +02:00
.cast<JsonMap>()
2021-03-18 22:22:55 +01:00
.firstWhereOrNull((e) => e['selected'] as bool)
?.get('content')
?.get('sectionListRenderer')
?.getList('contents')
?.firstOrNull
?.get('itemSectionRenderer')
?.getList('contents')
2021-06-28 11:54:17 +02:00
?.firstOrNull;
if (render?.containsKey('gridRenderer') ?? false) {
2021-07-21 02:06:02 +02:00
context =
render?.get('gridRenderer')?.getList('items')?.cast<JsonMap>();
2021-06-28 11:54:17 +02:00
} else if (render?.containsKey('messageRenderer') ?? false) {
// Workaround for no-videos.
context = const [];
}
2021-03-18 22:22:55 +01:00
}
2021-05-01 00:23:27 +02:00
if (context == null && root.containsKey('onResponseReceivedActions')) {
2021-03-18 22:22:55 +01:00
context = root
2021-05-01 00:23:27 +02:00
.getList('onResponseReceivedActions')
?.firstOrNull
?.get('appendContinuationItemsAction')
?.getList('continuationItems')
2021-07-21 02:06:02 +02:00
?.cast<JsonMap>();
2021-03-18 22:22:55 +01:00
}
if (context == null) {
throw FatalFailureException('Failed to get initial data context.');
}
return context;
2020-07-10 22:28:19 +02:00
}
2021-07-21 02:06:02 +02:00
JsonMap? 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'])
2021-07-21 02:06:02 +02:00
.cast<JsonMap>()
2021-03-18 22:22:55 +01:00
.firstWhereOrNull((e) => e['selected'] as bool)
?.get('content')
?.get('sectionListRenderer')
?.getList('contents')
?.firstOrNull
?.get('itemSectionRenderer')
?.getList('contents')
?.firstOrNull
?.get('gridRenderer')
2021-05-01 00:23:27 +02:00
?.getList('items')
?.firstWhereOrNull((e) => e['continuationItemRenderer'] != null)
?.get('continuationItemRenderer')
?.get('continuationEndpoint')
?.get('continuationCommand');
2021-03-18 22:22:55 +01:00
}
2021-05-01 00:23:27 +02:00
if (root.containsKey('onResponseReceivedActions')) {
2021-03-18 22:22:55 +01:00
return root
2021-05-01 00:23:27 +02:00
.getList('onResponseReceivedActions')
2021-03-18 22:22:55 +01:00
?.firstOrNull
2021-05-01 00:23:27 +02:00
?.get('appendContinuationItemsAction')
?.getList('continuationItems')
?.firstWhereOrNull((e) => e['continuationItemRenderer'] != null)
?.get('continuationItemRenderer')
?.get('continuationEndpoint')
?.get('continuationCommand');
2021-03-18 22:22:55 +01:00
}
return null;
2020-07-10 22:28:19 +02:00
}
2021-07-21 02:06:02 +02:00
ChannelVideo? _parseContent(JsonMap? 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(
2021-04-30 23:49:49 +02:00
VideoId(video.getT<String>('videoId')!),
video.get('title')?.getT<String>('simpleText') ??
video.get('title')?.getList('runs')?.map((e) => e['text']).join() ??
'',
video
.getList('thumbnailOverlays')
?.firstOrNull
?.get('thumbnailOverlayTimeStatusRenderer')
?.get('text')
?.getT<String>('simpleText')
?.toDuration() ??
Duration.zero,
video.get('thumbnail')?.getList('thumbnails')?.last.getT<String>('url') ??
'',
video.get('publishedTimeText')?.getT<String>('simpleText') ?? '',
video.get('viewCountText')?.getT<String>('simpleText')?.parseInt() ?? 0,
);
2020-07-10 22:28:19 +02:00
}
2021-03-18 22:22:55 +01:00
}
2021-05-01 00:23:27 +02:00
//