From a5ac528c02c15879ac354e091d3d99c64a5fdaa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Mauduit?= Date: Wed, 7 Dec 2016 19:22:27 +0100 Subject: [PATCH] Add reCaptchaException --- .../java/org/schabi/newpipe/Downloader.java | 18 ++++++++++--- .../newpipe/extractor/DashMpdParser.java | 5 +++- .../schabi/newpipe/extractor/Downloader.java | 8 +++--- .../exceptions/reCaptchaException.java | 27 +++++++++++++++++++ .../youtube/YoutubeStreamExtractor.java | 5 +++- 5 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/extractor/exceptions/reCaptchaException.java diff --git a/app/src/main/java/org/schabi/newpipe/Downloader.java b/app/src/main/java/org/schabi/newpipe/Downloader.java index 3c701fde3..748d6bd4d 100644 --- a/app/src/main/java/org/schabi/newpipe/Downloader.java +++ b/app/src/main/java/org/schabi/newpipe/Downloader.java @@ -1,5 +1,7 @@ package org.schabi.newpipe; +import org.schabi.newpipe.extractor.exceptions.reCaptchaException; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -65,7 +67,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { * @param siteUrl the URL of the text file to return the contents of * @param language the language (usually a 2-character code) to set as the preferred language * @return the contents of the specified text file*/ - public String download(String siteUrl, String language) throws IOException { + public String download(String siteUrl, String language) throws IOException, reCaptchaException { Map requestProperties = new HashMap<>(); requestProperties.put("Accept-Language", language); return download(siteUrl, requestProperties); @@ -78,7 +80,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { * @param customProperties set request header properties * @return the contents of the specified text file * @throws IOException*/ - public String download(String siteUrl, Map customProperties) throws IOException { + public String download(String siteUrl, Map customProperties) throws IOException, reCaptchaException { URL url = new URL(siteUrl); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); Iterator it = customProperties.entrySet().iterator(); @@ -90,7 +92,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { } /**Common functionality between download(String url) and download(String url, String language)*/ - private static String dl(HttpsURLConnection con) throws IOException { + private static String dl(HttpsURLConnection con) throws IOException, reCaptchaException { StringBuilder response = new StringBuilder(); BufferedReader in = null; @@ -113,6 +115,14 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { throw new IOException("unknown host or no network", uhe); //Toast.makeText(getActivity(), uhe.getMessage(), Toast.LENGTH_LONG).show(); } catch(Exception e) { + /* + * HTTP 429 == Too Many Request + * Receive from Youtube.com = ReCaptcha challenge request + * See : https://github.com/rg3/youtube-dl/issues/5138 + */ + if (con.getResponseCode() == 429) { + throw new reCaptchaException("reCaptcha Challenge requested"); + } throw new IOException(e); } finally { if(in != null) { @@ -127,7 +137,7 @@ public class Downloader implements org.schabi.newpipe.extractor.Downloader { * Primarily intended for downloading web pages. * @param siteUrl the URL of the text file to download * @return the contents of the specified text file*/ - public String download(String siteUrl) throws IOException { + public String download(String siteUrl) throws IOException, reCaptchaException { URL url = new URL(siteUrl); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); //HttpsURLConnection con = NetCipher.getHttpsURLConnection(url); diff --git a/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java b/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java index db26e3871..7dcbc16a8 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/DashMpdParser.java @@ -3,6 +3,7 @@ package org.schabi.newpipe.extractor; import android.util.Xml; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.exceptions.reCaptchaException; import org.schabi.newpipe.extractor.stream_info.AudioStream; import org.xmlpull.v1.XmlPullParser; @@ -43,13 +44,15 @@ public class DashMpdParser { } public static List getAudioStreams(String dashManifestUrl) - throws DashMpdParsingException { + throws DashMpdParsingException, reCaptchaException { String dashDoc; Downloader downloader = NewPipe.getDownloader(); try { dashDoc = downloader.download(dashManifestUrl); } catch(IOException ioe) { throw new DashMpdParsingException("Could not get dash mpd: " + dashManifestUrl, ioe); + } catch (reCaptchaException e) { + throw new reCaptchaException("reCaptcha Challenge needed"); } Vector audioStreams = new Vector<>(); try { diff --git a/app/src/main/java/org/schabi/newpipe/extractor/Downloader.java b/app/src/main/java/org/schabi/newpipe/extractor/Downloader.java index 14475dba7..f25375113 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/Downloader.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/Downloader.java @@ -1,5 +1,7 @@ package org.schabi.newpipe.extractor; +import org.schabi.newpipe.extractor.exceptions.reCaptchaException; + import java.io.IOException; import java.util.Map; @@ -31,7 +33,7 @@ public interface Downloader { * @param language the language (usually a 2-character code) to set as the preferred language * @return the contents of the specified text file * @throws IOException*/ - String download(String siteUrl, String language) throws IOException; + String download(String siteUrl, String language) throws IOException, reCaptchaException; /**Download the text file at the supplied URL as in download(String), * but set the HTTP header field "Accept-Language" to the supplied string. @@ -39,12 +41,12 @@ public interface Downloader { * @param customProperties set request header properties * @return the contents of the specified text file * @throws IOException*/ - String download(String siteUrl, Map customProperties) throws IOException; + String download(String siteUrl, Map customProperties) throws IOException, reCaptchaException; /**Download (via HTTP) the text file located at the supplied URL, and return its contents. * Primarily intended for downloading web pages. * @param siteUrl the URL of the text file to download * @return the contents of the specified text file * @throws IOException*/ - String download(String siteUrl) throws IOException; + String download(String siteUrl) throws IOException, reCaptchaException; } diff --git a/app/src/main/java/org/schabi/newpipe/extractor/exceptions/reCaptchaException.java b/app/src/main/java/org/schabi/newpipe/extractor/exceptions/reCaptchaException.java new file mode 100644 index 000000000..a9107f15e --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/extractor/exceptions/reCaptchaException.java @@ -0,0 +1,27 @@ +package org.schabi.newpipe.extractor.exceptions; + +/** + * Created by beneth on 07.12.16. + * + * Copyright (C) Christian Schabesberger 2016 + * reCaptchaException.java is part of NewPipe. + * + * NewPipe is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * NewPipe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with NewPipe. If not, see . + */ + +public class reCaptchaException extends ExtractionException { + public reCaptchaException(String message) { + super(message); + } +} diff --git a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java index e1cbdd1a6..aa07bf96f 100644 --- a/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeStreamExtractor.java @@ -11,6 +11,7 @@ import org.mozilla.javascript.ScriptableObject; import org.schabi.newpipe.extractor.AbstractStreamInfo; import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.exceptions.reCaptchaException; import org.schabi.newpipe.extractor.stream_info.AudioStream; import org.schabi.newpipe.extractor.Downloader; import org.schabi.newpipe.extractor.NewPipe; @@ -280,7 +281,7 @@ public class YoutubeStreamExtractor extends StreamExtractor { } } - private String getPlayerUrlFromRestrictedVideo(String pageUrl) throws ParsingException { + private String getPlayerUrlFromRestrictedVideo(String pageUrl) throws ParsingException, reCaptchaException { try { Downloader downloader = NewPipe.getDownloader(); String playerUrl = ""; @@ -302,6 +303,8 @@ public class YoutubeStreamExtractor extends StreamExtractor { } catch (IOException e) { throw new ParsingException( "Could load decryption code form restricted video for the Youtube service.", e); + } catch (reCaptchaException e) { + throw new reCaptchaException("reCaptcha Challenge requested"); } }