admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Toast;
 
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.JsonHttpResponseHandler;
import com.loopj.android.http.SyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.weikou.beibeivideo.BasicTextHttpResponseHandler;
import com.weikou.beibeivideo.BeibeiVideoAPI;
 
public class GetRealPathTask extends AsyncTask<String, Long, String[]> {
 
    private Context mContext;
 
    private String path;
 
    private String userAgent;
 
    private String leTvDada;
 
    private String baseUrl;
 
    public GetRealPathTask(Context mContext) {
        super();
        this.mContext = mContext;
    }
 
    public Context getContext() {
        return mContext;
    }
 
    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;
    }
 
    // 结果
 
    SyncHttpClient syncHttpClient = null;
 
    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;
    }
 
    // 获取视频链接
    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", "");
                    try {
                        BeibeiVideoAPI.setVideoErrorSy(
                                mContext,
                                mContext.getSharedPreferences("user",
                                        Context.MODE_PRIVATE).getString("uid",
                                        ""), params[1] + "::" + path + "::"
                                        + "豌豆荚", new JsonHttpResponseHandler() {
                                });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
 
                    /*
                     * 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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        reCount = 0;
        return new String[] { path, userAgent };
    }
 
    @Override
    protected String[] doInBackground(final String... params) {
        return getUrl(params);
    }
 
    private int reCount = 0;
 
    // 获取乐视链接
    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, videoDetailId);
                            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").getString("Url");
                                userAgent = jsonObject.getJSONObject("Data")
                                        .optString("Play_UserAgent");
                                System.out.println("请求地址完毕");
                                if (!StringUtils.isBlank(result))
                                    path = result;
                            }
                        }
                    });
        }
    }
 
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            Toast.makeText(mContext, msg.obj + "", Toast.LENGTH_LONG).show();
 
        };
    };
 
    private void getLeTvDada(JSONObject jsonObject, final String videodetailid)
            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);
        final String uurl = jsonObject.getJSONObject("Data").getString("Url");
        syncHttpClient.get(mContext, jsonObject.getJSONObject("Data")
                .getString("Url"), new TextHttpResponseHandler() {
 
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                    String responseString) {
                leTvDada = responseString;
                if (statusCode > 400)
                    try {
                        // MobclickAgent.reportError(mContext,mContext.getSharedPreferences("user",
                        // Context.MODE_PRIVATE).getString("uid", "") +"::"+uurl
                        // +"::"+videodetailid+"::"+statusCode);
                        BeibeiVideoAPI.setVideoErrorSy(
                                mContext,
                                mContext.getSharedPreferences("user",
                                        Context.MODE_PRIVATE).getString("uid",
                                        ""), uurl + "::" + videodetailid + "::"
                                        + statusCode,
                                new BasicTextHttpResponseHandler() {
 
                                    @Override
                                    public void onSuccessPerfect(
                                            int statusCode, Header[] headers,
                                            JSONObject jsonObject)
                                            throws Exception {
 
                                    }
                                });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
 
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers,
                    String responseString, Throwable throwable) {
 
                try {
                    // MobclickAgent.reportError(mContext,mContext.getSharedPreferences("user",
                    // Context.MODE_PRIVATE).getString("uid", "") +"::"+uurl
                    // +"::"+videodetailid+"::"+statusCode);
                    BeibeiVideoAPI.setVideoErrorSy(mContext, mContext
                            .getSharedPreferences("user", Context.MODE_PRIVATE)
                            .getString("uid", ""), uurl + "::" + videodetailid
                            + "::" + statusCode, new JsonHttpResponseHandler() {
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
 
        });
    }
 
    // 获取需要请求服务器的视频链接
    public static String getNeedSecondConnectServerUrl(String arrayStr) {
        JSONArray array = null;
        try {
            array = new JSONArray(arrayStr);
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (array != null) {
            String result = "";
            for (int i = 0; i < array.length(); i++) {
                result += array.optString(i) + ",";
            }
            if (result.endsWith(","))
                result = result.substring(0, result.length() - 1);
            return result;
        } else
            return "http://cloud.letv.com";
    }
}