Migrate ChannelUploadPage InitialData to JSON Class

This commit is contained in:
Mattia 2020-09-12 11:20:22 +02:00
parent 54cb3620ad
commit e2eb00243e
2 changed files with 5495 additions and 46 deletions

View File

@ -5,10 +5,10 @@ import 'package:html/parser.dart' as parser;
import '../../channels/channel_video.dart'; import '../../channels/channel_video.dart';
import '../../exceptions/exceptions.dart'; import '../../exceptions/exceptions.dart';
import '../../extensions/helpers_extension.dart';
import '../../retry.dart'; import '../../retry.dart';
import '../../videos/videos.dart'; import '../../videos/videos.dart';
import '../youtube_http_client.dart'; import '../youtube_http_client.dart';
import 'generated/channel_upload_page_id.g.dart';
/// ///
class ChannelUploadPage { class ChannelUploadPage {
@ -19,8 +19,8 @@ class ChannelUploadPage {
_InitialData _initialData; _InitialData _initialData;
/// ///
_InitialData get initialData => _InitialData get initialData => _initialData ??= _InitialData(
_initialData ??= _InitialData(json.decode(_matchJson(_extractJson( ChannelUploadPageId.fromJson(json.decode(_extractJson(
_root _root
.querySelectorAll('script') .querySelectorAll('script')
.map((e) => e.text) .map((e) => e.text)
@ -64,8 +64,8 @@ class ChannelUploadPage {
'https://www.youtube.com/browse_ajax?ctoken=${initialData.continuation}&continuation=${initialData.continuation}&itct=${initialData.clickTrackingParams}'; 'https://www.youtube.com/browse_ajax?ctoken=${initialData.continuation}&continuation=${initialData.continuation}&itct=${initialData.clickTrackingParams}';
return retry(() async { return retry(() async {
var raw = await httpClient.getString(url); var raw = await httpClient.getString(url);
return ChannelUploadPage( return ChannelUploadPage(null, channelId,
null, channelId, _InitialData(json.decode(raw)[1])); _InitialData(ChannelUploadPageId.fromJson(json.decode(raw)[1])));
}); });
} }
@ -86,11 +86,30 @@ class ChannelUploadPage {
: _root = parser.parse(raw); : _root = parser.parse(raw);
} }
void x() {
ChannelUploadPageId _id;
// @getContentContext
var x = _id.contents.twoColumnBrowseResultsRenderer.tabs
.map((e) => e.tabRenderer)
.firstWhere((e) => e.selected)
.content
.sectionListRenderer
.contents
.first
.itemSectionRenderer
.contents
.first
.gridRenderer
.items;
_id.response.continuationContents.gridContinuation.items;
}
class _InitialData { class _InitialData {
// Json parsed map // Json parsed map
final Map<String, dynamic> _root; final ChannelUploadPageId root;
_InitialData(this._root); _InitialData(this.root);
/* Cache results */ /* Cache results */
@ -98,64 +117,68 @@ class _InitialData {
String _continuation; String _continuation;
String _clickTrackingParams; String _clickTrackingParams;
List<Map<String, dynamic>> getContentContext(Map<String, dynamic> root) { List<GridContinuationItem> getContentContext() {
if (root['contents'] != null) { if (root.contents != null) {
return (_root['contents']['twoColumnBrowseResultsRenderer']['tabs'] return root.contents.twoColumnBrowseResultsRenderer.tabs
as List<dynamic>) .map((e) => e.tabRenderer)
.map((e) => e['tabRenderer']) .firstWhere((e) => e.selected)
.firstWhere((e) => e['selected'] == true)['content'] .content
['sectionListRenderer']['contents'] .sectionListRenderer
.first['itemSectionRenderer']['contents'] .contents
.first['gridRenderer']['items'] .first
.cast<Map<String, dynamic>>(); .itemSectionRenderer
.contents
.first
.gridRenderer
.items;
} }
if (root['response'] != null) { if (root.response != null) {
return _root['response']['continuationContents']['gridContinuation'] return root.response.continuationContents.gridContinuation.items;
['items']
.cast<Map<String, dynamic>>();
} }
throw FatalFailureException('Failed to get initial data context.'); throw FatalFailureException('Failed to get initial data context.');
} }
Map<String, dynamic> getContinuationContext(Map<String, dynamic> root) { NextContinuationData getContinuationContext() {
if (_root['contents'] != null) { if (root.contents != null) {
return (_root['contents']['twoColumnBrowseResultsRenderer']['tabs'] return root.contents.twoColumnBrowseResultsRenderer.tabs
as List<dynamic>) .map((e) => e.tabRenderer)
?.map((e) => e['tabRenderer']) .firstWhere((e) => e.selected)
?.firstWhere((e) => e['selected'] == true)['content'] .content
['sectionListRenderer']['contents'] .sectionListRenderer
?.first['itemSectionRenderer']['contents'] .contents
?.first['gridRenderer']['continuations'] .first
?.first['nextContinuationData'] .itemSectionRenderer
?.cast<String, dynamic>(); .contents
.first
.gridRenderer
.continuations
.first
.nextContinuationData;
} }
if (_root['response'] != null) { if (root.response != null) {
return _root['response']['continuationContents']['gridContinuation'] return root.response.continuationContents.gridContinuation.continuations
['continuations'] .first.nextContinuationData;
?.first
?.cast<String, dynamic>();
} }
return null; return null;
} }
List<ChannelVideo> get uploads => _uploads ??= getContentContext(_root) List<ChannelVideo> get uploads => _uploads ??= getContentContext()
?.map(_parseContent) ?.map(_parseContent)
?.where((e) => e != null) ?.where((e) => e != null)
?.toList() ?.toList()
?.cast<ChannelVideo>(); ?.cast<ChannelVideo>();
String get continuation => _continuation ??= String get continuation =>
getContinuationContext(_root)?.getValue('continuation') ?? ''; _continuation ??= getContinuationContext().continuation ?? '';
String get clickTrackingParams => _clickTrackingParams ??= String get clickTrackingParams => _clickTrackingParams ??=
getContinuationContext(_root)?.getValue('clickTrackingParams') ?? ''; getContinuationContext()?.clickTrackingParams ?? '';
dynamic _parseContent(content) { dynamic _parseContent(GridContinuationItem content) {
if (content == null || content['gridVideoRenderer'] == null) { if (content == null || content.gridVideoRenderer == null) {
return null; return null;
} }
var video = content['gridVideoRenderer'] as Map<String, dynamic>; var video = content.gridVideoRenderer;
return ChannelVideo( return ChannelVideo(VideoId(video.videoId), video.title.simpleText);
VideoId(video['videoId']), video['title']['simpleText']);
} }
} }

File diff suppressed because it is too large Load Diff