解析美拍视频源链接

原文链接https://www.jianshu.com/p/8b6e9caca9cc 

/**
 * 解密加密过的字符串
 * Created by lihan on 2017/7/29.
 */
public class ParseUrl {
    private Map dict1;
    private Map dict2;
    
    // 获得16进制数,该数用来分割字符串
    public Map getHex(String param1){
        dict1 = new HashMap();
        String cstr =  param1.substring(4);//str
        String[] splitStr = param1.substring(0,4).split("");
        String hex = "";
        for (int i=3; i >= 0; i--){
            hex = hex + splitStr[i];
        }
        dict1.put("str", cstr);
        dict1.put("hex", hex);
        return dict1;
    }
    // 获取正确的字符串,解析16进制数
    public Map getDecimal(String param1){
        dict2 = new HashMap();
        // loc2是用来分割字符串的索引标识,转换16进制
        String loc2 = String.valueOf(Integer.parseInt(param1,16));
        String[] pre = loc2.substring(0,2).split("");//dict1.put("loc2", loc2.substring(0,2));
        String[] tail = loc2.substring(2).split("");
        dict2.put("pre", pre);
        dict2.put("tail", tail);
        return dict2;
    }
    // 分割字符串
    public String substr(String param1, String[] param2) {
        String loc3 = param1.substring(0, Integer.parseInt(param2[0]));//param2 = pu.getDec(pa2).get("pre")
        String loc4 = param1.substring(Integer.parseInt(param2[0]), Integer.parseInt(param2[0])+Integer.parseInt(param2[1]));
        return loc3 + param1.substring(Integer.parseInt(param2[0])).replace(loc4, "");
    }
    // 获取分割的位置
    public String[] getPosition(String param1, String[] param2){
        param2[0] = String.valueOf(param1.length() - Integer.parseInt(param2[0]) - Integer.parseInt(param2[1]));
        return param2;
    }

    // 程序入口
    public static void main(String[] args) throws Exception {
        ParseUrl pu = new ParseUrl();
        // 获取html中的加密字符串
        String code = Crawl.parsehtml("http://www.meipai.com/media/816057957");
        Map dict2 = pu.getHex(code);
        Map dict3 = pu.getDecimal(dict2.get("hex"));
        String str4 = pu.substr(dict2.get("str"), dict3.get("pre"));
        BASE64Decoder base64 = new BASE64Decoder();
        byte[] url = base64.decodeBuffer(pu.substr(str4, pu.getPosition(str4, dict3.get("tail"))));
        // 视频真实的url地址
        System.out.println(new String(url));
    }
}

 

你可能感兴趣的:(Java)