py_huya.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #coding=utf-8
  2. #!/usr/bin/python
  3. import sys
  4. sys.path.append('..')
  5. from base.spider import Spider
  6. import json
  7. import re
  8. import time
  9. import html
  10. import base64
  11. import hashlib
  12. import urllib.parse
  13. class Spider(Spider):
  14. def getName(self):
  15. return "虎牙"
  16. def init(self,extend=""):
  17. pass
  18. def isVideoFormat(self,url):
  19. pass
  20. def manualVideoCheck(self):
  21. pass
  22. def homeContent(self,filter):
  23. result = {}
  24. cateManual = {
  25. "音乐": "音乐",
  26. "一起看": "一起看",
  27. "三国杀": "三国杀",
  28. "网游竞技": "网游竞技"
  29. }
  30. classes = []
  31. for k in cateManual:
  32. classes.append({
  33. 'type_name': k,
  34. 'type_id': cateManual[k]
  35. })
  36. result['class'] = classes
  37. if (filter):
  38. result['filters'] = self.config['filter']
  39. return result
  40. def homeVideoContent(self):
  41. result = {}
  42. return result
  43. def categoryContent(self,tid,pg,filter,extend):
  44. result = {}
  45. url = 'http://live.yj1211.work/api/live/getRecommendByPlatformArea?platform=huya&size=20&area={0}&page={1}'.format(tid, pg)
  46. rsp = self.fetch(url)
  47. content = rsp.text
  48. jo = json.loads(content)
  49. videos = []
  50. vodList = jo['data']
  51. for vod in vodList:
  52. aid = (vod['roomId']).strip()
  53. title = vod['roomName'].strip()
  54. img = vod['roomPic'].strip()
  55. remark = (vod['ownerName']).strip()
  56. videos.append({
  57. "vod_id": aid,
  58. "vod_name": title,
  59. "vod_pic": img,
  60. "vod_remarks": remark
  61. })
  62. result['list'] = videos
  63. result['page'] = pg
  64. result['pagecount'] = 9999
  65. result['limit'] = 90
  66. result['total'] = 999999
  67. return result
  68. def detailContent(self,array):
  69. aid = array[0]
  70. url = 'https://www.huya.com/' + aid
  71. header = {
  72. 'Content-Type': 'application/x-www-form-urlencoded',
  73. 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36'
  74. }
  75. rsp = self.fetch(url, headers=header)
  76. streamInfo = re.findall(r'stream: ([\s\S]*?)\n', rsp.text)
  77. if (len(streamInfo) > 0):
  78. liveData = json.loads(streamInfo[0])
  79. else:
  80. streamInfo = re.findall(r'"stream": "([\s\S]*?)"', rsp.text)
  81. if (len(streamInfo) > 0):
  82. liveDataBase64 = streamInfo[0]
  83. liveData = json.loads(str(base64.b64decode(liveDataBase64), 'utf-8'))
  84. streamInfoList = liveData['data'][0]['gameStreamInfoList']
  85. vod = {
  86. "vod_id": aid,
  87. "vod_name": liveData['data'][0]['gameLiveInfo']['roomName'],
  88. "vod_pic": liveData['data'][0]['gameLiveInfo']['screenshot'],
  89. "type_name": liveData['data'][0]['gameLiveInfo']['gameFullName'],
  90. "vod_year": "",
  91. "vod_area": "",
  92. "vod_remarks": "",
  93. "vod_actor": "",
  94. "vod_director": "",
  95. "vod_content": ""
  96. }
  97. playUrl = ''
  98. playFrom = ''
  99. for streamInfo in streamInfoList:
  100. hls_url = streamInfo['sHlsUrl'] + '/' + streamInfo['sStreamName'] + '.' + streamInfo['sHlsUrlSuffix']
  101. srcAntiCode = html.unescape(streamInfo['sHlsAntiCode'])
  102. c = srcAntiCode.split('&')
  103. c = [i for i in c if i != '']
  104. n = {i.split('=')[0]: i.split('=')[1] for i in c}
  105. fm = urllib.parse.unquote(n['fm'])
  106. u = base64.b64decode(fm).decode('utf-8')
  107. hash_prefix = u.split('_')[0]
  108. ctype = n.get('ctype', '')
  109. txyp = n.get('txyp', '')
  110. fs = n.get('fs', '')
  111. t = n.get('t', '')
  112. seqid = str(int(time.time() * 1e3 + 1463993859134))
  113. wsTime = hex(int(time.time()) + 3600).replace('0x', '')
  114. hash = hashlib.md5('_'.join([hash_prefix, '1463993859134', streamInfo['sStreamName'], hashlib.md5((seqid + '|' + ctype + '|' + t).encode('utf-8')).hexdigest(), wsTime]).encode('utf-8')).hexdigest()
  115. ratio = ''
  116. purl = "{}?wsSecret={}&wsTime={}&seqid={}&ctype={}&ver=1&txyp={}&fs={}&ratio={}&u={}&t={}&sv=2107230339".format(hls_url, hash, wsTime, seqid, ctype, txyp, fs, ratio, '1463993859134', t)
  117. playUrl = playUrl + '{}${}#'.format(streamInfo['sCdnType'], purl)
  118. vod['vod_play_from'] = '虎牙直播'
  119. vod['vod_play_url'] = playUrl
  120. result = {
  121. 'list': [
  122. vod
  123. ]
  124. }
  125. return result
  126. def searchContent(self,key,quick):
  127. result = {}
  128. return result
  129. def playerContent(self,flag,id,vipFlags):
  130. result = {}
  131. url = id
  132. result["parse"] = 0
  133. result["playUrl"] = ''
  134. result["url"] = url
  135. result["header"] = {
  136. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
  137. }
  138. result["contentType"] = ''
  139. return result
  140. config = {
  141. "player": {},
  142. "filter": {}
  143. }
  144. header = {}
  145. config = {
  146. "player": {},
  147. "filter": {}
  148. }
  149. header = {}
  150. def localProxy(self,param):
  151. action = {
  152. 'url':'',
  153. 'header':'',
  154. 'param':'',
  155. 'type':'string',
  156. 'after':''
  157. }
  158. return [200, "video/MP2T", action, ""]