解析腾讯视频上传视频源地址网址

        公司最近想用小程序播放腾讯视频的视频,由于小程序的微信接口只是调用视频的vid就能进行播放,但是在pc网页上进行播放就要拿到视频的真实地址,然而实际上腾讯视频上传后只是给你一个:https://v.qq.com/x/page/d0744x87gdd.html这样的网址,那么就需要我们在后台代码中进行解析,腾讯工程师非常调皮的是我们即便解析到真实地址过一段时间也会失效,所以需要随播随解

     下面上代码,笔者用的net.sf.json以及org.apache.commons    jar包,请自行查找! net.sf.json类与Gson、fastjson相似就不相应描述了!

  

package com.shop.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

import org.apache.commons.collections.map.ListOrderedMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 解析腾讯视频上传地址
 * 
 * @author 艾洪凯 time:2018-12-28
 *
 */

public class JxQqVideo {
	public static String jiexi(String uri) {
		Map map = new HashMap<>();
		uri = uri.substring(uri.lastIndexOf("/") + 1, uri.lastIndexOf("."));
		uri = "vids=" + uri;
		String requestUrl = "http://vv.video.qq.com/getinfo?platform=101001&charge=0&otype=json&defn=shd";
		String response = "";
		try {
			response = getData(requestUrl, uri);
			response = response.replace("QZOutputJson=", "").replace(";", "");
			map = parseJSON2Map(response);
			Map map2 = (Map) map.get("vl");
			List> list = (List>) map2.get("vi");
			String vkey = list.get(0).get("fvkey").toString();// vkey
			String fn = list.get(0).get("fn").toString();// fn
			map2 = (Map) list.get(0).get("ul");
			list = (List>) map2.get("ui");
			// 加判断
			String url1 = "";
			if (list.size() >= 2) {
				url1 = list.get(1).get("url").toString();
			} else {
				url1 = list.get(0).get("url").toString();
			}
			response = url1 + fn + "?vkey=" + vkey;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return response;
	}

	public static Map parseJSON2Map(String jsonStr) {
		ListOrderedMap map = new ListOrderedMap();
		// 最外层解析
		JSONObject json = JSONObject.fromObject(jsonStr);
		for (Object k : json.keySet()) {
			Object v = json.get(k);
			// 如果内层还是数组的话,继续解析
			if (v instanceof JSONArray) {
				List> list = new ArrayList>();
				Iterator it = ((JSONArray) v).iterator();
				while (it.hasNext()) {
					JSONObject json2 = it.next();
					list.add(parseJSON2Map(json2.toString()));
				}
				map.put(k.toString(), list);
			} else {
				if (v.toString().contains("{")) {
					map.put(k.toString(), parseJSON2Map(v.toString()));
				} else {
					map.put(k.toString(), v);
				}
			}
		}
		return map;
	}

	public static String getData(String url, String request) throws UnsupportedEncodingException, IOException {
		String charset = "UTF-8";
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		HttpURLConnection connect = (HttpURLConnection) (new URL(url).openConnection());
		connect.setRequestMethod("GET");
		connect.setDoOutput(true);
		connect.setConnectTimeout(1000 * 10);
		connect.setReadTimeout(1000 * 80);
		connect.setRequestProperty("ContentType", "application/x-www-form-urlencoded"); // 采用通用的URL百分号编码的数据MIME类型来传参和设置请求头
		connect.setDoInput(true);
		// 连接
		connect.connect();
		// 发送数据
		connect.getOutputStream().write(request.getBytes(charset));
		// 接收数据
		int responseCode = connect.getResponseCode();
		if (responseCode == HttpURLConnection.HTTP_OK) {
			InputStream in = connect.getInputStream();
			byte[] data = new byte[1024];
			int len = 0;
			while ((len = in.read(data, 0, data.length)) != -1) {
				outStream.write(data, 0, len);
			}
			in.close();
		}
		// 关闭连接
		connect.disconnect();
		String response = outStream.toString("UTF-8");
		return response;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(jiexi("https://v.qq.com/x/page/d0744x87gdd.html"));

	}

}

 运行结果:

浏览器浏览效果:

解析腾讯视频上传视频源地址网址_第1张图片

 代码写的不好,各位凑合看,还有什么好的方法,大家留言交流一下~

你可能感兴趣的:(辅助类)