Skip to content

Commit 41a8ed6

Browse files
committed
Make some vars final and add annotations to methods
1 parent bc6de14 commit 41a8ed6

2 files changed

Lines changed: 27 additions & 26 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.schabi.newpipe.extractor.services.youtube;
22

3+
import com.grack.nanojson.*;
34
import com.grack.nanojson.JsonArray;
45
import com.grack.nanojson.JsonObject;
56
import com.grack.nanojson.JsonParser;
@@ -8,16 +9,20 @@
89

910
import org.jsoup.Jsoup;
1011
import org.jsoup.nodes.Document;
12+
import org.schabi.newpipe.extractor.MetaInfo;
1113
import org.schabi.newpipe.extractor.Page;
1214
import org.schabi.newpipe.extractor.downloader.Response;
1315
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
1416
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
1517
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1618
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
1719
import org.schabi.newpipe.extractor.localization.Localization;
20+
import org.schabi.newpipe.extractor.stream.Description;
1821
import org.schabi.newpipe.extractor.utils.Parser;
1922
import org.schabi.newpipe.extractor.utils.Utils;
2023

24+
import javax.annotation.Nonnull;
25+
import javax.annotation.Nullable;
2126
import java.io.IOException;
2227
import java.io.UnsupportedEncodingException;
2328
import java.net.MalformedURLException;
@@ -28,13 +33,11 @@
2833
import java.time.OffsetDateTime;
2934
import java.time.ZoneOffset;
3035
import 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

3638
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
3739
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
40+
import static org.schabi.newpipe.extractor.utils.Utils.*;
3841
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
3942
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
4043
import 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
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeMusicSearchExtractorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
1111
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
1212

13-
import java.net.URLEncoder;
14-
1513
import javax.annotation.Nullable;
14+
import java.net.URLEncoder;
1615

1716
import static java.util.Collections.singletonList;
1817
import static org.schabi.newpipe.extractor.ServiceList.YouTube;

0 commit comments

Comments
 (0)