Skip to content

Commit dd4dd84

Browse files
committed
Get client version dynamically in YouTubeSearchExtractor
1 parent 583e9c1 commit dd4dd84

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/extractors/YoutubeSearchExtractor.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector;
1818
import org.schabi.newpipe.extractor.search.SearchExtractor;
1919
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
20+
import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector;
2021

2122
import java.io.IOException;
2223
import java.util.Collections;
@@ -111,14 +112,31 @@ public InfoItemsPage<InfoItem> getPage(String pageUrl) throws IOException, Extra
111112

112113
InfoItemsSearchCollector collector = getInfoItemSearchCollector();
113114
JsonArray ajaxJson;
115+
116+
Map<String, List<String>> headers = new HashMap<>();
117+
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
118+
114119
try {
115-
Map<String, List<String>> headers = new HashMap<>();
116-
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
117-
headers.put("X-YouTube-Client-Version", Collections.singletonList("2.20200221.03.00")); // TODO: Automatically get YouTube client version somehow
120+
// Use the hardcoded client version first to get JSON with a structure we know
121+
headers.put("X-YouTube-Client-Version",
122+
Collections.singletonList(YoutubeParsingHelper.HARDCODED_CLIENT_VERSION));
118123
final String response = getDownloader().get(pageUrl, headers, getExtractorLocalization()).responseBody();
124+
if (response.length() < 50) { // ensure to have a valid response
125+
throw new ParsingException("Could not parse json data for next streams");
126+
}
119127
ajaxJson = JsonParser.array().from(response);
120-
} catch (JsonParserException pe) {
121-
throw new ParsingException("Could not parse json data for next streams", pe);
128+
} catch (Exception e) {
129+
try {
130+
headers.put("X-YouTube-Client-Version",
131+
Collections.singletonList(YoutubeParsingHelper.getClientVersion(initialData, doc.toString())));
132+
final String response = getDownloader().get(pageUrl, headers, getExtractorLocalization()).responseBody();
133+
if (response.length() < 50) { // ensure to have a valid response
134+
throw new ParsingException("Could not parse json data for next streams");
135+
}
136+
ajaxJson = JsonParser.array().from(response);
137+
} catch (JsonParserException ignored) {
138+
throw new ParsingException("Could not parse json data for next streams", e);
139+
}
122140
}
123141

124142
JsonObject itemSectionRenderer = ajaxJson.getObject(1).getObject("response")

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,4 @@ public static String getClientVersion(JsonObject initialData, String html) throw
214214

215215
throw new ParsingException("Could not get client version");
216216
}
217-
218217
}

0 commit comments

Comments
 (0)