新增对Jellyfin的搜刮(但未完成自动化)
This commit is contained in:
@@ -3,7 +3,6 @@ package com.yutou.nas.utils;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yutou.nas.interfaces.DownloadInterface;
|
||||
import com.yutou.nas.utils.Interfaces.NetworkInterface;
|
||||
import com.yutou.nas.utils.StringUtils;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
@@ -11,25 +10,35 @@ import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class HttpTools {
|
||||
public static final String serverKey="zIrsh9TUZP2lfRW753PannG49E7VJvor";
|
||||
private static final int HttpRequestIndex=3;
|
||||
public static final String serverKey = "zIrsh9TUZP2lfRW753PannG49E7VJvor";
|
||||
private static final int HttpRequestIndex = 3;
|
||||
|
||||
public static String get(String url){
|
||||
return new HttpTools().http_get(url,0);
|
||||
public static String get(String url) {
|
||||
return https_get(url, null);
|
||||
}
|
||||
public static void post(final String url, final byte[] body, final NetworkInterface networkInterface){
|
||||
new HttpTools().http_post(url, body,0, networkInterface);
|
||||
|
||||
public static String post(final String url, final byte[] body) {
|
||||
return http_post(url, body, 0, null);
|
||||
}
|
||||
public static File syncDownload(final String url, final String saveName){
|
||||
|
||||
public static File syncDownload(final String url, final String saveName) {
|
||||
return new HttpTools().http_syncDownload(url, saveName);
|
||||
}
|
||||
public String http_get(String url,int index) {
|
||||
|
||||
public static String https_get(String url, Map<String, String> header) {
|
||||
try {
|
||||
HttpsURLConnection connection = (HttpsURLConnection) new URL(url).openConnection();
|
||||
URLConnection connection;
|
||||
connection = new URL(url).openConnection();
|
||||
connection.setRequestProperty("User-Agent", getExtUa());
|
||||
if (header != null) {
|
||||
for (String key : header.keySet()) {
|
||||
connection.addRequestProperty(key, header.get(key));
|
||||
}
|
||||
}
|
||||
connection.connect();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
StringBuilder str = new StringBuilder();
|
||||
@@ -38,68 +47,57 @@ public class HttpTools {
|
||||
str.append(tmp);
|
||||
}
|
||||
reader.close();
|
||||
connection.disconnect();
|
||||
return str.toString();
|
||||
} catch (Exception e) {
|
||||
if(index<HttpRequestIndex){
|
||||
return http_get(url,++index);
|
||||
}
|
||||
System.err.println("error url = "+url);
|
||||
System.err.println("error url = " + url);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void http_post(final String url, final byte[] body,final int index, final NetworkInterface networkInterface) {
|
||||
|
||||
new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
String tmp;
|
||||
StringBuilder str = new StringBuilder();
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.addRequestProperty("User-Agent", getExtUa());
|
||||
connection.setConnectTimeout(5 * 1000);
|
||||
connection.setReadTimeout(10 * 1000);
|
||||
//connection.addRequestProperty("Connection", "keep-alive");
|
||||
//connection.addRequestProperty("User-Agent", getExtUa());
|
||||
//connection.addRequestProperty("content-type", "application/json");
|
||||
connection.addRequestProperty("charset", "UTF-8");
|
||||
OutputStream outputStream = connection.getOutputStream();
|
||||
|
||||
outputStream.write(body);
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
while ((tmp = reader.readLine()) != null) {
|
||||
str.append(tmp);
|
||||
}
|
||||
final String finalStr = str.toString();
|
||||
|
||||
// Log.i(TAG + "[" + url + "?" + toGetSplice(body) + "]", "body:" + str + " (" + connection.getResponseCode() + ")");
|
||||
if (networkInterface != null) {
|
||||
try {
|
||||
networkInterface.httpGetData(str.toString(), connection.getResponseCode());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
connection.disconnect();
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
if(index<HttpRequestIndex){
|
||||
http_post(url, body, index+1, networkInterface);
|
||||
}else {
|
||||
e.printStackTrace();
|
||||
}
|
||||
public static String http_post(String url, byte[] body, int index, Map<String, String> headers) {
|
||||
String tmp;
|
||||
StringBuilder str = new StringBuilder();
|
||||
try {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
if (headers != null) {
|
||||
for (String key : headers.keySet()) {
|
||||
connection.addRequestProperty(key, headers.get(key));
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
connection.addRequestProperty("User-Agent", getExtUa());
|
||||
connection.setConnectTimeout(5 * 1000);
|
||||
connection.setReadTimeout(10 * 1000);
|
||||
//connection.addRequestProperty("Connection", "keep-alive");
|
||||
//connection.addRequestProperty("User-Agent", getExtUa());
|
||||
//connection.addRequestProperty("content-type", "application/json");
|
||||
connection.addRequestProperty("charset", "UTF-8");
|
||||
OutputStream outputStream = connection.getOutputStream();
|
||||
//System.out.println(new String(body));
|
||||
outputStream.write(body);
|
||||
outputStream.flush();
|
||||
outputStream.close();
|
||||
connection.connect();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
while ((tmp = reader.readLine()) != null) {
|
||||
str.append(tmp);
|
||||
}
|
||||
String finalStr = str.toString();
|
||||
|
||||
connection.disconnect();
|
||||
reader.close();
|
||||
return finalStr;
|
||||
} catch (Exception e) {
|
||||
if (index < HttpRequestIndex) {
|
||||
return http_post(url, body, index + 1, headers);
|
||||
} else {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String getExtUa() {
|
||||
@@ -205,14 +203,15 @@ public class HttpTools {
|
||||
if (jar != null) {
|
||||
jar.delete();
|
||||
}
|
||||
downloadInterface.onError(e);
|
||||
if (downloadInterface != null)
|
||||
downloadInterface.onError(e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public synchronized File http_syncDownload(final String url, final String saveName) {
|
||||
if(StringUtils.isEmpty(url)){
|
||||
if (StringUtils.isEmpty(url)) {
|
||||
return null;
|
||||
}
|
||||
File jar = null;
|
||||
|
||||
Reference in New Issue
Block a user