央视片库.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. import re
  10. class Spider(Spider): # 元类 默认的元类 type
  11. def getName(self):
  12. return "央视片库"
  13. def init(self,extend=""):
  14. print("============{0}============".format(extend))
  15. pass
  16. def isVideoFormat(self,url):
  17. pass
  18. def manualVideoCheck(self):
  19. pass
  20. def homeContent(self,filter):
  21. result = {}
  22. cateManual = {
  23. "电视剧": "电视剧",
  24. "动画片": "动画片",
  25. "纪录片": "纪录片"
  26. #"特别节目": "特别节目"
  27. }
  28. classes = []
  29. for k in cateManual:
  30. classes.append({
  31. 'type_name':k,
  32. 'type_id':cateManual[k]
  33. })
  34. result['class'] = classes
  35. if(filter):
  36. result['filters'] = self.config['filter']
  37. return result
  38. def homeVideoContent(self):
  39. result = {
  40. 'list':[]
  41. }
  42. return result
  43. def categoryContent(self,tid,pg,filter,extend):
  44. result = {}
  45. month = ""
  46. year = ""
  47. if 'month' in extend.keys():
  48. month = extend['month']
  49. if 'year' in extend.keys():
  50. year = extend['year']
  51. if year == '':
  52. month = ''
  53. prefix = year + month
  54. url="https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955899450127&area=&sc=&fc=%E5%8A%A8%E7%94%BB%E7%89%87&letter=&p={0}&n=24&serviceId=tvcctv&topv=1&t=json"
  55. if tid=="电视剧":
  56. url="https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955853485115&area=&sc=&fc=%E7%94%B5%E8%A7%86%E5%89%A7&year=&letter=&p={0}&n=24&serviceId=tvcctv&topv=1&t=json"
  57. elif tid=="纪录片":
  58. url="https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955924871139&fc=%E7%BA%AA%E5%BD%95%E7%89%87&channel=&sc=&year=&letter=&p={0}&n=24&serviceId=tvcctv&topv=1&t=json"
  59. elif tid=="4":
  60. url="https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955953877151&channel=&sc=&fc=%E7%89%B9%E5%88%AB%E8%8A%82%E7%9B%AE&bigday=&letter=&p={0}&n=24&serviceId=tvcctv&topv=1&t=json"
  61. suffix = ""
  62. jo = self.fetch(url.format(pg),headers=self.header).json()
  63. vodList=jo["data"]["list"]
  64. videos = []
  65. for vod in vodList:
  66. lastVideo =vod['url']
  67. brief=vod['brief']
  68. if len(brief) == 0:
  69. brief = ' '
  70. if len(lastVideo) == 0:
  71. lastVideo = '_'
  72. guid = tid+'###'+vod["title"]+'###'+lastVideo+'###'+vod['image']+'###'+brief
  73. title = vod["title"]
  74. img = vod['image']
  75. videos.append({
  76. "vod_id":guid,
  77. "vod_name":title,
  78. "vod_pic":img,
  79. "vod_remarks":''
  80. })
  81. result['list'] = videos
  82. result['page'] = pg
  83. result['pagecount'] = 9999
  84. result['limit'] = 90
  85. result['total'] = 999999
  86. return result
  87. def detailContent(self,array):
  88. aid = array[0].split('###')
  89. if aid[2].find("http")<0:
  90. return {}
  91. tid = aid[0]
  92. logo = aid[3]
  93. lastVideo = aid[2]
  94. title = aid[1]
  95. date = aid[0]
  96. if lastVideo == '_':
  97. return {}
  98. rsp = self.fetch(lastVideo)
  99. htmlTxt=rsp.text
  100. column_id = ""
  101. videoList = []
  102. patternTxt=r"'title':\s*'(.+?)',\n{0,1}\s*'img':\s*'(.+?)',\n{0,1}\s*'brief':\s*'(.+?)',\n{0,1}\s*'url':\s*'(.+?)'"
  103. titleIndex=0
  104. UrlIndex=3
  105. if tid=="电视剧" or tid=="纪录片":
  106. patternTxt=r"'title':\s*'(.+?)',\n{0,1}\s*'brief':\s*'(.+?)',\n{0,1}\s*'img':\s*'(.+?)',\n{0,1}\s*'url':\s*'(.+?)'"
  107. titleIndex=0
  108. UrlIndex=3
  109. elif tid=="特别节目":
  110. patternTxt=r'class="tp1"><a\s*href="(https://.+?)"\s*target="_blank"\s*title="(.+?)"></a></div>'
  111. titleIndex=1
  112. UrlIndex=0
  113. #https://api.cntv.cn/NewVideo/getVideoListByAlbumIdNew?id=VIDA3YcIusJ9mh4c9mw5XHyx230113&serviceId=tvcctv//由于方式不同暂时不做
  114. pattern = re.compile(patternTxt)
  115. ListRe=pattern.findall(htmlTxt)
  116. for value in ListRe:
  117. videoList.append(value[titleIndex]+"$"+value[UrlIndex])
  118. if len(videoList) == 0:
  119. return {}
  120. vod = {
  121. "vod_id":array[0],
  122. "vod_name":title,
  123. "vod_pic":logo,
  124. "type_name":tid,
  125. "vod_year":date,
  126. "vod_area":"",
  127. "vod_remarks":date,
  128. "vod_actor":"",
  129. "vod_director":column_id,
  130. "vod_content":aid[4]
  131. }
  132. vod['vod_play_from'] = 'CCTV'
  133. vod['vod_play_url'] = "#".join(videoList)
  134. result = {
  135. 'list':[
  136. vod
  137. ]
  138. }
  139. return result
  140. def searchContent(self,key,quick):
  141. result = {
  142. 'list':[]
  143. }
  144. return result
  145. def playerContent(self,flag,id,vipFlags):
  146. result = {}
  147. rsp = self.fetch(id)
  148. htmlTxt=rsp.text
  149. pattern = re.compile(r'var\sguid\s*=\s*"(.+?)";')
  150. ListRe=pattern.findall(htmlTxt)
  151. if ListRe==[]:
  152. return result
  153. url = "https://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid={0}".format(ListRe[0])
  154. jo = self.fetch(url,headers=self.header).json()
  155. link = jo['hls_url'].strip()
  156. rsp = self.fetch(link,headers=self.header)
  157. content = rsp.text.strip()
  158. arr = content.split('\n')
  159. urlPrefix = self.regStr(link,'(http[s]?://[a-zA-z0-9.]+)/')
  160. subUrl = arr[-1].split('/')
  161. subUrl[3] = '1200'
  162. subUrl[-1] = '1200.m3u8'
  163. hdUrl = urlPrefix + '/'.join(subUrl)
  164. url = urlPrefix + arr[-1]
  165. hdRsp = self.fetch(hdUrl,headers=self.header)
  166. if hdRsp.status_code == 200:
  167. url = hdUrl
  168. result["parse"] = 0
  169. result["playUrl"] = ''
  170. result["url"] = url
  171. result["header"] = ''
  172. return result
  173. config = {
  174. "player": {},
  175. "filter": {"CCTV":[{"key":"cid","name":"频道","value":[{"n":"全部","v":""},{"n":"CCTV-1综合","v":"EPGC1386744804340101"},{"n":"CCTV-2财经","v":"EPGC1386744804340102"},{"n":"CCTV-3综艺","v":"EPGC1386744804340103"},{"n":"CCTV-4中文国际","v":"EPGC1386744804340104"},{"n":"CCTV-5体育","v":"EPGC1386744804340107"},{"n":"CCTV-6电影","v":"EPGC1386744804340108"},{"n":"CCTV-7国防军事","v":"EPGC1386744804340109"},{"n":"CCTV-8电视剧","v":"EPGC1386744804340110"},{"n":"CCTV-9纪录","v":"EPGC1386744804340112"},{"n":"CCTV-10科教","v":"EPGC1386744804340113"},{"n":"CCTV-11戏曲","v":"EPGC1386744804340114"},{"n":"CCTV-12社会与法","v":"EPGC1386744804340115"},{"n":"CCTV-13新闻","v":"EPGC1386744804340116"},{"n":"CCTV-14少儿","v":"EPGC1386744804340117"},{"n":"CCTV-15音乐","v":"EPGC1386744804340118"},{"n":"CCTV-16奥林匹克","v":"EPGC1634630207058998"},{"n":"CCTV-17农业农村","v":"EPGC1563932742616872"},{"n":"CCTV-5+体育赛事","v":"EPGC1468294755566101"}]},{"key":"fc","name":"分类","value":[{"n":"全部","v":""},{"n":"新闻","v":"新闻"},{"n":"体育","v":"体育"},{"n":"综艺","v":"综艺"},{"n":"健康","v":"健康"},{"n":"生活","v":"生活"},{"n":"科教","v":"科教"},{"n":"经济","v":"经济"},{"n":"农业","v":"农业"},{"n":"法治","v":"法治"},{"n":"军事","v":"军事"},{"n":"少儿","v":"少儿"},{"n":"动画","v":"动画"},{"n":"纪实","v":"纪实"},{"n":"戏曲","v":"戏曲"},{"n":"音乐","v":"音乐"},{"n":"影视","v":"影视"}]},{"key":"fl","name":"字母","value":[{"n":"全部","v":""},{"n":"A","v":"A"},{"n":"B","v":"B"},{"n":"C","v":"C"},{"n":"D","v":"D"},{"n":"E","v":"E"},{"n":"F","v":"F"},{"n":"G","v":"G"},{"n":"H","v":"H"},{"n":"I","v":"I"},{"n":"J","v":"J"},{"n":"K","v":"K"},{"n":"L","v":"L"},{"n":"M","v":"M"},{"n":"N","v":"N"},{"n":"O","v":"O"},{"n":"P","v":"P"},{"n":"Q","v":"Q"},{"n":"R","v":"R"},{"n":"S","v":"S"},{"n":"T","v":"T"},{"n":"U","v":"U"},{"n":"V","v":"V"},{"n":"W","v":"W"},{"n":"X","v":"X"},{"n":"Y","v":"Y"},{"n":"Z","v":"Z"}]},{"key":"year","name":"年份","value":[{"n":"全部","v":""},{"n":"2022","v":"2022"},{"n":"2021","v":"2021"},{"n":"2020","v":"2020"},{"n":"2019","v":"2019"},{"n":"2018","v":"2018"},{"n":"2017","v":"2017"},{"n":"2016","v":"2016"},{"n":"2015","v":"2015"},{"n":"2014","v":"2014"},{"n":"2013","v":"2013"},{"n":"2012","v":"2012"},{"n":"2011","v":"2011"},{"n":"2010","v":"2010"},{"n":"2009","v":"2009"},{"n":"2008","v":"2008"},{"n":"2007","v":"2007"},{"n":"2006","v":"2006"},{"n":"2005","v":"2005"},{"n":"2004","v":"2004"},{"n":"2003","v":"2003"},{"n":"2002","v":"2002"},{"n":"2001","v":"2001"},{"n":"2000","v":"2000"}]},{"key":"month","name":"月份","value":[{"n":"全部","v":""},{"n":"12","v":"12"},{"n":"11","v":"11"},{"n":"10","v":"10"},{"n":"09","v":"09"},{"n":"08","v":"08"},{"n":"07","v":"07"},{"n":"06","v":"06"},{"n":"05","v":"05"},{"n":"04","v":"04"},{"n":"03","v":"03"},{"n":"02","v":"02"},{"n":"01","v":"01"}]}]}
  176. }
  177. header = {
  178. "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.54 Safari/537.36",
  179. "Origin": "https://tv.cctv.com",
  180. "Referer": "https://tv.cctv.com/"
  181. }
  182. def localProxy(self,param):
  183. return [200, "video/MP2T", action, ""]