搭讪.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 time
  8. import base64
  9. class Spider(Spider): # 元类 默认的元类 type
  10. def getName(self):
  11. return "搭讪"
  12. def init(self,extend=""):
  13. print("============{0}============".format(extend))
  14. pass
  15. def isVideoFormat(self,url):
  16. pass
  17. def manualVideoCheck(self):
  18. pass
  19. def homeContent(self,filter):
  20. result = {}
  21. cateManual = {
  22. "美女搭讪":"美女搭讪",
  23. "搭讪技巧":"搭讪技巧",
  24. "女追男":"女追男",
  25. "男追女":"男追女",
  26. "街头搭讪":"街头搭讪",
  27. "夜店搭讪":"夜店搭讪",
  28. "商超搭讪":"商超搭讪",
  29. "校园搭讪":"校园搭讪",
  30. "搭讪失败":"搭讪失败"
  31. }
  32. classes = []
  33. for k in cateManual:
  34. classes.append({
  35. 'type_name':k,
  36. 'type_id':cateManual[k]
  37. })
  38. result['class'] = classes
  39. if(filter):
  40. result['filters'] = self.config['filter']
  41. return result
  42. def homeVideoContent(self):
  43. result = {
  44. 'list':[]
  45. }
  46. return result
  47. cookies = ''
  48. def getCookie(self):
  49. import requests
  50. import http.cookies
  51. # 这里填cookie
  52. raw_cookie_line = "innersign=0;buvid3=E0C6010B-08CF-AE9C-FD18-0EC14F2AA35429667infoc; b_lsid=D101097FB2_187C825C5D7; _uuid=29167C75-10922-A764-5757-EC106517B471831442infoc; buvid4=59789C83-B64B-148E-A8FD-585BB3D0300830827-123042821-WOiBDeHIjjEhSRIFCi5kfQ%3D%3D; buvid_fp=3ee98313cd55b76fe3cb63a36d9d41c9; b_nut=100; SESSDATA=4fe90c6b%2C1698242168%2C30912%2A41; bili_jct=faffed7de17218860f3aa958182ca2ee; DedeUserID=516036434; DedeUserID__ckMd5=4e5107a4bc891865; sid=q2dc0y62"
  53. simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
  54. cookie_jar = requests.cookies.RequestsCookieJar()
  55. cookie_jar.update(simple_cookie)
  56. return cookie_jar
  57. def get_dynamic(self,pg):
  58. result = {}
  59. url= 'https://api.bilibili.com/x/polymer/web-dynamic/v1/feed/all?timezone_offset=-480&type=all&page={0}'.format(pg)
  60. rsp = self.fetch(url,cookies=self.getCookie())
  61. content = rsp.text
  62. jo = json.loads(content)
  63. if jo['code'] == 0:
  64. videos = []
  65. vodList = jo['data']['items']
  66. for vod in vodList:
  67. if vod['type'] == 'DYNAMIC_TYPE_AV':
  68. ivod = vod['modules']['module_dynamic']['major']['archive']
  69. aid = str(ivod['aid']).strip()
  70. title = ivod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  71. img = ivod['cover'].strip()
  72. remark = str(ivod['duration_text']).strip()
  73. videos.append({
  74. "vod_id":aid,
  75. "vod_name":title,
  76. "vod_pic":img,
  77. "vod_remarks":remark
  78. })
  79. result['list'] = videos
  80. result['page'] = pg
  81. result['pagecount'] = 9999
  82. result['limit'] = 90
  83. result['total'] = 999999
  84. return result
  85. def get_hot(self,pg):
  86. result = {}
  87. url= 'https://api.bilibili.com/x/web-interface/popular?ps=20&pn={0}'.format(pg)
  88. rsp = self.fetch(url,cookies=self.getCookie())
  89. content = rsp.text
  90. jo = json.loads(content)
  91. if jo['code'] == 0:
  92. videos = []
  93. vodList = jo['data']['list']
  94. for vod in vodList:
  95. aid = str(vod['aid']).strip()
  96. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  97. img = vod['pic'].strip()
  98. remark = str(vod['duration']).strip()
  99. videos.append({
  100. "vod_id":aid,
  101. "vod_name":title,
  102. "vod_pic":img,
  103. "vod_remarks":remark
  104. })
  105. result['list'] = videos
  106. result['page'] = pg
  107. result['pagecount'] = 9999
  108. result['limit'] = 90
  109. result['total'] = 999999
  110. return result
  111. def get_rank(self):
  112. result = {}
  113. url= 'https://api.bilibili.com/x/web-interface/ranking/v2?rid=0&type=all'
  114. rsp = self.fetch(url,cookies=self.getCookie())
  115. content = rsp.text
  116. jo = json.loads(content)
  117. if jo['code'] == 0:
  118. videos = []
  119. vodList = jo['data']['list']
  120. for vod in vodList:
  121. aid = str(vod['aid']).strip()
  122. title = vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  123. img = vod['pic'].strip()
  124. remark = str(vod['duration']).strip()
  125. videos.append({
  126. "vod_id":aid,
  127. "vod_name":title,
  128. "vod_pic":img,
  129. "vod_remarks":remark
  130. })
  131. result['list'] = videos
  132. result['page'] = 1
  133. result['pagecount'] = 1
  134. result['limit'] = 90
  135. result['total'] = 999999
  136. return result
  137. def categoryContent(self,tid,pg,filter,extend):
  138. result = {}
  139. if tid == "热门":
  140. return self.get_hot(pg=pg)
  141. if tid == "排行榜" :
  142. return self.get_rank()
  143. if tid == '动态':
  144. return self.get_dynamic(pg=pg)
  145. url = 'https://api.bilibili.com/x/web-interface/search/type?search_type=video&keyword={0}&page={1}'.format(tid,pg)
  146. if len(self.cookies) <= 0:
  147. self.getCookie()
  148. rsp = self.fetch(url,cookies=self.getCookie())
  149. content = rsp.text
  150. jo = json.loads(content)
  151. if jo['code'] != 0:
  152. rspRetry = self.fetch(url,cookies=self.getCookie())
  153. content = rspRetry.text
  154. jo = json.loads(content)
  155. videos = []
  156. vodList = jo['data']['result']
  157. for vod in vodList:
  158. aid = str(vod['aid']).strip()
  159. title = tid + ":" + vod['title'].strip().replace("<em class=\"keyword\">","").replace("</em>","")
  160. img = 'https:' + vod['pic'].strip()
  161. remark = str(vod['duration']).strip()
  162. videos.append({
  163. "vod_id":aid,
  164. "vod_name":title,
  165. "vod_pic":img,
  166. "vod_remarks":remark
  167. })
  168. result['list'] = videos
  169. result['page'] = pg
  170. result['pagecount'] = 9999
  171. result['limit'] = 90
  172. result['total'] = 999999
  173. return result
  174. def cleanSpace(self,str):
  175. return str.replace('\n','').replace('\t','').replace('\r','').replace(' ','')
  176. def detailContent(self,array):
  177. aid = array[0]
  178. url = "https://api.bilibili.com/x/web-interface/view?aid={0}".format(aid)
  179. rsp = self.fetch(url,headers=self.header,cookies=self.getCookie())
  180. jRoot = json.loads(rsp.text)
  181. jo = jRoot['data']
  182. title = jo['title'].replace("<em class=\"keyword\">","").replace("</em>","")
  183. pic = jo['pic']
  184. desc = jo['desc']
  185. typeName = jo['tname']
  186. vod = {
  187. "vod_id":aid,
  188. "vod_name":title,
  189. "vod_pic":pic,
  190. "type_name":typeName,
  191. "vod_year":"",
  192. "vod_area":"bilidanmu",
  193. "vod_remarks":"",
  194. "vod_actor":jo['owner']['name'],
  195. "vod_director":jo['owner']['name'],
  196. "vod_content":desc
  197. }
  198. ja = jo['pages']
  199. playUrl = ''
  200. for tmpJo in ja:
  201. cid = tmpJo['cid']
  202. part = tmpJo['part']
  203. playUrl = playUrl + '{0}${1}_{2}#'.format(part,aid,cid)
  204. vod['vod_play_from'] = 'B站'
  205. vod['vod_play_url'] = playUrl
  206. result = {
  207. 'list':[
  208. vod
  209. ]
  210. }
  211. return result
  212. def searchContent(self,key,quick):
  213. search = self.categoryContent(tid=key,pg=1,filter=None,extend=None)
  214. result = {
  215. 'list':search['list']
  216. }
  217. return result
  218. def playerContent(self,flag,id,vipFlags):
  219. # https://www.555dianying.cc/vodplay/static/js/playerconfig.js
  220. result = {}
  221. ids = id.split("_")
  222. url = 'https://api.bilibili.com:443/x/player/playurl?avid={0}&cid=%20%20{1}&qn=112'.format(ids[0],ids[1])
  223. rsp = self.fetch(url,cookies=self.getCookie())
  224. jRoot = json.loads(rsp.text)
  225. jo = jRoot['data']
  226. ja = jo['durl']
  227. maxSize = -1
  228. position = -1
  229. for i in range(len(ja)):
  230. tmpJo = ja[i]
  231. if maxSize < int(tmpJo['size']):
  232. maxSize = int(tmpJo['size'])
  233. position = i
  234. url = ''
  235. if len(ja) > 0:
  236. if position == -1:
  237. position = 0
  238. url = ja[position]['url']
  239. result["parse"] = 0
  240. result["playUrl"] = ''
  241. result["url"] = url
  242. result["header"] = {
  243. "Referer":"https://www.bilibili.com",
  244. "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"
  245. }
  246. result["contentType"] = 'video/x-flv'
  247. return result
  248. config = {
  249. "player": {},
  250. "filter": {}
  251. }
  252. header = {}
  253. def localProxy(self,param):
  254. return [200, "video/MP2T", action, ""]