mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-29 00:10:35 +05:30
Make some vars final and add annotations to methods
This commit is contained in:
parent
bc6de14952
commit
41a8ed625d
@ -1,5 +1,6 @@
|
|||||||
package org.schabi.newpipe.extractor.services.youtube;
|
package org.schabi.newpipe.extractor.services.youtube;
|
||||||
|
|
||||||
|
import com.grack.nanojson.*;
|
||||||
import com.grack.nanojson.JsonArray;
|
import com.grack.nanojson.JsonArray;
|
||||||
import com.grack.nanojson.JsonObject;
|
import com.grack.nanojson.JsonObject;
|
||||||
import com.grack.nanojson.JsonParser;
|
import com.grack.nanojson.JsonParser;
|
||||||
@ -8,6 +9,7 @@ import com.grack.nanojson.JsonWriter;
|
|||||||
|
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
|
import org.schabi.newpipe.extractor.MetaInfo;
|
||||||
import org.schabi.newpipe.extractor.Page;
|
import org.schabi.newpipe.extractor.Page;
|
||||||
import org.schabi.newpipe.extractor.downloader.Response;
|
import org.schabi.newpipe.extractor.downloader.Response;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
|
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
|
||||||
@ -15,9 +17,12 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
|||||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||||
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
||||||
import org.schabi.newpipe.extractor.localization.Localization;
|
import org.schabi.newpipe.extractor.localization.Localization;
|
||||||
|
import org.schabi.newpipe.extractor.stream.Description;
|
||||||
import org.schabi.newpipe.extractor.utils.Parser;
|
import org.schabi.newpipe.extractor.utils.Parser;
|
||||||
import org.schabi.newpipe.extractor.utils.Utils;
|
import org.schabi.newpipe.extractor.utils.Utils;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
@ -28,13 +33,11 @@ import java.time.LocalDate;
|
|||||||
import java.time.OffsetDateTime;
|
import java.time.OffsetDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.format.DateTimeParseException;
|
import java.time.format.DateTimeParseException;
|
||||||
import java.util.Collections;
|
import java.util.*;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
|
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
|
||||||
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
|
import static org.schabi.newpipe.extractor.utils.JsonUtils.EMPTY_STRING;
|
||||||
|
import static org.schabi.newpipe.extractor.utils.Utils.*;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
|
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
|
import static org.schabi.newpipe.extractor.utils.Utils.HTTPS;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
|
||||||
@ -87,27 +90,24 @@ public class YoutubeParsingHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return document;
|
public static boolean isYoutubeURL(final URL url) {
|
||||||
}
|
final String host = url.getHost();
|
||||||
|
|
||||||
public static boolean isYoutubeURL(URL url) {
|
|
||||||
String host = url.getHost();
|
|
||||||
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
|
return host.equalsIgnoreCase("youtube.com") || host.equalsIgnoreCase("www.youtube.com")
|
||||||
|| host.equalsIgnoreCase("m.youtube.com") || host.equalsIgnoreCase("music.youtube.com");
|
|| host.equalsIgnoreCase("m.youtube.com") || host.equalsIgnoreCase("music.youtube.com");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isYoutubeServiceURL(URL url) {
|
public static boolean isYoutubeServiceURL(final URL url) {
|
||||||
String host = url.getHost();
|
final String host = url.getHost();
|
||||||
return host.equalsIgnoreCase("www.youtube-nocookie.com") || host.equalsIgnoreCase("youtu.be");
|
return host.equalsIgnoreCase("www.youtube-nocookie.com") || host.equalsIgnoreCase("youtu.be");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isHooktubeURL(URL url) {
|
public static boolean isHooktubeURL(final URL url) {
|
||||||
String host = url.getHost();
|
final String host = url.getHost();
|
||||||
return host.equalsIgnoreCase("hooktube.com");
|
return host.equalsIgnoreCase("hooktube.com");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isInvidioURL(URL url) {
|
public static boolean isInvidioURL(final URL url) {
|
||||||
String host = url.getHost();
|
final String host = url.getHost();
|
||||||
return host.equalsIgnoreCase("invidio.us")
|
return host.equalsIgnoreCase("invidio.us")
|
||||||
|| host.equalsIgnoreCase("dev.invidio.us")
|
|| host.equalsIgnoreCase("dev.invidio.us")
|
||||||
|| host.equalsIgnoreCase("www.invidio.us")
|
|| host.equalsIgnoreCase("www.invidio.us")
|
||||||
@ -181,7 +181,7 @@ public class YoutubeParsingHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static OffsetDateTime parseDateFrom(String textualUploadDate) throws ParsingException {
|
public static OffsetDateTime parseDateFrom(final String textualUploadDate) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
return OffsetDateTime.parse(textualUploadDate);
|
return OffsetDateTime.parse(textualUploadDate);
|
||||||
} catch (DateTimeParseException e) {
|
} catch (DateTimeParseException e) {
|
||||||
@ -244,7 +244,7 @@ public class YoutubeParsingHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static JsonObject getInitialData(String html) throws ParsingException {
|
public static JsonObject getInitialData(final String html) throws ParsingException {
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
final String initialData = Parser.matchGroup1("window\\[\"ytInitialData\"\\]\\s*=\\s*(\\{.*?\\});", html);
|
final String initialData = Parser.matchGroup1("window\\[\"ytInitialData\"\\]\\s*=\\s*(\\{.*?\\});", html);
|
||||||
@ -261,10 +261,9 @@ public class YoutubeParsingHelper {
|
|||||||
public static boolean isHardcodedClientVersionValid() throws IOException, ExtractionException {
|
public static boolean isHardcodedClientVersionValid() throws IOException, ExtractionException {
|
||||||
final String url = "https://www.youtube.com/results?search_query=test&pbj=1";
|
final String url = "https://www.youtube.com/results?search_query=test&pbj=1";
|
||||||
|
|
||||||
Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
|
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
|
||||||
headers.put("X-YouTube-Client-Version",
|
headers.put("X-YouTube-Client-Version", Collections.singletonList(HARDCODED_CLIENT_VERSION));
|
||||||
Collections.singletonList(HARDCODED_CLIENT_VERSION));
|
|
||||||
final String response = getDownloader().get(url, headers).responseBody();
|
final String response = getDownloader().get(url, headers).responseBody();
|
||||||
|
|
||||||
return response.length() > 50; // ensure to have a valid response
|
return response.length() > 50; // ensure to have a valid response
|
||||||
@ -387,14 +386,14 @@ public class YoutubeParsingHelper {
|
|||||||
.end().done().getBytes("UTF-8");
|
.end().done().getBytes("UTF-8");
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
|
|
||||||
Map<String, List<String>> headers = new HashMap<>();
|
final Map<String, List<String>> headers = new HashMap<>();
|
||||||
headers.put("X-YouTube-Client-Name", Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEYS[1]));
|
headers.put("X-YouTube-Client-Name", Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEYS[1]));
|
||||||
headers.put("X-YouTube-Client-Version", Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEYS[2]));
|
headers.put("X-YouTube-Client-Version", Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEYS[2]));
|
||||||
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
|
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
|
||||||
headers.put("Referer", Collections.singletonList("music.youtube.com"));
|
headers.put("Referer", Collections.singletonList("music.youtube.com"));
|
||||||
headers.put("Content-Type", Collections.singletonList("application/json"));
|
headers.put("Content-Type", Collections.singletonList("application/json"));
|
||||||
|
|
||||||
String response = getDownloader().post(url, headers, json).responseBody();
|
final String response = getDownloader().post(url, headers, json).responseBody();
|
||||||
|
|
||||||
return response.length() > 50; // ensure to have a valid response
|
return response.length() > 50; // ensure to have a valid response
|
||||||
}
|
}
|
||||||
@ -429,6 +428,7 @@ public class YoutubeParsingHelper {
|
|||||||
return youtubeMusicKeys = new String[]{key, clientName, clientVersion};
|
return youtubeMusicKeys = new String[]{key, clientName, clientVersion};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint) throws ParsingException {
|
public static String getUrlFromNavigationEndpoint(JsonObject navigationEndpoint) throws ParsingException {
|
||||||
if (navigationEndpoint.has("urlEndpoint")) {
|
if (navigationEndpoint.has("urlEndpoint")) {
|
||||||
String internUrl = navigationEndpoint.getObject("urlEndpoint").getString("url");
|
String internUrl = navigationEndpoint.getObject("urlEndpoint").getString("url");
|
||||||
@ -490,6 +490,7 @@ public class YoutubeParsingHelper {
|
|||||||
* @param html whether to return HTML, by parsing the navigationEndpoint
|
* @param html whether to return HTML, by parsing the navigationEndpoint
|
||||||
* @return text in the JSON object or {@code null}
|
* @return text in the JSON object or {@code null}
|
||||||
*/
|
*/
|
||||||
|
@Nullable
|
||||||
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
|
public static String getTextFromObject(JsonObject textObject, boolean html) throws ParsingException {
|
||||||
if (isNullOrEmpty(textObject)) return null;
|
if (isNullOrEmpty(textObject)) return null;
|
||||||
|
|
||||||
@ -497,8 +498,8 @@ public class YoutubeParsingHelper {
|
|||||||
|
|
||||||
if (textObject.getArray("runs").isEmpty()) return null;
|
if (textObject.getArray("runs").isEmpty()) return null;
|
||||||
|
|
||||||
StringBuilder textBuilder = new StringBuilder();
|
final StringBuilder textBuilder = new StringBuilder();
|
||||||
for (Object textPart : textObject.getArray("runs")) {
|
for (final Object textPart : textObject.getArray("runs")) {
|
||||||
String text = ((JsonObject) textPart).getString("text");
|
String text = ((JsonObject) textPart).getString("text");
|
||||||
if (html && ((JsonObject) textPart).has("navigationEndpoint")) {
|
if (html && ((JsonObject) textPart).has("navigationEndpoint")) {
|
||||||
String url = getUrlFromNavigationEndpoint(((JsonObject) textPart).getObject("navigationEndpoint"));
|
String url = getUrlFromNavigationEndpoint(((JsonObject) textPart).getObject("navigationEndpoint"));
|
||||||
@ -520,6 +521,7 @@ public class YoutubeParsingHelper {
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
public static String getTextFromObject(JsonObject textObject) throws ParsingException {
|
public static String getTextFromObject(JsonObject textObject) throws ParsingException {
|
||||||
return getTextFromObject(textObject, false);
|
return getTextFromObject(textObject, false);
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,8 @@ import org.schabi.newpipe.extractor.search.SearchExtractor;
|
|||||||
import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
|
import org.schabi.newpipe.extractor.services.DefaultSearchExtractorTest;
|
||||||
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
|
||||||
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
|
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user