admin
2021-07-28 c0269fcfa876b9c5cf309b2006462b4d09c5ef95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.hanju.video.app.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.hanju.video.app.HttpApiUtil;
 
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 (HttpApiUtil.isDebug) {
                    Message msg = new Message();
                    msg.obj = path.contains("http://cloud.letv.com") + "--"
                            + path;
                }
            } 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", "");
 
                } else {
                    // path = result;
                }
 
            } else {
                path = HanJuConstant.HOST + "/BuWan" + path;
            }
        }
        System.out.println("返回地址");
        reCount = 0;
        while (isContains(params[2], path) && reCount < 3) {
            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 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) {
            }
        });
    }
}