|
17 | 17 | import org.schabi.newpipe.extractor.search.InfoItemsSearchCollector; |
18 | 18 | import org.schabi.newpipe.extractor.search.SearchExtractor; |
19 | 19 | import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper; |
| 20 | +import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; |
20 | 21 |
|
21 | 22 | import java.io.IOException; |
22 | 23 | import java.util.Collections; |
@@ -111,14 +112,31 @@ public InfoItemsPage<InfoItem> getPage(String pageUrl) throws IOException, Extra |
111 | 112 |
|
112 | 113 | InfoItemsSearchCollector collector = getInfoItemSearchCollector(); |
113 | 114 | JsonArray ajaxJson; |
| 115 | + |
| 116 | + Map<String, List<String>> headers = new HashMap<>(); |
| 117 | + headers.put("X-YouTube-Client-Name", Collections.singletonList("1")); |
| 118 | + |
114 | 119 | 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)); |
118 | 123 | 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 | + } |
119 | 127 | 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 | + } |
122 | 140 | } |
123 | 141 |
|
124 | 142 | JsonObject itemSectionRenderer = ajaxJson.getObject(1).getObject("response") |
|
0 commit comments