|
5 | 5 | import com.grack.nanojson.JsonObject; |
6 | 6 | import com.grack.nanojson.JsonParser; |
7 | 7 | import com.grack.nanojson.JsonParserException; |
8 | | - |
9 | 8 | import org.jsoup.Jsoup; |
10 | 9 | import org.jsoup.nodes.Document; |
11 | 10 | import org.schabi.newpipe.extractor.downloader.Response; |
|
22 | 21 | import java.net.URLDecoder; |
23 | 22 | import java.text.ParseException; |
24 | 23 | import java.text.SimpleDateFormat; |
25 | | -import java.util.Calendar; |
26 | | -import java.util.Collections; |
27 | | -import java.util.Date; |
28 | | -import java.util.HashMap; |
29 | | -import java.util.List; |
30 | | -import java.util.Map; |
| 24 | +import java.util.*; |
31 | 25 |
|
32 | 26 | import static org.schabi.newpipe.extractor.NewPipe.getDownloader; |
33 | 27 | import static org.schabi.newpipe.extractor.utils.Utils.HTTP; |
@@ -177,92 +171,84 @@ public static JsonObject getInitialData(String html) throws ParsingException { |
177 | 171 | } |
178 | 172 | } |
179 | 173 |
|
180 | | - public static boolean isHardcodedClientVersionValid() throws IOException { |
181 | | - try { |
182 | | - final String url = "https://www.youtube.com/results?search_query=test&pbj=1"; |
183 | | - |
184 | | - Map<String, List<String>> headers = new HashMap<>(); |
185 | | - headers.put("X-YouTube-Client-Name", Collections.singletonList("1")); |
186 | | - headers.put("X-YouTube-Client-Version", |
187 | | - Collections.singletonList(HARDCODED_CLIENT_VERSION)); |
188 | | - final String response = getDownloader().get(url, headers).responseBody(); |
189 | | - if (response.length() > 50) { // ensure to have a valid response |
190 | | - return true; |
191 | | - } |
192 | | - } catch (ReCaptchaException ignored) {} |
| 174 | + public static boolean isHardcodedClientVersionValid() throws IOException, ExtractionException { |
| 175 | + final String url = "https://www.youtube.com/results?search_query=test&pbj=1"; |
193 | 176 |
|
194 | | - return false; |
| 177 | + Map<String, List<String>> headers = new HashMap<>(); |
| 178 | + headers.put("X-YouTube-Client-Name", Collections.singletonList("1")); |
| 179 | + headers.put("X-YouTube-Client-Version", |
| 180 | + Collections.singletonList(HARDCODED_CLIENT_VERSION)); |
| 181 | + final String response = getDownloader().get(url, headers).responseBody(); |
| 182 | + |
| 183 | + return response.length() > 50; // ensure to have a valid response |
195 | 184 | } |
196 | 185 |
|
197 | 186 | /** |
198 | 187 | * Get the client version from a page |
199 | 188 | * @return |
200 | 189 | * @throws ParsingException |
201 | 190 | */ |
202 | | - public static String getClientVersion() throws ParsingException, IOException { |
| 191 | + public static String getClientVersion() throws IOException, ExtractionException { |
203 | 192 | if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion; |
204 | 193 |
|
205 | 194 | if (isHardcodedClientVersionValid()) { |
206 | 195 | clientVersion = HARDCODED_CLIENT_VERSION; |
207 | 196 | return clientVersion; |
208 | 197 | } |
209 | 198 |
|
210 | | - // Try extracting it from YouTube's website otherwise |
211 | | - try { |
212 | | - final String url = "https://www.youtube.com/results?search_query=test"; |
213 | | - final String html = getDownloader().get(url).responseBody(); |
214 | | - JsonObject initialData = getInitialData(html); |
215 | | - JsonArray serviceTrackingParams = initialData.getObject("responseContext").getArray("serviceTrackingParams"); |
216 | | - String shortClientVersion = null; |
217 | | - |
218 | | - // try to get version from initial data first |
219 | | - for (Object service : serviceTrackingParams) { |
220 | | - JsonObject s = (JsonObject) service; |
221 | | - if (s.getString("service").equals("CSI")) { |
222 | | - JsonArray params = s.getArray("params"); |
223 | | - for (Object param : params) { |
224 | | - JsonObject p = (JsonObject) param; |
225 | | - String key = p.getString("key"); |
226 | | - if (key != null && key.equals("cver")) { |
227 | | - clientVersion = p.getString("value"); |
228 | | - return clientVersion; |
229 | | - } |
| 199 | + final String url = "https://www.youtube.com/results?search_query=test"; |
| 200 | + final String html = getDownloader().get(url).responseBody(); |
| 201 | + JsonObject initialData = getInitialData(html); |
| 202 | + JsonArray serviceTrackingParams = initialData.getObject("responseContext").getArray("serviceTrackingParams"); |
| 203 | + String shortClientVersion = null; |
| 204 | + |
| 205 | + // try to get version from initial data first |
| 206 | + for (Object service : serviceTrackingParams) { |
| 207 | + JsonObject s = (JsonObject) service; |
| 208 | + if (s.getString("service").equals("CSI")) { |
| 209 | + JsonArray params = s.getArray("params"); |
| 210 | + for (Object param : params) { |
| 211 | + JsonObject p = (JsonObject) param; |
| 212 | + String key = p.getString("key"); |
| 213 | + if (key != null && key.equals("cver")) { |
| 214 | + clientVersion = p.getString("value"); |
| 215 | + return clientVersion; |
230 | 216 | } |
231 | | - } else if (s.getString("service").equals("ECATCHER")) { |
232 | | - // fallback to get a shortened client version which does not contain the last two digits |
233 | | - JsonArray params = s.getArray("params"); |
234 | | - for (Object param : params) { |
235 | | - JsonObject p = (JsonObject) param; |
236 | | - String key = p.getString("key"); |
237 | | - if (key != null && key.equals("client.version")) { |
238 | | - shortClientVersion = p.getString("value"); |
239 | | - } |
| 217 | + } |
| 218 | + } else if (s.getString("service").equals("ECATCHER")) { |
| 219 | + // fallback to get a shortened client version which does not contain the last two digits |
| 220 | + JsonArray params = s.getArray("params"); |
| 221 | + for (Object param : params) { |
| 222 | + JsonObject p = (JsonObject) param; |
| 223 | + String key = p.getString("key"); |
| 224 | + if (key != null && key.equals("client.version")) { |
| 225 | + shortClientVersion = p.getString("value"); |
240 | 226 | } |
241 | 227 | } |
242 | 228 | } |
| 229 | + } |
243 | 230 |
|
244 | | - String contextClientVersion; |
245 | | - String[] patterns = { |
246 | | - "INNERTUBE_CONTEXT_CLIENT_VERSION\":\"([0-9\\.]+?)\"", |
247 | | - "innertube_context_client_version\":\"([0-9\\.]+?)\"", |
248 | | - "client.version=([0-9\\.]+)" |
249 | | - }; |
250 | | - for (String pattern : patterns) { |
251 | | - try { |
252 | | - contextClientVersion = Parser.matchGroup1(pattern, html); |
253 | | - if (contextClientVersion != null && !contextClientVersion.isEmpty()) { |
254 | | - clientVersion = contextClientVersion; |
255 | | - return clientVersion; |
256 | | - } |
257 | | - } catch (Exception ignored) { |
| 231 | + String contextClientVersion; |
| 232 | + String[] patterns = { |
| 233 | + "INNERTUBE_CONTEXT_CLIENT_VERSION\":\"([0-9\\.]+?)\"", |
| 234 | + "innertube_context_client_version\":\"([0-9\\.]+?)\"", |
| 235 | + "client.version=([0-9\\.]+)" |
| 236 | + }; |
| 237 | + for (String pattern : patterns) { |
| 238 | + try { |
| 239 | + contextClientVersion = Parser.matchGroup1(pattern, html); |
| 240 | + if (contextClientVersion != null && !contextClientVersion.isEmpty()) { |
| 241 | + clientVersion = contextClientVersion; |
| 242 | + return clientVersion; |
258 | 243 | } |
| 244 | + } catch (Exception ignored) { |
259 | 245 | } |
| 246 | + } |
260 | 247 |
|
261 | | - if (shortClientVersion != null) { |
262 | | - clientVersion = shortClientVersion; |
263 | | - return clientVersion; |
264 | | - } |
265 | | - } catch (Exception ignored) {} |
| 248 | + if (shortClientVersion != null) { |
| 249 | + clientVersion = shortClientVersion; |
| 250 | + return clientVersion; |
| 251 | + } |
266 | 252 |
|
267 | 253 | throw new ParsingException("Could not get client version"); |
268 | 254 | } |
|
0 commit comments