Search in sources :

Example 1 with ReCaptchaException

use of org.schabi.newpipe.extractor.exceptions.ReCaptchaException in project NewPipe by TeamNewPipe.

the class Downloader method dl.

/**Common functionality between download(String url) and download(String url, String language)*/
private static String dl(HttpsURLConnection con) throws IOException, ReCaptchaException {
    StringBuilder response = new StringBuilder();
    BufferedReader in = null;
    try {
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        if (getCookies().length() > 0) {
            con.setRequestProperty("Cookie", getCookies());
        }
        in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
    } catch (UnknownHostException uhe) {
        //thrown when there's no internet connection
        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) {
            in.close();
        }
    }
    return response.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException)

Example 2 with ReCaptchaException

use of org.schabi.newpipe.extractor.exceptions.ReCaptchaException in project NewPipe by TeamNewPipe.

the class Downloader method getBody.

private ResponseBody getBody(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
    final Request.Builder requestBuilder = new Request.Builder().method("GET", null).url(siteUrl).addHeader("User-Agent", USER_AGENT);
    for (Map.Entry<String, String> header : customProperties.entrySet()) {
        requestBuilder.addHeader(header.getKey(), header.getValue());
    }
    if (!TextUtils.isEmpty(mCookies)) {
        requestBuilder.addHeader("Cookie", mCookies);
    }
    final Request request = requestBuilder.build();
    final Response response = client.newCall(request).execute();
    final ResponseBody body = response.body();
    if (response.code() == 429) {
        throw new ReCaptchaException("reCaptcha Challenge requested");
    }
    if (body == null) {
        response.close();
        return null;
    }
    return body;
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) HashMap(java.util.HashMap) Map(java.util.Map) ResponseBody(okhttp3.ResponseBody) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException)

Example 3 with ReCaptchaException

use of org.schabi.newpipe.extractor.exceptions.ReCaptchaException in project NewPipe by TeamNewPipe.

the class ExtractorHelper method handleGeneralException.

/**
 * A simple and general error handler that show a Toast for known exceptions, and for others, opens the report error activity with the (optional) error message.
 */
public static void handleGeneralException(Context context, int serviceId, String url, Throwable exception, UserAction userAction, String optionalErrorMessage) {
    final Handler handler = new Handler(context.getMainLooper());
    handler.post(() -> {
        if (exception instanceof ReCaptchaException) {
            Toast.makeText(context, R.string.recaptcha_request_toast, Toast.LENGTH_LONG).show();
            // Starting ReCaptcha Challenge Activity
            Intent intent = new Intent(context, ReCaptchaActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } else if (exception instanceof IOException) {
            Toast.makeText(context, R.string.network_error, Toast.LENGTH_LONG).show();
        } else if (exception instanceof YoutubeStreamExtractor.GemaException) {
            Toast.makeText(context, R.string.blocked_by_gema, Toast.LENGTH_LONG).show();
        } else if (exception instanceof ContentNotAvailableException) {
            Toast.makeText(context, R.string.content_not_available, Toast.LENGTH_LONG).show();
        } else {
            int errorId = exception instanceof YoutubeStreamExtractor.DecryptException ? R.string.youtube_signature_decryption_error : exception instanceof ParsingException ? R.string.parsing_error : R.string.general_error;
            ErrorActivity.reportError(handler, context, exception, MainActivity.class, null, ErrorActivity.ErrorInfo.make(userAction, serviceId == -1 ? "none" : NewPipe.getNameOfService(serviceId), url + (optionalErrorMessage == null ? "" : optionalErrorMessage), errorId));
        }
    });
}
Also used : ParsingException(org.schabi.newpipe.extractor.exceptions.ParsingException) Handler(android.os.Handler) ContentNotAvailableException(org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException) Intent(android.content.Intent) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) MainActivity(org.schabi.newpipe.MainActivity) ReCaptchaException(org.schabi.newpipe.extractor.exceptions.ReCaptchaException)

Aggregations

ReCaptchaException (org.schabi.newpipe.extractor.exceptions.ReCaptchaException)3 IOException (java.io.IOException)2 Intent (android.content.Intent)1 Handler (android.os.Handler)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 InterruptedIOException (java.io.InterruptedIOException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 MainActivity (org.schabi.newpipe.MainActivity)1 ContentNotAvailableException (org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException)1 ParsingException (org.schabi.newpipe.extractor.exceptions.ParsingException)1