mirror of
https://github.com/TeamNewPipe/NewPipeExtractor.git
synced 2025-04-29 00:10:35 +05:30
[YouTube] Avoid crashing by letting exceptions bubble up
This commit is contained in:
parent
e9644e6216
commit
342bdbb852
@ -5,7 +5,6 @@ import com.grack.nanojson.JsonArray;
|
|||||||
import com.grack.nanojson.JsonObject;
|
import com.grack.nanojson.JsonObject;
|
||||||
import com.grack.nanojson.JsonParser;
|
import com.grack.nanojson.JsonParser;
|
||||||
import com.grack.nanojson.JsonParserException;
|
import com.grack.nanojson.JsonParserException;
|
||||||
|
|
||||||
import org.jsoup.Jsoup;
|
import org.jsoup.Jsoup;
|
||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.schabi.newpipe.extractor.downloader.Response;
|
import org.schabi.newpipe.extractor.downloader.Response;
|
||||||
@ -22,12 +21,7 @@ import java.net.URL;
|
|||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
|
import static org.schabi.newpipe.extractor.NewPipe.getDownloader;
|
||||||
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
|
import static org.schabi.newpipe.extractor.utils.Utils.HTTP;
|
||||||
@ -177,8 +171,7 @@ public class YoutubeParsingHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isHardcodedClientVersionValid() throws IOException {
|
public static boolean isHardcodedClientVersionValid() throws IOException, ExtractionException {
|
||||||
try {
|
|
||||||
final String url = "https://www.youtube.com/results?search_query=test&pbj=1";
|
final String url = "https://www.youtube.com/results?search_query=test&pbj=1";
|
||||||
|
|
||||||
Map<String, List<String>> headers = new HashMap<>();
|
Map<String, List<String>> headers = new HashMap<>();
|
||||||
@ -186,12 +179,8 @@ public class YoutubeParsingHelper {
|
|||||||
headers.put("X-YouTube-Client-Version",
|
headers.put("X-YouTube-Client-Version",
|
||||||
Collections.singletonList(HARDCODED_CLIENT_VERSION));
|
Collections.singletonList(HARDCODED_CLIENT_VERSION));
|
||||||
final String response = getDownloader().get(url, headers).responseBody();
|
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
|
* @return
|
||||||
* @throws ParsingException
|
* @throws ParsingException
|
||||||
*/
|
*/
|
||||||
public static String getClientVersion() throws ParsingException, IOException {
|
public static String getClientVersion() throws IOException, ExtractionException {
|
||||||
if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion;
|
if (clientVersion != null && !clientVersion.isEmpty()) return clientVersion;
|
||||||
|
|
||||||
if (isHardcodedClientVersionValid()) {
|
if (isHardcodedClientVersionValid()) {
|
||||||
@ -207,8 +196,6 @@ public class YoutubeParsingHelper {
|
|||||||
return clientVersion;
|
return clientVersion;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try extracting it from YouTube's website otherwise
|
|
||||||
try {
|
|
||||||
final String url = "https://www.youtube.com/results?search_query=test";
|
final String url = "https://www.youtube.com/results?search_query=test";
|
||||||
final String html = getDownloader().get(url).responseBody();
|
final String html = getDownloader().get(url).responseBody();
|
||||||
JsonObject initialData = getInitialData(html);
|
JsonObject initialData = getInitialData(html);
|
||||||
@ -262,7 +249,6 @@ public class YoutubeParsingHelper {
|
|||||||
clientVersion = shortClientVersion;
|
clientVersion = shortClientVersion;
|
||||||
return clientVersion;
|
return clientVersion;
|
||||||
}
|
}
|
||||||
} catch (Exception ignored) {}
|
|
||||||
|
|
||||||
throw new ParsingException("Could not get client version");
|
throw new ParsingException("Could not get client version");
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import org.junit.BeforeClass;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.schabi.newpipe.DownloaderTestImpl;
|
import org.schabi.newpipe.DownloaderTestImpl;
|
||||||
import org.schabi.newpipe.extractor.NewPipe;
|
import org.schabi.newpipe.extractor.NewPipe;
|
||||||
|
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
|
||||||
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
|
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeParsingHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -17,7 +18,7 @@ public class YoutubeParsingHelperTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsHardcodedClientVersionValid() throws IOException {
|
public void testIsHardcodedClientVersionValid() throws IOException, ExtractionException {
|
||||||
assertTrue("Hardcoded client version is not valid anymore",
|
assertTrue("Hardcoded client version is not valid anymore",
|
||||||
YoutubeParsingHelper.isHardcodedClientVersionValid());
|
YoutubeParsingHelper.isHardcodedClientVersionValid());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user