Skip to content

Commit 1a6b8da

Browse files
committed
Annotate YoutubeParsingHelper methods with Nonnull when needed
1 parent d8177b5 commit 1a6b8da

1 file changed

Lines changed: 46 additions & 30 deletions

File tree

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

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,25 @@ private static boolean isGoogleURL(String url) {
109109
}
110110
}
111111

112-
public static boolean isYoutubeURL(final URL url) {
112+
public static boolean isYoutubeURL(@Nonnull final URL url) {
113113
final String host = url.getHost();
114114
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
115115
|| host.equalsIgnoreCase("m.youtube.com")
116116
|| host.equalsIgnoreCase("music.youtube.com");
117117
}
118118

119-
public static boolean isYoutubeServiceURL(final URL url) {
119+
public static boolean isYoutubeServiceURL(@Nonnull final URL url) {
120120
final String host = url.getHost();
121121
return host.equalsIgnoreCase("www.youtube-nocookie.com")
122122
|| host.equalsIgnoreCase("youtu.be");
123123
}
124124

125-
public static boolean isHooktubeURL(final URL url) {
125+
public static boolean isHooktubeURL(@Nonnull final URL url) {
126126
final String host = url.getHost();
127127
return host.equalsIgnoreCase("hooktube.com");
128128
}
129129

130-
public static boolean isInvidioURL(final URL url) {
130+
public static boolean isInvidioURL(@Nonnull final URL url) {
131131
final String host = url.getHost();
132132
return host.equalsIgnoreCase("invidio.us")
133133
|| host.equalsIgnoreCase("dev.invidio.us")
@@ -164,7 +164,7 @@ public static boolean isInvidioURL(final URL url) {
164164
* @return the duration in seconds
165165
* @throws ParsingException when more than 3 separators are found
166166
*/
167-
public static int parseDurationString(final String input)
167+
public static int parseDurationString(@Nonnull final String input)
168168
throws ParsingException, NumberFormatException {
169169
// If time separator : is not detected, try . instead
170170
final String[] splitInput = input.contains(":")
@@ -205,7 +205,8 @@ public static int parseDurationString(final String input)
205205
+ Integer.parseInt(Utils.removeNonDigitCharacters(seconds));
206206
}
207207

208-
public static String getFeedUrlFrom(final String channelIdOrUser) {
208+
@Nonnull
209+
public static String getFeedUrlFrom(@Nonnull final String channelIdOrUser) {
209210
if (channelIdOrUser.startsWith("user/")) {
210211
return FEED_BASE_USER + channelIdOrUser.replace("user/", "");
211212
} else if (channelIdOrUser.startsWith("channel/")) {
@@ -236,7 +237,7 @@ public static OffsetDateTime parseDateFrom(final String textualUploadDate)
236237
* @param playlistId the id of the playlist
237238
* @return Whether given id belongs to a YouTube Mix
238239
*/
239-
public static boolean isYoutubeMixId(final String playlistId) {
240+
public static boolean isYoutubeMixId(@Nonnull final String playlistId) {
240241
return playlistId.startsWith("RD") && !isYoutubeMusicMixId(playlistId);
241242
}
242243

@@ -247,7 +248,7 @@ public static boolean isYoutubeMixId(final String playlistId) {
247248
* @param playlistId the playlist id
248249
* @return Whether given id belongs to a YouTube Music Mix
249250
*/
250-
public static boolean isYoutubeMusicMixId(final String playlistId) {
251+
public static boolean isYoutubeMusicMixId(@Nonnull final String playlistId) {
251252
return playlistId.startsWith("RDAMVM") || playlistId.startsWith("RDCLAK");
252253
}
253254

@@ -257,7 +258,7 @@ public static boolean isYoutubeMusicMixId(final String playlistId) {
257258
*
258259
* @return Whether given id belongs to a YouTube Channel Mix
259260
*/
260-
public static boolean isYoutubeChannelMixId(final String playlistId) {
261+
public static boolean isYoutubeChannelMixId(@Nonnull final String playlistId) {
261262
return playlistId.startsWith("RDCM");
262263
}
263264

@@ -266,7 +267,9 @@ public static boolean isYoutubeChannelMixId(final String playlistId) {
266267
*
267268
* @throws ParsingException If the playlistId is a Channel Mix or not a mix.
268269
*/
269-
public static String extractVideoIdFromMixId(final String playlistId) throws ParsingException {
270+
@Nonnull
271+
public static String extractVideoIdFromMixId(@Nonnull final String playlistId)
272+
throws ParsingException {
270273
if (playlistId.startsWith("RDMM")) { // My Mix
271274
return playlistId.substring(4);
272275

@@ -555,7 +558,7 @@ public static String[] getYoutubeMusicKey() throws IOException, ReCaptchaExcepti
555558
}
556559

557560
@Nullable
558-
public static String getUrlFromNavigationEndpoint(final JsonObject navigationEndpoint)
561+
public static String getUrlFromNavigationEndpoint(@Nonnull final JsonObject navigationEndpoint)
559562
throws ParsingException {
560563
if (navigationEndpoint.has("urlEndpoint")) {
561564
String internUrl = navigationEndpoint.getObject("urlEndpoint").getString("url");
@@ -668,7 +671,7 @@ public static String getTextFromObject(final JsonObject textObject) throws Parsi
668671
}
669672

670673
@Nullable
671-
public static String getTextAtKey(final JsonObject jsonObject, final String key)
674+
public static String getTextAtKey(@Nonnull final JsonObject jsonObject, final String key)
672675
throws ParsingException {
673676
if (jsonObject.isString(key)) {
674677
return jsonObject.getString(key);
@@ -677,7 +680,7 @@ public static String getTextAtKey(final JsonObject jsonObject, final String key)
677680
}
678681
}
679682

680-
public static String fixThumbnailUrl(String thumbnailUrl) {
683+
public static String fixThumbnailUrl(@Nonnull String thumbnailUrl) {
681684
if (thumbnailUrl.startsWith("//")) {
682685
thumbnailUrl = thumbnailUrl.substring(2);
683686
}
@@ -691,7 +694,8 @@ public static String fixThumbnailUrl(String thumbnailUrl) {
691694
return thumbnailUrl;
692695
}
693696

694-
public static String getValidJsonResponseBody(final Response response)
697+
@Nonnull
698+
public static String getValidJsonResponseBody(@Nonnull final Response response)
695699
throws ParsingException, MalformedURLException {
696700
if (response.responseCode() == 404) {
697701
throw new ContentNotAvailableException("Not found"
@@ -748,7 +752,8 @@ public static JsonObject getJsonPostResponse(final String endpoint,
748752

749753
public static JsonObject getJsonMobilePostResponse(final String endpoint,
750754
final byte[] body,
751-
final ContentCountry contentCountry,
755+
@Nonnull final ContentCountry
756+
contentCountry,
752757
final Localization localization)
753758
throws IOException, ExtractionException {
754759
final Map<String, List<String>> headers = new HashMap<>();
@@ -776,7 +781,8 @@ public static JsonArray getJsonResponse(final String url, final Localization loc
776781
return JsonUtils.toJsonArray(getValidJsonResponseBody(response));
777782
}
778783

779-
public static JsonArray getJsonResponse(final Page page, final Localization localization)
784+
public static JsonArray getJsonResponse(@Nonnull final Page page,
785+
final Localization localization)
780786
throws IOException, ExtractionException {
781787
final Map<String, List<String>> headers = new HashMap<>();
782788
addYouTubeHeaders(headers);
@@ -786,8 +792,11 @@ public static JsonArray getJsonResponse(final Page page, final Localization loca
786792
return JsonUtils.toJsonArray(getValidJsonResponseBody(response));
787793
}
788794

789-
public static JsonBuilder<JsonObject> prepareJsonBuilder(final Localization localization,
790-
final ContentCountry contentCountry)
795+
@Nonnull
796+
public static JsonBuilder<JsonObject> prepareJsonBuilder(@Nonnull final Localization
797+
localization,
798+
@Nonnull final ContentCountry
799+
contentCountry)
791800
throws IOException, ExtractionException {
792801
// @formatter:off
793802
return JsonObject.builder()
@@ -802,8 +811,10 @@ public static JsonBuilder<JsonObject> prepareJsonBuilder(final Localization loca
802811
// @formatter:on
803812
}
804813

805-
public static JsonBuilder<JsonObject> prepareMobileJsonBuilder(final Localization localization,
806-
final ContentCountry
814+
@Nonnull
815+
public static JsonBuilder<JsonObject> prepareMobileJsonBuilder(@Nonnull final Localization
816+
localization,
817+
@Nonnull final ContentCountry
807818
contentCountry)
808819
throws IOException, ExtractionException {
809820
// @formatter:off
@@ -835,7 +846,7 @@ public static void addYouTubeHeaders(final Map<String, List<String>> headers)
835846
* <code>Origin</code>, and <code>Referer</code> headers.
836847
* @param headers The headers which should be completed
837848
*/
838-
public static void addClientInfoHeaders(final Map<String, List<String>> headers)
849+
public static void addClientInfoHeaders(@Nonnull final Map<String, List<String>> headers)
839850
throws IOException, ExtractionException {
840851
if (headers.get("Origin") == null) {
841852
headers.put("Origin", Collections.singletonList("https://www.youtube.com"));
@@ -856,20 +867,22 @@ public static void addClientInfoHeaders(final Map<String, List<String>> headers)
856867
* @see #CONSENT_COOKIE
857868
* @param headers the headers which should be completed
858869
*/
859-
public static void addCookieHeader(final Map<String, List<String>> headers) {
870+
public static void addCookieHeader(@Nonnull final Map<String, List<String>> headers) {
860871
if (headers.get("Cookie") == null) {
861872
headers.put("Cookie", Arrays.asList(generateConsentCookie()));
862873
} else {
863874
headers.get("Cookie").add(generateConsentCookie());
864875
}
865876
}
866877

878+
@Nonnull
867879
public static String generateConsentCookie() {
868880
final int statusCode = 100 + numberGenerator.nextInt(900);
869881
return CONSENT_COOKIE + statusCode;
870882
}
871883

872-
public static String extractCookieValue(final String cookieName, final Response response) {
884+
public static String extractCookieValue(final String cookieName,
885+
@Nonnull final Response response) {
873886
final List<String> cookies = response.responseHeaders().get("set-cookie");
874887
int startIndex;
875888
String result = "";
@@ -892,7 +905,8 @@ public static String extractCookieValue(final String cookieName, final Response
892905
* @param initialData the object which will be checked if an alert is present
893906
* @throws ContentNotAvailableException if an alert is detected
894907
*/
895-
public static void defaultAlertsCheck(final JsonObject initialData) throws ParsingException {
908+
public static void defaultAlertsCheck(@Nonnull final JsonObject initialData)
909+
throws ParsingException {
896910
final JsonArray alerts = initialData.getArray("alerts");
897911
if (!isNullOrEmpty(alerts)) {
898912
final JsonObject alertRenderer = alerts.getObject(0).getObject("alertRenderer");
@@ -902,7 +916,7 @@ public static void defaultAlertsCheck(final JsonObject initialData) throws Parsi
902916
if (alertText != null && alertText.contains("This account has been terminated")) {
903917
if (alertText.contains("violation") || alertText.contains("violating")
904918
|| alertText.contains("infringement")) {
905-
// possible error messages:
919+
// Possible error messages:
906920
// "This account has been terminated for a violation of YouTube's Terms of Service."
907921
// "This account has been terminated due to multiple or severe violations of YouTube's policy prohibiting hate speech."
908922
// "This account has been terminated due to multiple or severe violations of YouTube's policy prohibiting content designed to harass, bully or threaten."
@@ -922,7 +936,8 @@ public static void defaultAlertsCheck(final JsonObject initialData) throws Parsi
922936
}
923937

924938
@Nonnull
925-
public static List<MetaInfo> getMetaInfo(final JsonArray contents) throws ParsingException {
939+
public static List<MetaInfo> getMetaInfo(@Nonnull final JsonArray contents)
940+
throws ParsingException {
926941
final List<MetaInfo> metaInfo = new ArrayList<>();
927942
for (final Object content : contents) {
928943
final JsonObject resultObject = (JsonObject) content;
@@ -948,7 +963,7 @@ public static List<MetaInfo> getMetaInfo(final JsonArray contents) throws Parsin
948963
}
949964

950965
@Nonnull
951-
private static MetaInfo getInfoPanelContent(final JsonObject infoPanelContentRenderer)
966+
private static MetaInfo getInfoPanelContent(@Nonnull final JsonObject infoPanelContentRenderer)
952967
throws ParsingException {
953968
final MetaInfo metaInfo = new MetaInfo();
954969
final StringBuilder sb = new StringBuilder();
@@ -981,7 +996,7 @@ private static MetaInfo getInfoPanelContent(final JsonObject infoPanelContentRen
981996
}
982997

983998
@Nonnull
984-
private static MetaInfo getClarificationRendererContent(final JsonObject clarificationRenderer)
999+
private static MetaInfo getClarificationRendererContent(@Nonnull final JsonObject clarificationRenderer)
9851000
throws ParsingException {
9861001
final MetaInfo metaInfo = new MetaInfo();
9871002

@@ -1018,7 +1033,7 @@ private static MetaInfo getClarificationRendererContent(final JsonObject clarifi
10181033
.has("secondarySource")) {
10191034
final String url = getUrlFromNavigationEndpoint(clarificationRenderer
10201035
.getObject("secondaryEndpoint"));
1021-
// ignore Google URLs, because those point to a Google search about "Covid-19"
1036+
// Ignore Google URLs, because those point to a Google search about "Covid-19"
10221037
if (url != null && !isGoogleURL(url)) {
10231038
try {
10241039
metaInfo.addUrl(new URL(url));
@@ -1068,7 +1083,8 @@ public static boolean isVerified(final JsonArray badges) {
10681083
return false;
10691084
}
10701085

1071-
public static String unescapeDocument(final String doc) {
1086+
@Nonnull
1087+
public static String unescapeDocument(@Nonnull final String doc) {
10721088
return doc
10731089
.replaceAll("\\\\x22", "\"")
10741090
.replaceAll("\\\\x7b", "{")

0 commit comments

Comments
 (0)