[YouTube] Avoid crashing by letting exceptions bubble up

This commit is contained in:
Mauricio Colli 2020-02-29 18:42:43 -03:00 committed by TobiGr
parent e9644e6216
commit 342bdbb852
2 changed files with 58 additions and 71 deletions

View File

@ -5,7 +5,6 @@ import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.schabi.newpipe.extractor.downloader.Response;
@ -22,12 +21,7 @@ import java.net.URL;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
@ -177,8 +171,7 @@ public class YoutubeParsingHelper {
}
}
public static boolean isHardcodedClientVersionValid() throws IOException {
try {
public static boolean isHardcodedClientVersionValid() throws IOException, ExtractionException {
final String url = "https://www.youtube.com/results?search_query=test&pbj=1";
Map<String, List<String>> headers = new HashMap<>();
@ -186,12 +179,8 @@ public class YoutubeParsingHelper {
headers.put("X-YouTube-Client-Version",
Collections.singletonList(HARDCODED_CLIENT_VERSION));
final String response = getDownloader().get(url, headers).responseBody();
if (response.length() > 50) { // ensure to have a valid response
return true;
}
} catch (ReCaptchaException ignored) {}
return false;
return response.length() > 50; // ensure to have a valid response
}
/**
@ -199,7 +188,7 @@ public class YoutubeParsingHelper {
* @return
* @throws ParsingException
*/
public static String getClientVersion() throws ParsingException, IOException {
public static String getClientVersion() throws IOException, ExtractionException {
if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion;
if (isHardcodedClientVersionValid()) {
@ -207,8 +196,6 @@ public class YoutubeParsingHelper {
return clientVersion;
}
// Try extracting it from YouTube's website otherwise
try {
final String url = "https://www.youtube.com/results?search_query=test";
final String html = getDownloader().get(url).responseBody();
JsonObject initialData = getInitialData(html);
@ -262,7 +249,6 @@ public class YoutubeParsingHelper {
clientVersion = shortClientVersion;
return clientVersion;
}
} catch (Exception ignored) {}
throw new ParsingException("Could not get client version");
}

View File

@ -4,6 +4,7 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
import java.io.IOException;
@ -17,7 +18,7 @@ public class YoutubeParsingHelperTest {
}
@Test
public void testIsHardcodedClientVersionValid() throws IOException {
public void testIsHardcodedClientVersionValid() throws IOException, ExtractionException {
assertTrue("Hardcoded client version is not valid anymore",
YoutubeParsingHelper.isHardcodedClientVersionValid());
}