xigua haokan

const requestpromise = require("request-promise");
const crypto = require('crypto');


function md5(str) {
  const hash = crypto.createHash('md5');
  hash.update(str);
  return hash.digest('hex');
}


async function searchXiGuaVideo(kw) {
  const url = `https://www.ixigua.com/search/${kw}/?logTag=594535e3690f17a88cdb&tab_name=search&fss=input`
  const htmlTxt = await getHtml(url)
  const startStr = 'window._SSR_HYDRATED_DATA='
  const endStr = '</script></body></html>'
  const startInd = htmlTxt.lastIndexOf(startStr) + startStr.length
  const endInd = htmlTxt.lastIndexOf(endStr)
  const encodedString = htmlTxt.substring(startInd, endInd).replace('undefined', '""').replace(/undefined/g, '""').replace(/\r\n/g, '').replace(/\r/g, '').replace(/\n/g, '')
  const json_data = JSON.parse(encodedString)
  const data = json_data['complexSearch']['data'][0]['data']
  const group_id = data["group_id"]
  return group_id
}

async function getXiGuaVideoPlayUrl(kw) {
  const vid = await searchXiGuaVideo(encodeURI(kw))
  console.log(5555555, vid)
  const url = `https://m.ixigua.com/douyin/share/video/${vid}?aweme_type=107&schema_type=1&utm_source=copy&utm_campaign=client_share&utm_medium=android&app=aweme`
  const htmlTxt = await getHtml(url)
  const startStr = '<script id="RENDER_DATA" type="application/json">'
  const endStr = '</script></body></html>'
  const startInd = htmlTxt.lastIndexOf(startStr) + startStr.length
  const endInd = htmlTxt.lastIndexOf(endStr)
  console.log(startInd, endInd)
  const encodedString = htmlTxt.substring(startInd, endInd)
  const decodedString = decodeURIComponent(encodedString)
  const json_data = JSON.parse(decodedString)
  const data = json_data["app"]["videoInfoRes"]["item_list"][0]
  const video_url = data["video"]["play_addr"]["url_list"][0].replace("playwm", "play")
  console.log(video_url)
}
async function searchHaoKanVideo(kw) {
  const l = Date.now()
  const kwEncode = encodeURIComponent(kw)
  const s = [1, kwEncode, 10, l, 1].join("_")
  const sign = md5(s)
  const url = `https://haokan.baidu.com/haokan/ui-search/pc/search/video?pn=1&rn=10&type=video&query=${kwEncode}&sign=${sign}&version=1&timestamp=${l}`
  const htmlTxt = await getHtml(url)
  const json_data = JSON.parse(htmlTxt)
  const vid = json_data["data"]["list"][0]['vid']
  return vid

}
async function getHaoKanVideoPlayUrl(kw) {
  const vid = await searchHaoKanVideo(kw)
  const htmlTxt = await getHtml(`https://haokan.baidu.com/v?_format=json&vid=${vid}`)
  console.log(htmlTxt)
  const json_data = JSON.parse(htmlTxt)
  const video_data = json_data["data"]["apiData"]["curVideoMeta"]
  const video_url = video_data["playurl"]
  console.log(video_url)
}
function getHtml(htmlurl) {
  //信源地址
  var options = {
    uri: htmlurl,
    method: 'GET',
    headers: {
      'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
      'cookie': 'ttwid=1%7CJZMT9e0PXopa24Dk5ol9iMhVs9jk_pztYB-l1Mn2_98%7C1717031970%7C225e65f731edce7d524da6efcf6b46a0ab52f78333e54f0a1bb7afc4b7abdeb0'
    },
    timeout: 1200 //设置http请求超时时间
  };
  return requestpromise(options).then((res) => {
    return res;
  }).catch((err) => {
    console.log(err.message);
    return {
      "error_code": '-1',
      "reason": "网络异常或者请求超时"
    }
  });

};
//getXiGuaVideoPlayUrl('诺言郭有才')

getHaoKanVideoPlayUrl('成语一帆风顺')

 

posted @ 2024-06-03 14:21  howhy  阅读(41)  评论(0编辑  收藏  举报