22
33import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .DISABLE_PRETTY_PRINT_PARAMETER ;
44import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .YOUTUBEI_V1_URL ;
5- import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .addClientInfoHeaders ;
65import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .fixThumbnailUrl ;
76import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getJsonPostResponse ;
87import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getKey ;
98import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getTextFromObject ;
10- import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .getValidJsonResponseBody ;
119import static org .schabi .newpipe .extractor .services .youtube .YoutubeParsingHelper .prepareDesktopJsonBuilder ;
12- import static org .schabi .newpipe .extractor .utils .Utils .UTF_8 ;
1310import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
1411
1512import com .grack .nanojson .JsonArray ;
2017import org .schabi .newpipe .extractor .StreamingService ;
2118import org .schabi .newpipe .extractor .channel .ChannelExtractor ;
2219import org .schabi .newpipe .extractor .downloader .Downloader ;
23- import org .schabi .newpipe .extractor .downloader .Response ;
2420import org .schabi .newpipe .extractor .exceptions .ContentNotAvailableException ;
2521import org .schabi .newpipe .extractor .exceptions .ContentNotSupportedException ;
2622import org .schabi .newpipe .extractor .exceptions .ExtractionException ;
3127import org .schabi .newpipe .extractor .services .youtube .linkHandler .YoutubeChannelLinkHandlerFactory ;
3228import org .schabi .newpipe .extractor .stream .StreamInfoItem ;
3329import org .schabi .newpipe .extractor .stream .StreamInfoItemsCollector ;
34- import org .schabi .newpipe .extractor .utils .JsonUtils ;
3530import org .schabi .newpipe .extractor .utils .Utils ;
3631
3732import java .io .IOException ;
33+ import java .nio .charset .StandardCharsets ;
3834import java .util .ArrayList ;
39- import java .util .HashMap ;
4035import java .util .List ;
41- import java .util .Map ;
36+ import java .util .Objects ;
4237
4338import javax .annotation .Nonnull ;
4439import javax .annotation .Nullable ;
@@ -85,8 +80,8 @@ public YoutubeChannelExtractor(final StreamingService service,
8580 }
8681
8782 @ Override
88- public void onFetchPage (@ Nonnull final Downloader downloader ) throws IOException ,
89- ExtractionException {
83+ public void onFetchPage (@ Nonnull final Downloader downloader )
84+ throws IOException , ExtractionException {
9085 final String channelPath = super .getId ();
9186 final String [] channelId = channelPath .split ("/" );
9287 String id = "" ;
@@ -98,22 +93,12 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
9893 getExtractorLocalization (), getExtractorContentCountry ())
9994 .value ("url" , "https://www.youtube.com/" + channelPath )
10095 .done ())
101- .getBytes (UTF_8 );
96+ .getBytes (StandardCharsets . UTF_8 );
10297
10398 final JsonObject jsonResponse = getJsonPostResponse ("navigation/resolve_url" ,
10499 body , getExtractorLocalization ());
105100
106- if (!isNullOrEmpty (jsonResponse .getObject ("error" ))) {
107- final JsonObject errorJsonObject = jsonResponse .getObject ("error" );
108- final int errorCode = errorJsonObject .getInt ("code" );
109- if (errorCode == 404 ) {
110- throw new ContentNotAvailableException ("This channel doesn't exist." );
111- } else {
112- throw new ContentNotAvailableException ("Got error:\" "
113- + errorJsonObject .getString ("status" ) + "\" : "
114- + errorJsonObject .getString ("message" ));
115- }
116- }
101+ checkIfChannelResponseIsValid (jsonResponse );
117102
118103 final JsonObject endpoint = jsonResponse .getObject ("endpoint" );
119104
@@ -146,22 +131,12 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
146131 .value ("browseId" , id )
147132 .value ("params" , "EgZ2aWRlb3M%3D" ) // Equal to videos
148133 .done ())
149- .getBytes (UTF_8 );
134+ .getBytes (StandardCharsets . UTF_8 );
150135
151136 final JsonObject jsonResponse = getJsonPostResponse ("browse" , body ,
152137 getExtractorLocalization ());
153138
154- if (!isNullOrEmpty (jsonResponse .getObject ("error" ))) {
155- final JsonObject errorJsonObject = jsonResponse .getObject ("error" );
156- final int errorCode = errorJsonObject .getInt ("code" );
157- if (errorCode == 404 ) {
158- throw new ContentNotAvailableException ("This channel doesn't exist." );
159- } else {
160- throw new ContentNotAvailableException ("Got error:\" "
161- + errorJsonObject .getString ("status" ) + "\" : "
162- + errorJsonObject .getString ("message" ));
163- }
164- }
139+ checkIfChannelResponseIsValid (jsonResponse );
165140
166141 final JsonObject endpoint = jsonResponse .getArray ("onResponseReceivedActions" )
167142 .getObject (0 )
@@ -199,6 +174,21 @@ public void onFetchPage(@Nonnull final Downloader downloader) throws IOException
199174 YoutubeParsingHelper .defaultAlertsCheck (initialData );
200175 }
201176
177+ private void checkIfChannelResponseIsValid (@ Nonnull final JsonObject jsonResponse )
178+ throws ContentNotAvailableException {
179+ if (!isNullOrEmpty (jsonResponse .getObject ("error" ))) {
180+ final JsonObject errorJsonObject = jsonResponse .getObject ("error" );
181+ final int errorCode = errorJsonObject .getInt ("code" );
182+ if (errorCode == 404 ) {
183+ throw new ContentNotAvailableException ("This channel doesn't exist." );
184+ } else {
185+ throw new ContentNotAvailableException ("Got error:\" "
186+ + errorJsonObject .getString ("status" ) + "\" : "
187+ + errorJsonObject .getString ("message" ));
188+ }
189+ }
190+ }
191+
202192 @ Nonnull
203193 @ Override
204194 public String getUrl () throws ParsingException {
@@ -340,8 +330,8 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
340330 final List <String > channelIds = new ArrayList <>();
341331 channelIds .add (getName ());
342332 channelIds .add (getUrl ());
343- final JsonObject continuation = collectStreamsFrom (collector , gridRenderer
344- .getArray ("items" ), channelIds );
333+ final JsonObject continuation = collectStreamsFrom (collector ,
334+ gridRenderer .getArray ("items" ), channelIds );
345335
346336 nextPage = getNextPageFrom (continuation , channelIds );
347337 }
@@ -350,23 +340,19 @@ public InfoItemsPage<StreamInfoItem> getInitialPage() throws IOException, Extrac
350340 }
351341
352342 @ Override
353- public InfoItemsPage <StreamInfoItem > getPage (final Page page ) throws IOException ,
354- ExtractionException {
343+ public InfoItemsPage <StreamInfoItem > getPage (final Page page )
344+ throws IOException , ExtractionException {
355345 if (page == null || isNullOrEmpty (page .getUrl ())) {
356346 throw new IllegalArgumentException ("Page doesn't contain an URL" );
357347 }
358348
359349 final List <String > channelIds = page .getIds ();
360350
361351 final StreamInfoItemsCollector collector = new StreamInfoItemsCollector (getServiceId ());
362- final Map <String , List <String >> headers = new HashMap <>();
363- addClientInfoHeaders (headers );
364352
365- final Response response = getDownloader (). post ( page . getUrl (), null , page .getBody (),
353+ final JsonObject ajaxJson = getJsonPostResponse ( "browse" , page .getBody (),
366354 getExtractorLocalization ());
367355
368- final JsonObject ajaxJson = JsonUtils .toJsonObject (getValidJsonResponseBody (response ));
369-
370356 final JsonObject sectionListContinuation = ajaxJson .getArray ("onResponseReceivedActions" )
371357 .getObject (0 )
372358 .getObject ("appendContinuationItemsAction" );
@@ -379,8 +365,8 @@ public InfoItemsPage<StreamInfoItem> getPage(final Page page) throws IOException
379365
380366 @ Nullable
381367 private Page getNextPageFrom (final JsonObject continuations ,
382- final List <String > channelIds ) throws IOException ,
383- ExtractionException {
368+ final List <String > channelIds )
369+ throws IOException , ExtractionException {
384370 if (isNullOrEmpty (continuations )) {
385371 return null ;
386372 }
@@ -393,7 +379,7 @@ private Page getNextPageFrom(final JsonObject continuations,
393379 getExtractorContentCountry ())
394380 .value ("continuation" , continuation )
395381 .done ())
396- .getBytes (UTF_8 );
382+ .getBytes (StandardCharsets . UTF_8 );
397383
398384 return new Page (YOUTUBEI_V1_URL + "browse?key=" + getKey ()
399385 + DISABLE_PRETTY_PRINT_PARAMETER , null , channelIds , null , body );
@@ -443,39 +429,43 @@ public String getUploaderUrl() {
443429
444430 @ Nullable
445431 private JsonObject getVideoTab () throws ParsingException {
446- if (this . videoTab != null ) {
447- return this . videoTab ;
432+ if (videoTab != null ) {
433+ return videoTab ;
448434 }
449435
450436 final JsonArray tabs = initialData .getObject ("contents" )
451437 .getObject ("twoColumnBrowseResultsRenderer" )
452438 .getArray ("tabs" );
453439
454- JsonObject foundVideoTab = null ;
455- for (final Object tab : tabs ) {
456- if (((JsonObject ) tab ).has ("tabRenderer" )) {
457- if (((JsonObject ) tab ).getObject ("tabRenderer" ).getString ("title" ,
458- "" ).equals ("Videos" )) {
459- foundVideoTab = ((JsonObject ) tab ).getObject ("tabRenderer" );
460- break ;
461- }
462- }
463- }
464-
465- if (foundVideoTab == null ) {
466- throw new ContentNotSupportedException ("This channel has no Videos tab" );
467- }
468-
469- final String messageRendererText = getTextFromObject (foundVideoTab .getObject ("content" )
470- .getObject ("sectionListRenderer" ).getArray ("contents" ).getObject (0 )
471- .getObject ("itemSectionRenderer" ).getArray ("contents" ).getObject (0 )
472- .getObject ("messageRenderer" ).getObject ("text" ));
440+ final JsonObject foundVideoTab = tabs .stream ()
441+ .filter (Objects ::nonNull )
442+ .filter (JsonObject .class ::isInstance )
443+ .map (JsonObject .class ::cast )
444+ .filter (tab -> tab .has ("tabRenderer" )
445+ && tab .getObject ("tabRenderer" )
446+ .getString ("title" , "" )
447+ .equals ("Videos" ))
448+ .findFirst ()
449+ .map (tab -> tab .getObject ("tabRenderer" ))
450+ .orElseThrow (
451+ () -> new ContentNotSupportedException ("This channel has no Videos tab" ));
452+
453+ final String messageRendererText = getTextFromObject (
454+ foundVideoTab .getObject ("content" )
455+ .getObject ("sectionListRenderer" )
456+ .getArray ("contents" )
457+ .getObject (0 )
458+ .getObject ("itemSectionRenderer" )
459+ .getArray ("contents" )
460+ .getObject (0 )
461+ .getObject ("messageRenderer" )
462+ .getObject ("text" ));
473463 if (messageRendererText != null
474464 && messageRendererText .equals ("This channel has no videos." )) {
475465 return null ;
476466 }
477467
478- this . videoTab = foundVideoTab ;
468+ videoTab = foundVideoTab ;
479469 return foundVideoTab ;
480470 }
481471}
0 commit comments