Check if hard-coded client version is valid in separate function

This commit is contained in:
wb9688 2020-02-28 16:35:24 +01:00
parent 7898ba230d
commit b89b6b1db2

View file

@ -175,15 +175,7 @@ public class YoutubeParsingHelper {
} }
} }
/** public static boolean isHardcodedClientVersionValid() {
* Get the client version from a page
* @return
* @throws ParsingException
*/
public static String getClientVersion() throws ParsingException {
if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion;
// Test if hard-coded client version is valid
try { try {
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";
@ -193,11 +185,26 @@ public class YoutubeParsingHelper {
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();
if (response.length() > 50) { // ensure to have a valid response if (response.length() > 50) { // ensure to have a valid response
clientVersion = HARDCODED_CLIENT_VERSION; return true;
return clientVersion;
} }
} catch (Exception ignored) {} } catch (Exception ignored) {}
return false;
}
/**
* Get the client version from a page
* @return
* @throws ParsingException
*/
public static String getClientVersion() throws ParsingException {
if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion;
if (isHardcodedClientVersionValid()) {
clientVersion = HARDCODED_CLIENT_VERSION;
return clientVersion;
}
// Try extracting it from YouTube's website otherwise // Try extracting it from YouTube's website otherwise
try { try {
final String url = "https://www.youtube.com/results?search_query=test"; final String url = "https://www.youtube.com/results?search_query=test";