package com.weikou.beibeivideo.util;
|
|
import java.io.IOException;
|
import java.net.HttpURLConnection;
|
import java.net.MalformedURLException;
|
import java.net.URL;
|
import java.util.Map;
|
import java.util.Map.Entry;
|
|
import org.apache.http.Header;
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
import android.content.Context;
|
import android.os.AsyncTask;
|
import android.os.Message;
|
import android.text.TextUtils;
|
|
import com.google.gson.Gson;
|
import com.google.gson.JsonSyntaxException;
|
import com.google.gson.reflect.TypeToken;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.loopj.android.http.SyncHttpClient;
|
import com.loopj.android.http.TextHttpResponseHandler;
|
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
|
import com.weikou.beibeivideo.BeibeiVideoAPI;
|
|
public class GetDownLoadTask extends AsyncTask<String, Long, String[]> {
|
|
private Context mContext;
|
|
private String path;
|
|
private String userAgent;
|
|
private String leTvDada;
|
|
private String baseUrl;
|
|
public GetDownLoadTask(Context mContext) {
|
super();
|
this.mContext = mContext;
|
}
|
|
public Context getContext() {
|
return mContext;
|
}
|
|
int reCount = 0;
|
|
// 获取视频链接
|
private String[] getUrl(final String... params) {
|
baseUrl = params[0];
|
path = params[0];
|
if (!TextUtils.isEmpty(path)) {
|
reCount = 0;
|
if (isContains(params[2], path)) {
|
if (BeibeiVideoAPI.isDebug) {
|
Message msg = new Message();
|
msg.obj = path.contains("http://cloud.letv.com") + "--"
|
+ path;
|
// handler.sendMessage(msg);
|
}
|
getThirdUrl(path, params[1], "", "");
|
} else if (path.contains("http")) {
|
path = "http://flv.wandoujia.com/hack/flv/download?url=" + path
|
+ "?ref=pinapple&format=HIGH&f=android&v=2.2.0";
|
// 进行豌豆荚同步请求 3-请求三次
|
String result = getWanDouJiaUrl(path);
|
if (StringUtils.isBlank(result)) {// 无法解析
|
String uid = mContext.getSharedPreferences("user",
|
Context.MODE_PRIVATE).getString("uid", "");
|
/*
|
* BeibeiVideoAPI.getRealUrl(mContext, uid, params[1],
|
* params[0], new BasicTextHttpResponseHandler() {
|
*
|
* @Override public void onSuccessPerfect(int statusCode,
|
* Header[] headers, JSONObject jsonObject) throws Exception
|
* { if (jsonObject != null) { path =
|
* jsonObject.optString("Data"); } } });
|
*/
|
|
getThirdUrl(baseUrl, params[1], "", "");
|
} else {
|
// path = result;
|
}
|
|
} else {
|
path = BeibeiConstant.HOST + "/BuWan" + path;
|
}
|
}
|
System.out.println("返回地址");
|
reCount = 0;
|
while (isContains(params[2], path) && reCount < 3) {
|
getThirdUrl(path, params[1], "", "");
|
reCount++;
|
try {
|
Thread.sleep(reCount * 2 * 1000);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
reCount = 0;
|
|
return new String[] { path, userAgent };
|
}
|
|
private boolean isContains(String list, String path) {
|
if (list != null && list.length() > 0) {
|
String[] sts = list.split(",");
|
for (String st : sts) {
|
if (path.contains(st))
|
return true;
|
}
|
}
|
return false;
|
}
|
|
private String getWanDouJiaUrl(String url) {
|
String result = "";
|
int count = 0;
|
while (StringUtils.isBlank(result) && count < 3) {
|
count++;
|
try {
|
URL u = new URL(url);
|
HttpURLConnection connection = (HttpURLConnection) u
|
.openConnection();
|
connection.setReadTimeout(60 * 1000);
|
connection.setConnectTimeout(60 * 1000);
|
connection.setRequestMethod("GET");
|
int responseCode = connection.getResponseCode();
|
if (responseCode == 200 || responseCode == 302) {
|
result = connection.getURL().toString();
|
break;
|
}
|
} catch (MalformedURLException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return result;
|
}
|
|
@Override
|
protected String[] doInBackground(final String... params) {
|
return getUrl(params);
|
}
|
|
private void getThirdUrl(final String pa, final String videoDetailId,
|
final String setp, final String data) {
|
String uid = mContext
|
.getSharedPreferences("user", Context.MODE_PRIVATE).getString(
|
"uid", "");
|
BeibeiVideoAPI.getThirdVideoRequest(mContext, uid, videoDetailId, pa,
|
setp, data, new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
getLeTvDada(jsonObject);
|
if (!StringUtils.isBlank(jsonObject.optJSONObject(
|
"Data").optString("Step"))
|
&& Integer.parseInt(jsonObject
|
.optJSONObject("Data").optString(
|
"Step")) > 1) {
|
getThirdUrl(
|
pa,
|
videoDetailId,
|
(Integer.parseInt(jsonObject
|
.optJSONObject("Data")
|
.optString("Step")) - 1)
|
+ "", leTvDada);
|
}
|
}
|
}
|
});
|
|
if (!TextUtils.isEmpty(leTvDada)) {
|
BeibeiVideoAPI.parseVideoUrl(mContext, uid, baseUrl, videoDetailId,
|
leTvDada, new BasicTextHttpResponseHandler() {
|
|
@Override
|
public void onSuccessPerfect(int statusCode,
|
Header[] headers, JSONObject jsonObject)
|
throws Exception {
|
if (jsonObject.getBoolean("IsPost")) {
|
String result = jsonObject
|
.getJSONObject("Data").optString(
|
"DownLoad");
|
userAgent = jsonObject.getJSONObject("Data")
|
.optString("DownLoad_UserAgent");
|
System.out.println("请求地址完毕");
|
if (!StringUtils.isBlank(result))
|
path = result;
|
}
|
}
|
});
|
}
|
}
|
|
private void getLeTvDada(JSONObject jsonObject) throws JsonSyntaxException,
|
JSONException {
|
Map<String, String> headersMap = new Gson().fromJson(jsonObject
|
.getJSONObject("Data").getJSONObject("Header").toString(),
|
new TypeToken<Map<String, String>>() {
|
}.getType());
|
SyncHttpClient syncHttpClient = new SyncHttpClient();
|
syncHttpClient.setURLEncodingEnabled(false);
|
syncHttpClient.setTimeout(60 * 1000);
|
for (Entry<String, String> entry : headersMap.entrySet()) {
|
syncHttpClient.addHeader(entry.getKey(), entry.getValue());
|
}
|
syncHttpClient.setURLEncodingEnabled(false);
|
syncHttpClient.get(mContext, jsonObject.getJSONObject("Data")
|
.getString("Url"), new TextHttpResponseHandler() {
|
|
@Override
|
public void onSuccess(int statusCode, Header[] headers,
|
String responseString) {
|
leTvDada = responseString;
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers,
|
String responseString, Throwable throwable) {
|
}
|
});
|
}
|
}
|