tencent.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import requests
  2. import ujson
  3. import time
  4. import re
  5. import execjs
  6. from urllib.parse import quote
  7. class tencent:
  8. def __init__(self, url, timeout=None,cookie=''):
  9. self.url = url
  10. self.timeout = timeout or 2
  11. self.headers = {
  12. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36¬",
  13. "cookie":cookie,
  14. }
  15. self.int_time = int(time.time())
  16. self.cookie = cookie
  17. self.cookie_dict = {}
  18. self.parse_cookie()
  19. def parse_cookie(self):
  20. if self.cookie:
  21. for i in self.cookie.rstrip(';').split(";"):
  22. kv = i.split("=")
  23. print(kv)
  24. self.cookie_dict[kv[0].strip()] = kv[1]
  25. print(self.cookie_dict)
  26. def get_vid(self):
  27. vipUrl = self.url
  28. vid = None
  29. if vipUrl.find('v.qq.com/x/cover/') > -1:
  30. _type = vipUrl.split("v.qq.com/x/cover/")[1].split(".html")[0]
  31. if _type.find('/') > -1:
  32. vid = _type.split("/")[1]
  33. else:
  34. r = requests.get(vipUrl, headers=self.headers)
  35. html = r.text
  36. vid = html.split('<link rel="canonical" href="https://v.qq.com/x/cover/')[1].split('/')[1].split('.')[0]
  37. else:
  38. if re.search('/page/.*\.html',vipUrl):
  39. vid = vipUrl.split("/page/")[1].split(".html")[0]
  40. else:
  41. if vipUrl.find('&vid=') > -1:
  42. vid = vipUrl.split("&vid=")[1].split("&")[0]
  43. print(f'vid:{vid}')
  44. return vid
  45. def get_adparams(self):
  46. pf = "in"
  47. ad_type = quote("LD|KB|PVL")
  48. pf_ex = "pc"
  49. url = quote(self.url)
  50. refer = quote("https://v.qq.com/")
  51. ty = "web"
  52. plugin = "1.0.0"
  53. v = "3.5.57"
  54. coverid = re.search("cover/(.*?).html", self.url).group(1)
  55. coverid = coverid.split('/')[0]
  56. # print(f'coverid:{coverid}')
  57. vid = self.get_vid()
  58. pt = ""
  59. flowid = "f48222928272c7950a794ffbea32022c_10901"
  60. vptag = quote("vptag=www_baidu_com|channel")
  61. pu = "1"
  62. chid = "0"
  63. adaptor = "2"
  64. dtype = "1"
  65. live = "0"
  66. resp_type = "json"
  67. guid = "2634e72faf052aa51f98971b2a68718c"
  68. req_type = 1
  69. # from = "0"
  70. appversion = "1.0.157"
  71. uid = self.cookie_dict['vqq_vuserid']
  72. tkn = self.cookie_dict['vqq_vusession']
  73. lt = "qq"
  74. platform = "10901"
  75. opid = self.cookie_dict['vqq_openid']
  76. atkn = self.cookie_dict['vqq_access_token']
  77. appid = self.cookie_dict['vqq_appid']
  78. tpid = "1"
  79. result = f"pf={pf}&ad_type={ad_type}&pf_ex={pf_ex}&url={url}&refer={refer}&ty={ty}&plugin={plugin}&v={v}&coverid={coverid}&vid={vid}&pt={pt}&flowid={flowid}&vptag={vptag}&pu={pu}&chid={chid}&adaptor={adaptor}&dtype={dtype}&live={live}&resp_type={resp_type}&guid={guid}&req_type={req_type}&from=0&appversion={appversion}&" \
  80. f"uid={uid}&tkn={tkn}&lt={lt}&platform={platform}&opid={opid}&atkn={atkn}&appid={appid}&tpid={tpid}"
  81. return result
  82. def get_vinfoparams(self):
  83. spsrt = "1"
  84. charge = "1"
  85. defaultfmt = "auto"
  86. otype = "ojson"
  87. guid = "2634e72faf052aa51f98971b2a68718c"
  88. # 随机数 + platform
  89. flowid = "f48222928272c7950a794ffbea32022c_10901"
  90. platform = "10901"
  91. sdtfrom = "v1010"
  92. defnpayver = "1"
  93. appVer = "3.5.57"
  94. host = "v.qq.com"
  95. ehost = quote(self.url)
  96. refer = "v.qq.com"
  97. sphttps = "1"
  98. tm = self.int_time
  99. spwm = "4"
  100. logintoken = quote(str({"main_login": self.cookie_dict['main_login'], "openid": self.cookie_dict['vqq_openid'],
  101. "appid": self.cookie_dict['vqq_appid'],
  102. "access_token": self.cookie_dict['vqq_access_token'],
  103. "vuserid": self.cookie_dict['vqq_vuserid'],
  104. "vusession": self.cookie_dict['vqq_vusession']}))
  105. # print(f'logintoken:{logintoken}')
  106. vid = self.get_vid()
  107. defn = "fhd"
  108. fhdswitch = "0"
  109. show1080p = "1"
  110. isHLS = "1"
  111. dtype = "3"
  112. sphls = "2"
  113. spgzip = "1"
  114. dlver = "2"
  115. drm = "32"
  116. hdcp = "1"
  117. spau = "1"
  118. spaudio = "15"
  119. defsrc = "1"
  120. encryptVer = "9.1"
  121. cKey = self.get_cKey(platform, appVer, vid, guid, tm)
  122. fp2p = "1"
  123. spadseg = "3"
  124. result = f"spsrt={spsrt}&charge={charge}&defaultfmt={defaultfmt}&otype={otype}&guid={guid}&flowid={flowid}&platform={platform}&sdtfrom={sdtfrom}&defnpayver={defnpayver}&appVer={appVer}&host={host}&ehost={ehost}&refer={refer}&sphttps={sphttps}&tm={tm}&spwm={spwm}&logintoken={logintoken}&vid={vid}&defn={defn}&fhdswitch={fhdswitch}&show1080p={show1080p}&isHLS={isHLS}&dtype={dtype}&sphls={sphls}&spgzip={spgzip}&dlver={dlver}&drm={drm}&hdcp={hdcp}&spau={spau}&spaudio={spaudio}&defsrc={defsrc}&encryptVer={encryptVer}&cKey={cKey}&fp2p={fp2p}&spadseg={spadseg}"
  125. return result
  126. def get_cKey(self, platform, version, vid, guid, tm):
  127. file = './js/getck.js'
  128. ctx = execjs.compile(open(file).read())
  129. params = ctx.call("getckey", platform, version, vid, '', guid,
  130. tm)
  131. return params
  132. def get_buid(self):
  133. return "vinfoad"
  134. def deal_data(self,data):
  135. # print(data)
  136. vinfo = ujson.loads(data['vinfo'])
  137. vl = vinfo['vl']
  138. urls = vl['vi'][0]['ul']['ui']
  139. # print(urls)
  140. try:
  141. pt = urls[-1]['hls']['pt']
  142. except:
  143. pt = ''
  144. url = urls[-1]['url']
  145. realUrl = url + pt
  146. print(realUrl)
  147. return realUrl
  148. def start(self):
  149. ad_params = self.get_adparams()
  150. vinfoparams = self.get_vinfoparams()
  151. buid = self.get_buid()
  152. params = {"buid": buid,
  153. "adparam": ad_params,
  154. "vinfoparam": vinfoparams}
  155. # print(params)
  156. res = requests.post("https://vd.l.qq.com/proxyhttp", headers=self.headers, json=params)
  157. data = res.json()
  158. return self.deal_data(data)
  159. if __name__ == '__main__':
  160. # 控制台执行 document.cookie 获取
  161. cookie = """
  162. pgv_pvid=5805499462; iip=0; RK=yQaYRyNLbG; ptcz=2a0d041daba2e1e3872184cd999e01bf90678c0e492c5900527c802251d224ad; tvfe_boss_uuid=53b5e88a3ebeba2c; ts_uid=8123938908; tvfe_search_uid=225c6955-d257-4d4a-97d0-cc327ffea211; txv_boss_uuid=95755769-e482-3a22-3e9f-6f8d842da1a7; pgv_pvi=1381712896; logTrackKey=613d40c3fea04aafb45fc9642dd67b99; video_platform=2; main_login=qq; vqq_vuserid=1260982452; vqq_openid=406CA2296D6A3B970597D6CF1605B6B7; vqq_appid=101483052; qq_nick=feng; pgv_info=ssid=s1368217315; pac_uid=1_434857005; vversion_name=8.2.95; video_guid=3419ca23530808d22bb278e881e46647; video_omgid=3419ca23530808d22bb278e881e46647; _qpsvr_localtk=0.710205836458567; compared_guid=bc772040638cf0da; vqq_access_token=88AEE1A8BC32318537BC7D81586E44A4; o_cookie=434857005; qv_als=vzJJwNyUEiCeDF1UA11662524934RoBNzA==; video_bucketid=4; fqm_pvqid=0a2a19f9-b09d-48d2-835a-cee916bdb63f; fqm_sessionid=a2c625f7-98e9-4d69-adb4-ad82846832bc; uin=o0434857005; skey=@rZMv3mYSR; tab_experiment_str=8752038#9047927#8752037#9040406#9099387; bucket_id=9231009; last_refresh_time=1666604352564; last_refresh_vuserid=1260982452; ts_refer=m.v.qq.com/; qq_head=http://thirdqq.qlogo.cn/g?b=sdk&k=llMfAicCbslpBk4funDukzg&s=100&t=318; vqq_vusession=sj85gfjn1ZL5jGI_RW5lLA.N; ptag=m_v_qq_com|channel; tab_experiment_data=exp_id=9099387&status=1; ts_last=v.qq.com/x/cover/m441e3rjq9kwpsc.html
  163. """.strip()
  164. login_token = """
  165. vqq_appid=101483052; vqq_openid=406CA2296D6A3B970597D6CF1605B6B7; vqq_vuserid=1260982452;vqq_vusession=Xd5bba2m9ByWls30Vi2FSQ.N; vqq_refresh_token=2D85975A29FB056EB6F9A338CB8F1EF5;vqq_next_refresh_time=6530;vqq_access_token=88AEE1A8BC32318537BC7D81586E44A4;main_login=qq;
  166. """.strip()
  167. auth_token = """
  168. vqq_appid=101483052; vqq_openid=406CA2296D6A3B970597D6CF1605B6B7; vqq_vuserid=1260982452;vqq_vusession=
  169. """.strip()
  170. cookie = login_token
  171. print(cookie)
  172. # refresh_url = 'https://access.video.qq.com/user/auth_refresh?vappid=11059694&vsecret=fdf61a6be0aad57132bc5cdf78ac30145b6cd2c1470b0cfe&type=qq&g_tk=1698594290&g_vstk=270754686&g_actk=2412125&callback=jQuery191028559957521840595_1666665357793&_=1666665357794'
  173. # headers = {
  174. # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36",
  175. # "Referer":"https://v.qq.com/",
  176. # # "cookie":cookie,
  177. # "cookie":auth_token,
  178. # }
  179. # r = requests.get(refresh_url,headers=headers)
  180. # print(r.text)
  181. # 斗罗大陆
  182. url = 'https://v.qq.com/x/cover/m441e3rjq9kwpsc/c00442r6ry6.html'
  183. # 复仇者联盟
  184. # url = 'https://v.qq.com/x/cover/v2098lbuihuqs11/m00314jtw6k.html'
  185. vqq = tencent(url=url,cookie=cookie)
  186. vqq.start()