From 3136af8231ac9a179d2cdfc65ce948f6324cd708 Mon Sep 17 00:00:00 2001 From: Mattia Date: Thu, 3 Nov 2022 11:56:05 +0100 Subject: [PATCH] Fix `getUploadsFromPage`. #228 --- .gitignore | 3 +- .../pages/channel_upload_page.dart | 54 ++++++++++++------- test/channel_test.dart | 7 +++ 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index a9ef816..0f19adf 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ doc/api/ *.iml /local/ -.flutter-plugins-dependencies \ No newline at end of file +.flutter-plugins-dependencies +/coverage/ diff --git a/lib/src/reverse_engineering/pages/channel_upload_page.dart b/lib/src/reverse_engineering/pages/channel_upload_page.dart index 3a9ace4..296da34 100644 --- a/lib/src/reverse_engineering/pages/channel_upload_page.dart +++ b/lib/src/reverse_engineering/pages/channel_upload_page.dart @@ -67,27 +67,36 @@ class _InitialData extends InitialData { List getContentContext() { List? context; if (root.containsKey('contents')) { - final render = root + var render = root .get('contents') ?.get('twoColumnBrowseResultsRenderer') ?.getList('tabs') ?.map((e) => e['tabRenderer']) .cast() .firstWhereOrNull((e) => e['selected'] as bool? ?? false) - ?.get('content') - ?.get('sectionListRenderer') - ?.getList('contents') - ?.firstOrNull - ?.get('itemSectionRenderer') - ?.getList('contents') - ?.firstOrNull; - - if (render?.containsKey('gridRenderer') ?? false) { - context = - render?.get('gridRenderer')?.getList('items')?.cast(); - } else if (render?.containsKey('messageRenderer') ?? false) { - // Workaround for no-videos. - context = const []; + ?.get('content'); + + if (render != null) { + if (render.containsKey('sectionListRenderer')) { + render = render + .get('sectionListRenderer') + ?.getList('contents') + ?.firstOrNull + ?.get('itemSectionRenderer') + ?.getList('contents') + ?.firstOrNull; + + if (render?.containsKey('gridRenderer') ?? false) { + context = + render?.get('gridRenderer')?.getList('items')?.cast(); + } else if (render?.containsKey('messageRenderer') ?? false) { + // Workaround for no-videos. + context = const []; + } + } else if (render.containsKey('richGridRenderer')) { + context = + render.get('richGridRenderer')?.getList('contents') ?? const []; + } } } if (context == null && root.containsKey('onResponseReceivedActions')) { @@ -112,7 +121,7 @@ class _InitialData extends InitialData { ?.getList('tabs') ?.map((e) => e['tabRenderer']) .cast() - .firstWhereOrNull((e) => e['selected'] as bool) + .firstWhereOrNull((e) => e['selected'] as bool? ?? false) ?.get('content') ?.get('sectionListRenderer') ?.getList('contents') @@ -142,11 +151,20 @@ class _InitialData extends InitialData { } ChannelVideo? _parseContent(JsonMap? content) { - if (content == null || !content.containsKey('gridVideoRenderer')) { + if (content == null) { return null; } - var video = content.get('gridVideoRenderer')!; + Map? video; + if (content.containsKey('gridVideoRenderer')) { + video = content.get('gridVideoRenderer'); + } else if (content.containsKey('richItemRenderer')) { + video = content.get('richItemRenderer')?.get('content')?.get('videoRenderer'); + } + + if (video == null) { + return null; + } return ChannelVideo( VideoId(video.getT('videoId')!), video.get('title')?.getT('simpleText') ?? diff --git a/test/channel_test.dart b/test/channel_test.dart index f786ea4..662cfe4 100644 --- a/test/channel_test.dart +++ b/test/channel_test.dart @@ -72,6 +72,13 @@ void main() { expect(videos, hasLength(30)); }); + test('Get next page of yt video', () async { + var videos = + await yt!.channels.getUploadsFromPage('UCEnBXANsKmyj2r9xVyKoDiQ'); + final nextPage = await videos.nextPage(); + expect(nextPage, hasLength(30)); + }); + //TODO: Remove dupe test test('Get about page of a youtube', () async { var aboutPage = await yt!.channels.getAboutPageByUsername(