2015-09-04 02:15:03 +02:00
|
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
2016-12-24 15:19:40 +01:00
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
|
2017-09-03 03:04:18 -03:00
|
|
|
|
import org.schabi.newpipe.util.ExtractorHelper;
|
2016-12-07 19:22:27 +01:00
|
|
|
|
|
2015-09-04 02:15:03 +02:00
|
|
|
|
import java.io.BufferedReader;
|
2015-11-08 01:09:03 +00:00
|
|
|
|
import java.io.IOException;
|
2015-09-04 02:15:03 +02:00
|
|
|
|
import java.io.InputStreamReader;
|
2017-09-03 03:04:18 -03:00
|
|
|
|
import java.io.InterruptedIOException;
|
2015-09-04 02:15:03 +02:00
|
|
|
|
import java.net.URL;
|
2016-09-10 16:26:21 +02:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
2015-09-04 02:15:03 +02:00
|
|
|
|
|
2015-12-24 14:55:33 +01:00
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
|
2015-12-24 14:55:33 +01:00
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
|
/*
|
2016-01-28 21:21:19 +01:00
|
|
|
|
* Created by Christian Schabesberger on 28.01.16.
|
2015-09-04 02:15:03 +02:00
|
|
|
|
*
|
2016-01-28 23:27:16 +01:00
|
|
|
|
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
|
2015-09-04 02:15:03 +02:00
|
|
|
|
* Downloader.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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-18 11:50:22 +01:00
|
|
|
|
public class Downloader implements org.schabi.newpipe.extractor.Downloader {
|
2017-09-03 03:04:18 -03:00
|
|
|
|
|
|
|
|
|
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";
|
2016-12-07 22:16:28 +01:00
|
|
|
|
private static String mCookies = "";
|
2015-09-04 02:15:03 +02:00
|
|
|
|
|
2016-12-07 22:09:24 +01:00
|
|
|
|
private static Downloader instance = null;
|
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
|
private Downloader() {
|
|
|
|
|
}
|
2016-12-07 22:09:24 +01:00
|
|
|
|
|
|
|
|
|
public static Downloader getInstance() {
|
2017-09-03 03:04:18 -03:00
|
|
|
|
if (instance == null) {
|
2016-12-07 22:09:24 +01:00
|
|
|
|
synchronized (Downloader.class) {
|
|
|
|
|
if (instance == null) {
|
|
|
|
|
instance = new Downloader();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 22:16:28 +01:00
|
|
|
|
public static synchronized void setCookies(String cookies) {
|
|
|
|
|
Downloader.mCookies = cookies;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static synchronized String getCookies() {
|
|
|
|
|
return Downloader.mCookies;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
|
/**
|
|
|
|
|
* Download the text file at the supplied URL as in download(String),
|
2015-11-16 23:32:00 +00:00
|
|
|
|
* but set the HTTP header field "Accept-Language" to the supplied string.
|
2017-09-03 03:04:18 -03:00
|
|
|
|
*
|
|
|
|
|
* @param siteUrl the URL of the text file to return the contents of
|
2015-11-16 23:32:00 +00:00
|
|
|
|
* @param language the language (usually a 2-character code) to set as the preferred language
|
2017-09-03 03:04:18 -03:00
|
|
|
|
* @return the contents of the specified text file
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2016-12-24 15:19:40 +01:00
|
|
|
|
public String download(String siteUrl, String language) throws IOException, ReCaptchaException {
|
2016-09-10 16:26:21 +02:00
|
|
|
|
Map<String, String> requestProperties = new HashMap<>();
|
|
|
|
|
requestProperties.put("Accept-Language", language);
|
|
|
|
|
return download(siteUrl, requestProperties);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
|
/**
|
|
|
|
|
* Download the text file at the supplied URL as in download(String),
|
|
|
|
|
* but set the HTTP headers included in the customProperties map.
|
|
|
|
|
*
|
|
|
|
|
* @param siteUrl the URL of the text file to return the contents of
|
2016-09-10 16:26:21 +02:00
|
|
|
|
* @param customProperties set request header properties
|
|
|
|
|
* @return the contents of the specified text file
|
2017-09-03 03:04:18 -03:00
|
|
|
|
* @throws IOException
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
2016-12-24 15:19:40 +01:00
|
|
|
|
public String download(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException {
|
2016-01-31 19:57:30 +01:00
|
|
|
|
URL url = new URL(siteUrl);
|
2016-07-25 11:59:37 +02:00
|
|
|
|
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
2016-09-10 16:26:21 +02:00
|
|
|
|
Iterator it = customProperties.entrySet().iterator();
|
2017-09-03 03:04:18 -03:00
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
Map.Entry pair = (Map.Entry) it.next();
|
|
|
|
|
con.setRequestProperty((String) pair.getKey(), (String) pair.getValue());
|
2016-09-10 16:26:21 +02:00
|
|
|
|
}
|
2016-01-31 19:57:30 +01:00
|
|
|
|
return dl(con);
|
2015-11-08 01:09:03 +00:00
|
|
|
|
}
|
2016-01-04 00:10:51 +01:00
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public String download(String siteUrl) throws IOException, ReCaptchaException {
|
|
|
|
|
URL url = new URL(siteUrl);
|
|
|
|
|
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
|
|
|
|
|
//HttpsURLConnection con = NetCipher.getHttpsURLConnection(url);
|
|
|
|
|
return dl(con);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common functionality between download(String url) and download(String url, String language)
|
|
|
|
|
*/
|
2016-12-24 15:19:40 +01:00
|
|
|
|
private static String dl(HttpsURLConnection con) throws IOException, ReCaptchaException {
|
2015-11-29 13:06:27 +01:00
|
|
|
|
StringBuilder response = new StringBuilder();
|
2016-01-31 19:57:30 +01:00
|
|
|
|
BufferedReader in = null;
|
2015-11-08 01:09:03 +00:00
|
|
|
|
|
|
|
|
|
try {
|
2017-09-03 03:04:18 -03:00
|
|
|
|
con.setReadTimeout(30 * 1000);// 30s
|
2015-09-04 02:15:03 +02:00
|
|
|
|
con.setRequestMethod("GET");
|
|
|
|
|
con.setRequestProperty("User-Agent", USER_AGENT);
|
|
|
|
|
|
2016-12-07 22:16:28 +01:00
|
|
|
|
if (getCookies().length() > 0) {
|
|
|
|
|
con.setRequestProperty("Cookie", getCookies());
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-03 03:04:18 -03:00
|
|
|
|
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
2015-09-04 02:15:03 +02:00
|
|
|
|
|
2017-09-28 10:06:48 -03:00
|
|
|
|
String inputLine;
|
2017-09-03 03:04:18 -03:00
|
|
|
|
while ((inputLine = in.readLine()) != null) {
|
2015-09-04 02:15:03 +02:00
|
|
|
|
response.append(inputLine);
|
|
|
|
|
}
|
2017-09-03 03:04:18 -03:00
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e("Downloader", "dl() ----- Exception thrown → " + e.getClass().getName());
|
|
|
|
|
|
|
|
|
|
if (ExtractorHelper.isInterruptedCaused(e)) {
|
|
|
|
|
throw new InterruptedIOException(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 19:22:27 +01:00
|
|
|
|
/*
|
|
|
|
|
* 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) {
|
2016-12-24 15:19:40 +01:00
|
|
|
|
throw new ReCaptchaException("reCaptcha Challenge requested");
|
2016-12-07 19:22:27 +01:00
|
|
|
|
}
|
2017-09-03 03:04:18 -03:00
|
|
|
|
|
|
|
|
|
throw new IOException(con.getResponseCode() + " " + con.getResponseMessage(), e);
|
2016-01-31 19:57:30 +01:00
|
|
|
|
} finally {
|
2017-09-03 03:04:18 -03:00
|
|
|
|
if (in != null) {
|
2016-01-31 19:57:30 +01:00
|
|
|
|
in.close();
|
|
|
|
|
}
|
2015-11-02 15:03:11 +00:00
|
|
|
|
}
|
2015-11-29 13:06:27 +01:00
|
|
|
|
|
2015-09-04 02:15:03 +02:00
|
|
|
|
return response.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|