Skip to content

Commit

Permalink
Annotate YoutubeParsingHelper methods with Nonnull when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
AudricV committed Jun 15, 2021
1 parent 651674a commit 5f4f123
Showing 1 changed file with 46 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,25 @@ private static boolean isGoogleURL(String url) {
}
}

public static boolean isYoutubeURL(final URL url) {
public static boolean isYoutubeURL(@Nonnull final URL url) {
final String host = url.getHost();
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
|| host.equalsIgnoreCase("m.youtube.com")
|| host.equalsIgnoreCase("music.youtube.com");
}

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

public static boolean isHooktubeURL(final URL url) {
public static boolean isHooktubeURL(@Nonnull final URL url) {
final String host = url.getHost();
return host.equalsIgnoreCase("hooktube.com");
}

public static boolean isInvidioURL(final URL url) {
public static boolean isInvidioURL(@Nonnull final URL url) {
final String host = url.getHost();
return host.equalsIgnoreCase("invidio.us")
|| host.equalsIgnoreCase("dev.invidio.us")
Expand Down Expand Up @@ -156,7 +156,7 @@ public static boolean isInvidioURL(final URL url) {
* @return the duration in seconds
* @throws ParsingException when more than 3 separators are found
*/
public static int parseDurationString(final String input)
public static int parseDurationString(@Nonnull final String input)
throws ParsingException, NumberFormatException {
// If time separator : is not detected, try . instead
final String[] splitInput = input.contains(":")
Expand Down Expand Up @@ -197,7 +197,8 @@ public static int parseDurationString(final String input)
+ Integer.parseInt(Utils.removeNonDigitCharacters(seconds));
}

public static String getFeedUrlFrom(final String channelIdOrUser) {
@Nonnull
public static String getFeedUrlFrom(@Nonnull final String channelIdOrUser) {
if (channelIdOrUser.startsWith("user/")) {
return FEED_BASE_USER + channelIdOrUser.replace("user/", "");
} else if (channelIdOrUser.startsWith("channel/")) {
Expand Down Expand Up @@ -228,7 +229,7 @@ public static OffsetDateTime parseDateFrom(final String textualUploadDate)
* @param playlistId the id of the playlist
* @return Whether given id belongs to a YouTube Mix
*/
public static boolean isYoutubeMixId(final String playlistId) {
public static boolean isYoutubeMixId(@Nonnull final String playlistId) {
return playlistId.startsWith("RD") && !isYoutubeMusicMixId(playlistId);
}

Expand All @@ -239,7 +240,7 @@ public static boolean isYoutubeMixId(final String playlistId) {
* @param playlistId the playlist id
* @return Whether given id belongs to a YouTube Music Mix
*/
public static boolean isYoutubeMusicMixId(final String playlistId) {
public static boolean isYoutubeMusicMixId(@Nonnull final String playlistId) {
return playlistId.startsWith("RDAMVM") || playlistId.startsWith("RDCLAK");
}

Expand All @@ -249,7 +250,7 @@ public static boolean isYoutubeMusicMixId(final String playlistId) {
*
* @return Whether given id belongs to a YouTube Channel Mix
*/
public static boolean isYoutubeChannelMixId(final String playlistId) {
public static boolean isYoutubeChannelMixId(@Nonnull final String playlistId) {
return playlistId.startsWith("RDCM");
}

Expand All @@ -258,7 +259,9 @@ public static boolean isYoutubeChannelMixId(final String playlistId) {
*
* @throws ParsingException If the playlistId is a Channel Mix or not a mix.
*/
public static String extractVideoIdFromMixId(final String playlistId) throws ParsingException {
@Nonnull
public static String extractVideoIdFromMixId(@Nonnull final String playlistId)
throws ParsingException {
if (playlistId.startsWith("RDMM")) { // My Mix
return playlistId.substring(4);

Expand Down Expand Up @@ -547,7 +550,7 @@ public static String[] getYoutubeMusicKey() throws IOException, ReCaptchaExcepti
}

@Nullable
public static String getUrlFromNavigationEndpoint(final JsonObject navigationEndpoint)
public static String getUrlFromNavigationEndpoint(@Nonnull final JsonObject navigationEndpoint)
throws ParsingException {
if (navigationEndpoint.has("urlEndpoint")) {
String internUrl = navigationEndpoint.getObject("urlEndpoint").getString("url");
Expand Down Expand Up @@ -660,7 +663,7 @@ public static String getTextFromObject(final JsonObject textObject) throws Parsi
}

@Nullable
public static String getTextAtKey(final JsonObject jsonObject, final String key)
public static String getTextAtKey(@Nonnull final JsonObject jsonObject, final String key)
throws ParsingException {
if (jsonObject.isString(key)) {
return jsonObject.getString(key);
Expand All @@ -669,7 +672,7 @@ public static String getTextAtKey(final JsonObject jsonObject, final String key)
}
}

public static String fixThumbnailUrl(String thumbnailUrl) {
public static String fixThumbnailUrl(@Nonnull String thumbnailUrl) {
if (thumbnailUrl.startsWith("//")) {
thumbnailUrl = thumbnailUrl.substring(2);
}
Expand All @@ -683,7 +686,8 @@ public static String fixThumbnailUrl(String thumbnailUrl) {
return thumbnailUrl;
}

public static String getValidJsonResponseBody(final Response response)
@Nonnull
public static String getValidJsonResponseBody(@Nonnull final Response response)
throws ParsingException, MalformedURLException {
if (response.responseCode() == 404) {
throw new ContentNotAvailableException("Not found"
Expand Down Expand Up @@ -740,7 +744,8 @@ public static JsonObject getJsonPostResponse(final String endpoint,

public static JsonObject getJsonMobilePostResponse(final String endpoint,
final byte[] body,
final ContentCountry contentCountry,
@Nonnull final ContentCountry
contentCountry,
final Localization localization)
throws IOException, ExtractionException {
final Map<String, List<String>> headers = new HashMap<>();
Expand Down Expand Up @@ -768,7 +773,8 @@ public static JsonArray getJsonResponse(final String url, final Localization loc
return JsonUtils.toJsonArray(getValidJsonResponseBody(response));
}

public static JsonArray getJsonResponse(final Page page, final Localization localization)
public static JsonArray getJsonResponse(@Nonnull final Page page,
final Localization localization)
throws IOException, ExtractionException {
final Map<String, List<String>> headers = new HashMap<>();
addYouTubeHeaders(headers);
Expand All @@ -778,8 +784,11 @@ public static JsonArray getJsonResponse(final Page page, final Localization loca
return JsonUtils.toJsonArray(getValidJsonResponseBody(response));
}

public static JsonBuilder<JsonObject> prepareJsonBuilder(final Localization localization,
final ContentCountry contentCountry)
@Nonnull
public static JsonBuilder<JsonObject> prepareJsonBuilder(@Nonnull final Localization
localization,
@Nonnull final ContentCountry
contentCountry)
throws IOException, ExtractionException {
// @formatter:off
return JsonObject.builder()
Expand All @@ -794,8 +803,10 @@ public static JsonBuilder<JsonObject> prepareJsonBuilder(final Localization loca
// @formatter:on
}

public static JsonBuilder<JsonObject> prepareMobileJsonBuilder(final Localization localization,
final ContentCountry
@Nonnull
public static JsonBuilder<JsonObject> prepareMobileJsonBuilder(@Nonnull final Localization
localization,
@Nonnull final ContentCountry
contentCountry)
throws IOException, ExtractionException {
// @formatter:off
Expand Down Expand Up @@ -827,7 +838,7 @@ public static void addYouTubeHeaders(final Map<String, List<String>> headers)
* <code>Origin</code>, and <code>Referer</code> headers.
* @param headers The headers which should be completed
*/
public static void addClientInfoHeaders(final Map<String, List<String>> headers)
public static void addClientInfoHeaders(@Nonnull final Map<String, List<String>> headers)
throws IOException, ExtractionException {
if (headers.get("Origin") == null) {
headers.put("Origin", Collections.singletonList("https://www.youtube.com"));
Expand All @@ -848,19 +859,21 @@ public static void addClientInfoHeaders(final Map<String, List<String>> headers)
* @see #CONSENT_COOKIE
* @param headers the headers which should be completed
*/
public static void addCookieHeader(final Map<String, List<String>> headers) {
public static void addCookieHeader(@Nonnull final Map<String, List<String>> headers) {
if (headers.get("Cookie") == null) {
headers.put("Cookie", Arrays.asList(generateConsentCookie()));
} else {
headers.get("Cookie").add(generateConsentCookie());
}
}

@Nonnull
public static String generateConsentCookie() {
return CONSENT_COOKIE + 100 + numberGenerator.nextInt(900);
}

public static String extractCookieValue(final String cookieName, final Response response) {
public static String extractCookieValue(final String cookieName,
@Nonnull final Response response) {
final List<String> cookies = response.responseHeaders().get("set-cookie");
int startIndex;
String result = "";
Expand All @@ -883,7 +896,8 @@ public static String extractCookieValue(final String cookieName, final Response
* @param initialData the object which will be checked if an alert is present
* @throws ContentNotAvailableException if an alert is detected
*/
public static void defaultAlertsCheck(final JsonObject initialData) throws ParsingException {
public static void defaultAlertsCheck(@Nonnull final JsonObject initialData)
throws ParsingException {
final JsonArray alerts = initialData.getArray("alerts");
if (!isNullOrEmpty(alerts)) {
final JsonObject alertRenderer = alerts.getObject(0).getObject("alertRenderer");
Expand All @@ -893,7 +907,7 @@ public static void defaultAlertsCheck(final JsonObject initialData) throws Parsi
if (alertText != null && alertText.contains("This account has been terminated")) {
if (alertText.contains("violation") || alertText.contains("violating")
|| alertText.contains("infringement")) {
// possible error messages:
// Possible error messages:
// "This account has been terminated for a violation of YouTube's Terms of Service."
// "This account has been terminated due to multiple or severe violations of YouTube's policy prohibiting hate speech."
// "This account has been terminated due to multiple or severe violations of YouTube's policy prohibiting content designed to harass, bully or threaten."
Expand All @@ -913,7 +927,8 @@ public static void defaultAlertsCheck(final JsonObject initialData) throws Parsi
}

@Nonnull
public static List<MetaInfo> getMetaInfo(final JsonArray contents) throws ParsingException {
public static List<MetaInfo> getMetaInfo(@Nonnull final JsonArray contents)
throws ParsingException {
final List<MetaInfo> metaInfo = new ArrayList<>();
for (final Object content : contents) {
final JsonObject resultObject = (JsonObject) content;
Expand All @@ -939,7 +954,7 @@ public static List<MetaInfo> getMetaInfo(final JsonArray contents) throws Parsin
}

@Nonnull
private static MetaInfo getInfoPanelContent(final JsonObject infoPanelContentRenderer)
private static MetaInfo getInfoPanelContent(@Nonnull final JsonObject infoPanelContentRenderer)
throws ParsingException {
final MetaInfo metaInfo = new MetaInfo();
final StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -972,7 +987,7 @@ private static MetaInfo getInfoPanelContent(final JsonObject infoPanelContentRen
}

@Nonnull
private static MetaInfo getClarificationRendererContent(final JsonObject clarificationRenderer)
private static MetaInfo getClarificationRendererContent(@Nonnull final JsonObject clarificationRenderer)
throws ParsingException {
final MetaInfo metaInfo = new MetaInfo();

Expand Down Expand Up @@ -1009,7 +1024,7 @@ private static MetaInfo getClarificationRendererContent(final JsonObject clarifi
.has("secondarySource")) {
final String url = getUrlFromNavigationEndpoint(clarificationRenderer
.getObject("secondaryEndpoint"));
// ignore Google URLs, because those point to a Google search about "Covid-19"
// Ignore Google URLs, because those point to a Google search about "Covid-19"
if (url != null && !isGoogleURL(url)) {
try {
metaInfo.addUrl(new URL(url));
Expand Down Expand Up @@ -1059,7 +1074,8 @@ public static boolean isVerified(final JsonArray badges) {
return false;
}

public static String unescapeDocument(final String doc) {
@Nonnull
public static String unescapeDocument(@Nonnull final String doc) {
return doc
.replaceAll("\\\\x22", "\"")
.replaceAll("\\\\x7b", "{")
Expand Down

0 comments on commit 5f4f123

Please sign in to comment.