获取虎牙,YY,映客等平台直播源

package com.video.parse;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.video.parse.common.util.Common;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.HashMap;

@Slf4j
public class LiveResource {

    public static void main(String[] args) {
        yy("54880976");

        huYa("52513");

        yingKe("727419703");
    }

    private static String yingKe(String roomNumber) {
        String interfaceUrl = StrFormatter.format("https://webapi.busi.inke.cn/web/live_share_pc?uid={}", roomNumber);
        String jsonStr = HttpUtil.get(interfaceUrl);
        log.debug(JSONUtil.toJsonPrettyStr(jsonStr));
        JSON json = JSONUtil.parse(jsonStr);
        String hls = JSONUtil.getByPath(json, "data.live_addr[0].hls_stream_addr").toString();
        log.debug("映客直播间: [{}] , 直播源: [{}] ", roomNumber, hls);
        return hls;
    }

    /**
     * 获取虎牙直播间直播源
     *
     * @param roomNumber 直播间号
     * @return
     */
    private static String huYa(String roomNumber) {
        String interfaceUrl = StrFormatter.format("https://m.huya.com/{}", roomNumber);
        Document doc = Common.jsonpConnect(interfaceUrl, true);
        Elements select = doc.select("video[id=html5player-video]");
        String src = select.get(0).attr("src");
        String hls = "https:" + StrUtil.subBefore(src, "?", true);
        log.debug("虎牙直播间: [{}] , 直播源: [{}] ", roomNumber, hls);
        return hls;
    }

    /**
     * 获取虎牙直播间直播源
     *
     * @param roomNumber 直播间号
     * @return
     */
    private static String yy(String roomNumber) {
        HashMap headers = Common.getMobileHeaders();
        headers.put("referer", StrFormatter.format("http://wap.yy.com/mobileweb/{}", roomNumber));
        String interfaceUrl = StrFormatter.format("http://interface.yy.com/hls/new/get/{}/{}/1200?source=wapyy&callback=jsonp3", roomNumber, roomNumber);
        String body = HttpUtil.createPost(interfaceUrl).addHeaders(headers).execute().body();
        String jsonStr = StrUtil.sub(body, 7, body.length() - 1);
        JSON json = JSONUtil.parse(jsonStr);
        String hls = JSONUtil.getByPath(json, "hls").toString();
        log.debug("YY直播间: [{}] , 直播源: [{}] ", roomNumber, hls);
        return hls;
    }
}
package com.video.parse.common.util;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.HashMap;

public class Common {

    public static Document jsonpConnect(String url, Boolean isMobile) {
        Document doc = null;
        if (StrUtil.isNotEmpty(url)) {
            try {
                url = ReUtil.get(PatternPool.URL_HTTP, url, 0);
                Connection connect = Jsoup.connect(url);
                if (isMobile) {
                    connect.headers(Common.getMobileHeaders());
                }
                doc = connect.timeout(5000).get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }

    public static String getLocationUrl(String url) {
        HashMap headers = getMobileHeaders();
        HttpResponse execute = HttpUtil.createGet(url).addHeaders(headers).execute();
        String redirectUrl = execute.header("Location");
        return redirectUrl;
    }

    public static HashMap getMobileHeaders() {
        HashMap headers = MapUtil.newHashMap();
        headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Mobile Safari/537.36");
        // headers.put("User-Agent", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1");
        // headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Mobile Safari/537.36");
        return headers;
    }
}

依赖jsoup 、hutool、lombok工具

你可能感兴趣的:(Java)