admin
6 天以前 7f0825f8195a522ed7e8bcdb6347f3a719e06c74
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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) {
            }
        });
    }
}