Only use fallback methods for clientVersion when clientVersion hasn't been successfully extracted yet

This commit is contained in:
wb9688 2020-07-26 13:14:25 +02:00
parent f345f667e2
commit 667dce034c

View File

@ -216,7 +216,7 @@ public class YoutubeParsingHelper {
return response.length() > 50; // ensure to have a valid response return response.length() > 50; // ensure to have a valid response
} }
private static void getClientVersionAndKey() throws IOException, ExtractionException { private static void extractClientVersionAndKey() throws IOException, ExtractionException {
final String url = "https://www.youtube.com/results?search_query=test"; final String url = "https://www.youtube.com/results?search_query=test";
final String html = getDownloader().get(url).responseBody(); final String html = getDownloader().get(url).responseBody();
final JsonObject initialData = getInitialData(html); final JsonObject initialData = getInitialData(html);
@ -259,11 +259,12 @@ public class YoutubeParsingHelper {
contextClientVersion = Parser.matchGroup1(pattern, html); contextClientVersion = Parser.matchGroup1(pattern, html);
if (!isNullOrEmpty(contextClientVersion)) { if (!isNullOrEmpty(contextClientVersion)) {
clientVersion = contextClientVersion; clientVersion = contextClientVersion;
break;
} }
} catch (Parser.RegexException ignored) { } } catch (Parser.RegexException ignored) { }
} }
if (shortClientVersion != null) { if (!isNullOrEmpty(clientVersion) && !isNullOrEmpty(shortClientVersion)) {
clientVersion = shortClientVersion; clientVersion = shortClientVersion;
} }
@ -283,7 +284,7 @@ public class YoutubeParsingHelper {
if (!isNullOrEmpty(clientVersion)) return clientVersion; if (!isNullOrEmpty(clientVersion)) return clientVersion;
if (isHardcodedClientVersionValid()) return clientVersion = HARDCODED_CLIENT_VERSION; if (isHardcodedClientVersionValid()) return clientVersion = HARDCODED_CLIENT_VERSION;
getClientVersionAndKey(); extractClientVersionAndKey();
if (isNullOrEmpty(key)) throw new ParsingException("Could not extract client version"); if (isNullOrEmpty(key)) throw new ParsingException("Could not extract client version");
return clientVersion; return clientVersion;
} }
@ -294,7 +295,7 @@ public class YoutubeParsingHelper {
public static String getKey() throws IOException, ExtractionException { public static String getKey() throws IOException, ExtractionException {
if (!isNullOrEmpty(key)) return key; if (!isNullOrEmpty(key)) return key;
getClientVersionAndKey(); extractClientVersionAndKey();
if (isNullOrEmpty(key)) throw new ParsingException("Could not extract key"); if (isNullOrEmpty(key)) throw new ParsingException("Could not extract key");
return key; return key;
} }