11package org .schabi .newpipe .extractor .services .youtube ;
22
3+ import com .grack .nanojson .*;
34import com .grack .nanojson .JsonArray ;
45import com .grack .nanojson .JsonObject ;
56import com .grack .nanojson .JsonParser ;
89
910import org .jsoup .Jsoup ;
1011import org .jsoup .nodes .Document ;
12+ import org .schabi .newpipe .extractor .MetaInfo ;
1113import org .schabi .newpipe .extractor .Page ;
1214import org .schabi .newpipe .extractor .downloader .Response ;
1315import org .schabi .newpipe .extractor .exceptions .ContentNotAvailableException ;
1416import org .schabi .newpipe .extractor .exceptions .ExtractionException ;
1517import org .schabi .newpipe .extractor .exceptions .ParsingException ;
1618import org .schabi .newpipe .extractor .exceptions .ReCaptchaException ;
1719import org .schabi .newpipe .extractor .localization .Localization ;
20+ import org .schabi .newpipe .extractor .stream .Description ;
1821import org .schabi .newpipe .extractor .utils .Parser ;
1922import org .schabi .newpipe .extractor .utils .Utils ;
2023
24+ import javax .annotation .Nonnull ;
25+ import javax .annotation .Nullable ;
2126import java .io .IOException ;
2227import java .io .UnsupportedEncodingException ;
2328import java .net .MalformedURLException ;
2833import java .time .OffsetDateTime ;
2934import java .time .ZoneOffset ;
3035import java .time .format .DateTimeParseException ;
31- import java .util .Collections ;
32- import java .util .HashMap ;
33- import java .util .List ;
34- import java .util .Map ;
36+ import java .util .*;
3537
3638import static org .schabi .newpipe .extractor .NewPipe .getDownloader ;
3739import static org .schabi .newpipe .extractor .utils .JsonUtils .EMPTY_STRING ;
40+ import static org .schabi .newpipe .extractor .utils .Utils .*;
3841import static org .schabi .newpipe .extractor .utils .Utils .HTTP ;
3942import static org .schabi .newpipe .extractor .utils .Utils .HTTPS ;
4043import static org .schabi .newpipe .extractor .utils .Utils .isNullOrEmpty ;
@@ -87,27 +90,24 @@ private static boolean isGoogleURL(String url) {
8790 }
8891 }
8992
90- return document ;
91- }
92-
93- public static boolean isYoutubeURL (URL url ) {
94- String host = url .getHost ();
93+ public static boolean isYoutubeURL (final URL url ) {
94+ final String host = url .getHost ();
9595 return host .equalsIgnoreCase ("youtube.com" ) || host .equalsIgnoreCase ("www.youtube.com" )
9696 || host .equalsIgnoreCase ("m.youtube.com" ) || host .equalsIgnoreCase ("music.youtube.com" );
9797 }
9898
99- public static boolean isYoutubeServiceURL (URL url ) {
100- String host = url .getHost ();
99+ public static boolean isYoutubeServiceURL (final URL url ) {
100+ final String host = url .getHost ();
101101 return host .equalsIgnoreCase ("www.youtube-nocookie.com" ) || host .equalsIgnoreCase ("youtu.be" );
102102 }
103103
104- public static boolean isHooktubeURL (URL url ) {
105- String host = url .getHost ();
104+ public static boolean isHooktubeURL (final URL url ) {
105+ final String host = url .getHost ();
106106 return host .equalsIgnoreCase ("hooktube.com" );
107107 }
108108
109- public static boolean isInvidioURL (URL url ) {
110- String host = url .getHost ();
109+ public static boolean isInvidioURL (final URL url ) {
110+ final String host = url .getHost ();
111111 return host .equalsIgnoreCase ("invidio.us" )
112112 || host .equalsIgnoreCase ("dev.invidio.us" )
113113 || host .equalsIgnoreCase ("www.invidio.us" )
@@ -181,7 +181,7 @@ public static String getFeedUrlFrom(final String channelIdOrUser) {
181181 }
182182 }
183183
184- public static OffsetDateTime parseDateFrom (String textualUploadDate ) throws ParsingException {
184+ public static OffsetDateTime parseDateFrom (final String textualUploadDate ) throws ParsingException {
185185 try {
186186 return OffsetDateTime .parse (textualUploadDate );
187187 } catch (DateTimeParseException e ) {
@@ -244,7 +244,7 @@ public static String extractVideoIdFromMixId(final String playlistId) throws Par
244244 }
245245 }
246246
247- public static JsonObject getInitialData (String html ) throws ParsingException {
247+ public static JsonObject getInitialData (final String html ) throws ParsingException {
248248 try {
249249 try {
250250 final String initialData = Parser .matchGroup1 ("window\\ [\" ytInitialData\" \\ ]\\ s*=\\ s*(\\ {.*?\\ });" , html );
@@ -261,10 +261,9 @@ public static JsonObject getInitialData(String html) throws ParsingException {
261261 public static boolean isHardcodedClientVersionValid () throws IOException , ExtractionException {
262262 final String url = "https://www.youtube.com/results?search_query=test&pbj=1" ;
263263
264- Map <String , List <String >> headers = new HashMap <>();
264+ final Map <String , List <String >> headers = new HashMap <>();
265265 headers .put ("X-YouTube-Client-Name" , Collections .singletonList ("1" ));
266- headers .put ("X-YouTube-Client-Version" ,
267- Collections .singletonList (HARDCODED_CLIENT_VERSION ));
266+ headers .put ("X-YouTube-Client-Version" , Collections .singletonList (HARDCODED_CLIENT_VERSION ));
268267 final String response = getDownloader ().get (url , headers ).responseBody ();
269268
270269 return response .length () > 50 ; // ensure to have a valid response
@@ -387,14 +386,14 @@ public static boolean areHardcodedYoutubeMusicKeysValid() throws IOException, Re
387386 .end ().done ().getBytes ("UTF-8" );
388387 // @formatter:on
389388
390- Map <String , List <String >> headers = new HashMap <>();
389+ final Map <String , List <String >> headers = new HashMap <>();
391390 headers .put ("X-YouTube-Client-Name" , Collections .singletonList (HARDCODED_YOUTUBE_MUSIC_KEYS [1 ]));
392391 headers .put ("X-YouTube-Client-Version" , Collections .singletonList (HARDCODED_YOUTUBE_MUSIC_KEYS [2 ]));
393392 headers .put ("Origin" , Collections .singletonList ("https://music.youtube.com" ));
394393 headers .put ("Referer" , Collections .singletonList ("music.youtube.com" ));
395394 headers .put ("Content-Type" , Collections .singletonList ("application/json" ));
396395
397- String response = getDownloader ().post (url , headers , json ).responseBody ();
396+ final String response = getDownloader ().post (url , headers , json ).responseBody ();
398397
399398 return response .length () > 50 ; // ensure to have a valid response
400399 }
@@ -429,6 +428,7 @@ public static String[] getYoutubeMusicKeys() throws IOException, ReCaptchaExcept
429428 return youtubeMusicKeys = new String []{key , clientName , clientVersion };
430429 }
431430
431+ @ Nullable
432432 public static String getUrlFromNavigationEndpoint (JsonObject navigationEndpoint ) throws ParsingException {
433433 if (navigationEndpoint .has ("urlEndpoint" )) {
434434 String internUrl = navigationEndpoint .getObject ("urlEndpoint" ).getString ("url" );
@@ -490,15 +490,16 @@ public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint)
490490 * @param html whether to return HTML, by parsing the navigationEndpoint
491491 * @return text in the JSON object or {@code null}
492492 */
493+ @ Nullable
493494 public static String getTextFromObject (JsonObject textObject , boolean html ) throws ParsingException {
494495 if (isNullOrEmpty (textObject )) return null ;
495496
496497 if (textObject .has ("simpleText" )) return textObject .getString ("simpleText" );
497498
498499 if (textObject .getArray ("runs" ).isEmpty ()) return null ;
499500
500- StringBuilder textBuilder = new StringBuilder ();
501- for (Object textPart : textObject .getArray ("runs" )) {
501+ final StringBuilder textBuilder = new StringBuilder ();
502+ for (final Object textPart : textObject .getArray ("runs" )) {
502503 String text = ((JsonObject ) textPart ).getString ("text" );
503504 if (html && ((JsonObject ) textPart ).has ("navigationEndpoint" )) {
504505 String url = getUrlFromNavigationEndpoint (((JsonObject ) textPart ).getObject ("navigationEndpoint" ));
@@ -520,6 +521,7 @@ public static String getTextFromObject(JsonObject textObject, boolean html) thro
520521 return text ;
521522 }
522523
524+ @ Nullable
523525 public static String getTextFromObject (JsonObject textObject ) throws ParsingException {
524526 return getTextFromObject (textObject , false );
525527 }
0 commit comments