Skip to content

Commit 342bdbb

Browse files
mauriciocolliTobiGr
authored andcommitted
[YouTube] Avoid crashing by letting exceptions bubble up
1 parent e9644e6 commit 342bdbb

2 files changed

Lines changed: 58 additions & 71 deletions

File tree

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

Lines changed: 56 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.grack.nanojson.JsonObject;
66
import com.grack.nanojson.JsonParser;
77
import com.grack.nanojson.JsonParserException;
8-
98
import org.jsoup.Jsoup;
109
import org.jsoup.nodes.Document;
1110
import org.schabi.newpipe.extractor.downloader.Response;
@@ -22,12 +21,7 @@
2221
import java.net.URLDecoder;
2322
import java.text.ParseException;
2423
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.*;
3125

3226
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
3327
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
@@ -177,92 +171,84 @@ public static JsonObject getInitialData(String html) throws ParsingException {
177171
}
178172
}
179173

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";
193176

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
195184
}
196185

197186
/**
198187
* Get the client version from a page
199188
* @return
200189
* @throws ParsingException
201190
*/
202-
public static String getClientVersion() throws ParsingException, IOException {
191+
public static String getClientVersion() throws IOException, ExtractionException {
203192
if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion;
204193

205194
if (isHardcodedClientVersionValid()) {
206195
clientVersion = HARDCODED_CLIENT_VERSION;
207196
return clientVersion;
208197
}
209198

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;
230216
}
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");
240226
}
241227
}
242228
}
229+
}
243230

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;
258243
}
244+
} catch (Exception ignored) {
259245
}
246+
}
260247

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+
}
266252

267253
throw new ParsingException("Could not get client version");
268254
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelperTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Test;
55
import org.schabi.newpipe.DownloaderTestImpl;
66
import org.schabi.newpipe.extractor.NewPipe;
7+
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
78
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
89

910
import java.io.IOException;
@@ -17,7 +18,7 @@ public static void setUp() {
1718
}
1819

1920
@Test
20-
public void testIsHardcodedClientVersionValid() throws IOException {
21+
public void testIsHardcodedClientVersionValid() throws IOException, ExtractionException {
2122
assertTrue("Hardcoded client version is not valid anymore",
2223
YoutubeParsingHelper.isHardcodedClientVersionValid());
2324
}

0 commit comments

Comments
 (0)