cctv_spider.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. # coding=utf-8
  2. # !/usr/bin/python
  3. import os.path
  4. import sys
  5. sys.path.append('..')
  6. try:
  7. # from base.spider import Spider as BaseSpider
  8. from base.spider import BaseSpider
  9. except ImportError:
  10. from t4.base.spider import BaseSpider
  11. import json
  12. import time
  13. import base64
  14. import re
  15. from urllib import request, parse
  16. from pathlib import Path
  17. import urllib
  18. import urllib.request
  19. """
  20. 配置示例:
  21. t4的配置里ext节点会自动变成api对应query参数extend,但t4的ext字符串不支持路径格式,比如./开头或者.json结尾
  22. api里会自动含有ext参数是base64编码后的选中的筛选条件
  23. 错误示例,ext含有json:
  24. {
  25. "key":"hipy_cctv",
  26. "name":"hipy_cctv",
  27. "type":4,
  28. "api":"http://192.168.31.49:5707/api/v1/vod/cctv_spider?api_ext={{host}}/txt/hipy/cctv_spider.json",
  29. "searchable":1,
  30. "quickSearch":1,
  31. "filterable":1,
  32. "ext":"cctv_spider.json"
  33. }
  34. 正确示例。同时存在ext和api_ext会优先取ext作为extend加载init
  35. {
  36. "key":"hipy_t4_cctv",
  37. "name":"cctv(hipy_t4)",
  38. "type":4,
  39. "api":"http://192.168.31.49:5707/api/v1/vod/cctv_spider?api_ext={{host}}/txt/hipy/cctv_spider.json",
  40. "searchable":1,
  41. "quickSearch":1,
  42. "filterable":1,
  43. "ext":"cctv_spider"
  44. }
  45. {
  46. "key": "hipy_t3_cctv",
  47. "name": "cctv(hipy_t3)",
  48. "type": 3,
  49. "api": "{{host}}/txt/hipy/cctv_spider.py",
  50. "searchable": 1,
  51. "quickSearch": 1,
  52. "filterable": 1,
  53. "ext": "{{host}}/txt/hipy/cctv_spider.json"
  54. }
  55. """
  56. class Spider(BaseSpider): # 元类 默认的元类 type
  57. module = None
  58. def getDependence(self):
  59. return ['base_spider']
  60. def getName(self):
  61. return "中央电视台" # 可搜索
  62. def init_api_ext_file(self):
  63. ext_file = __file__.replace('.py', '.json')
  64. print(f'ext_file:{ext_file}')
  65. # 特别节目网页: https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65
  66. # 特别节目分类筛选获取页面: https://tv.cctv.com/yxg/tbjm/index.shtml
  67. # 纪录片网页: https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65
  68. # 纪录片分类筛选获取页面:https://tv.cctv.com/yxg/jlp/index.shtml
  69. # ==================== 获取特别节目的筛选条件 ======================
  70. r = self.fetch('https://tv.cctv.com/yxg/tbjm/index.shtml')
  71. html = r.text
  72. html = self.html(html)
  73. filter_tbjm = []
  74. lis = html.xpath('//*[@id="pindao"]/li')
  75. li_value = []
  76. for li in lis:
  77. li_value.append({
  78. 'n': ''.join(li.xpath('./span//text()')),
  79. 'v': ''.join(li.xpath('@datacd')),
  80. })
  81. # print(li_value)
  82. filter_tbjm.append({
  83. "key": "datapd-channel",
  84. "name": "频道",
  85. "value": li_value
  86. })
  87. lis = html.xpath('//*[@id="fenlei"]/li')
  88. li_value = []
  89. for li in lis:
  90. li_value.append({
  91. 'n': ''.join(li.xpath('./span//text()')),
  92. 'v': ''.join(li.xpath('@datalx')),
  93. })
  94. # print(li_value)
  95. filter_tbjm.append({
  96. "key": "datafl-sc",
  97. "name": "类型",
  98. "value": li_value
  99. })
  100. lis = html.xpath('//*[@id="zimu"]/li')
  101. li_value = []
  102. for li in lis:
  103. li_value.append({
  104. 'n': ''.join(li.xpath('./span//text()')),
  105. 'v': ''.join(li.xpath('@datazm')),
  106. })
  107. # print(li_value)
  108. filter_tbjm.append({
  109. "key": "dataszm-letter",
  110. "name": "首字母",
  111. "value": li_value
  112. })
  113. print(filter_tbjm)
  114. # ==================== 纪录片筛选获取 ======================
  115. r = self.fetch('https://tv.cctv.com/yxg/jlp/index.shtml')
  116. html = r.text
  117. html = self.html(html)
  118. filter_jlp = []
  119. lis = html.xpath('//*[@id="pindao"]/li')
  120. li_value = []
  121. for li in lis:
  122. li_value.append({
  123. 'n': ''.join(li.xpath('./span//text()')),
  124. 'v': ''.join(li.xpath('@datacd')),
  125. })
  126. # print(li_value)
  127. filter_jlp.append({
  128. "key": "datapd-channel",
  129. "name": "频道",
  130. "value": li_value
  131. })
  132. lis = html.xpath('//*[@id="fenlei"]/li')
  133. li_value = []
  134. for li in lis:
  135. li_value.append({
  136. 'n': ''.join(li.xpath('./span//text()')),
  137. 'v': ''.join(li.xpath('@datalx')),
  138. })
  139. # print(li_value)
  140. filter_jlp.append({
  141. "key": "datafl-sc",
  142. "name": "类型",
  143. "value": li_value
  144. })
  145. lis = html.xpath('//*[@id="nianfen"]/li')
  146. li_value = []
  147. for li in lis:
  148. li_value.append({
  149. 'n': ''.join(li.xpath('./span//text()')),
  150. 'v': ''.join(li.xpath('@datanf')),
  151. })
  152. # print(li_value)
  153. filter_jlp.append({
  154. "key": "datanf-year",
  155. "name": "年份",
  156. "value": li_value
  157. })
  158. lis = html.xpath('//*[@id="zimu"]/li')
  159. li_value = []
  160. for li in lis:
  161. li_value.append({
  162. 'n': ''.join(li.xpath('./span//text()')),
  163. 'v': ''.join(li.xpath('@datazm')),
  164. })
  165. # print(li_value)
  166. filter_jlp.append({
  167. "key": "dataszm-letter",
  168. "name": "首字母",
  169. "value": li_value
  170. })
  171. print(filter_jlp)
  172. ext_file_dict = {
  173. "特别节目": filter_tbjm,
  174. "纪录片": filter_jlp,
  175. }
  176. # print(json.dumps(ext_file_dict,ensure_ascii=False,indent=4))
  177. with open(ext_file, mode='w+', encoding='utf-8') as f:
  178. # f.write(json.dumps(ext_file_dict,ensure_ascii=False,indent=4))
  179. f.write(json.dumps(ext_file_dict, ensure_ascii=False))
  180. def init(self, extend=""):
  181. def init_file(ext_file):
  182. ext_file = Path(ext_file).as_posix()
  183. # print(f'ext_file:{ext_file}')
  184. if os.path.exists(ext_file):
  185. # print('存在扩展文件')
  186. with open(ext_file, mode='r', encoding='utf-8') as f:
  187. try:
  188. ext_dict = json.loads(f.read())
  189. # print(ext_dict)
  190. self.config['filter'].update(ext_dict)
  191. except Exception as e:
  192. print(f'更新扩展筛选条件发生错误:{e}')
  193. print("============依赖列表:{0}============".format(extend))
  194. ext = self.extend
  195. print("============ext:{0}============".format(ext))
  196. if isinstance(ext, str) and ext:
  197. if ext.startswith('./'):
  198. ext_file = os.path.join(os.path.dirname(__file__), ext)
  199. init_file(ext_file)
  200. elif ext.startswith('http'):
  201. try:
  202. r = self.fetch(ext)
  203. self.config['filter'].update(r.json())
  204. except Exception as e:
  205. print(f'更新扩展筛选条件发生错误:{e}')
  206. elif not ext.startswith('./') and not ext.startswith('http'):
  207. ext_file = os.path.join(os.path.dirname(__file__), './' + ext + '.json')
  208. init_file(ext_file)
  209. # 装载模块,这里只要一个就够了
  210. if isinstance(extend, list):
  211. for lib in extend:
  212. if '.Spider' in str(type(lib)):
  213. self.module = lib
  214. break
  215. def isVideoFormat(self, url):
  216. pass
  217. def manualVideoCheck(self):
  218. pass
  219. def homeContent(self, filter):
  220. result = {}
  221. cateManual = {
  222. "栏目大全": "栏目大全",
  223. "特别节目": "特别节目",
  224. "纪录片": "纪录片",
  225. "电视剧": "电视剧",
  226. "动画片": "动画片"
  227. }
  228. classes = []
  229. for k in cateManual:
  230. classes.append({
  231. 'type_name': k,
  232. 'type_id': cateManual[k]
  233. })
  234. result['class'] = classes
  235. if (filter):
  236. result['filters'] = self.config['filter']
  237. return result
  238. def homeVideoContent(self):
  239. result = {
  240. 'list': []
  241. }
  242. if self.module:
  243. result = self.module.homeVideoContent()
  244. return result
  245. def categoryContent(self, tid, pg, filter, extend):
  246. result = {}
  247. month = "" # 月
  248. year = "" # 年
  249. area = '' # 地区
  250. channel = '' # 频道
  251. datafl = '' # 类型
  252. letter = '' # 字母
  253. pagecount = 24
  254. if tid == '动画片':
  255. id = urllib.parse.quote(tid)
  256. if 'datadq-area' in extend.keys():
  257. area = urllib.parse.quote(extend['datadq-area'])
  258. if 'dataszm-letter' in extend.keys():
  259. letter = extend['dataszm-letter']
  260. if 'datafl-sc' in extend.keys():
  261. datafl = urllib.parse.quote(extend['datafl-sc'])
  262. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955899450127&area={0}&sc={4}&fc={1}&letter={2}&p={3}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  263. area, id, letter, pg, datafl)
  264. elif tid == '纪录片':
  265. id = urllib.parse.quote(tid)
  266. if 'datapd-channel' in extend.keys():
  267. channel = urllib.parse.quote(extend['datapd-channel'])
  268. if 'datafl-sc' in extend.keys():
  269. datafl = urllib.parse.quote(extend['datafl-sc'])
  270. if 'datanf-year' in extend.keys():
  271. year = extend['datanf-year']
  272. if 'dataszm-letter' in extend.keys():
  273. letter = extend['dataszm-letter']
  274. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955924871139&fc={0}&channel={1}&sc={2}&year={3}&letter={4}&p={5}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  275. id, channel, datafl, year, letter, pg)
  276. elif tid == '电视剧':
  277. id = urllib.parse.quote(tid)
  278. if 'datafl-sc' in extend.keys():
  279. datafl = urllib.parse.quote(extend['datafl-sc'])
  280. if 'datanf-year' in extend.keys():
  281. year = extend['datanf-year']
  282. if 'dataszm-letter' in extend.keys():
  283. letter = extend['dataszm-letter']
  284. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955853485115&area={0}&sc={1}&fc={2}&year={3}&letter={4}&p={5}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  285. area, datafl, id, year, letter, pg)
  286. elif tid == '特别节目':
  287. id = urllib.parse.quote(tid)
  288. if 'datapd-channel' in extend.keys():
  289. channel = urllib.parse.quote(extend['datapd-channel'])
  290. if 'datafl-sc' in extend.keys():
  291. datafl = urllib.parse.quote(extend['datafl-sc'])
  292. if 'dataszm-letter' in extend.keys():
  293. letter = extend['dataszm-letter']
  294. url = 'https://api.cntv.cn/list/getVideoAlbumList?channelid=CHAL1460955953877151&channel={0}&sc={1}&fc={2}&bigday=&letter={3}&p={4}&n=24&serviceId=tvcctv&topv=1&t=json'.format(
  295. channel, datafl, id, letter, pg)
  296. elif tid == '栏目大全':
  297. cid = '' # 频道
  298. if 'cid' in extend.keys():
  299. cid = extend['cid']
  300. fc = '' # 分类
  301. if 'fc' in extend.keys():
  302. fc = extend['fc']
  303. fl = '' # 字母
  304. if 'fl' in extend.keys():
  305. fl = extend['fl']
  306. url = 'https://api.cntv.cn/lanmu/columnSearch?&fl={0}&fc={1}&cid={2}&p={3}&n=20&serviceId=tvcctv&t=json&cb=ko'.format(
  307. fl, fc, cid, pg)
  308. pagecount = 20
  309. else:
  310. url = 'https://tv.cctv.com/epg/index.shtml'
  311. videos = []
  312. htmlText = self.fetch(url).text
  313. if tid == '栏目大全':
  314. index = htmlText.rfind(');')
  315. if index > -1:
  316. htmlText = htmlText[3:index]
  317. videos = self.get_list1(html=htmlText, tid=tid)
  318. else:
  319. videos = self.get_list(html=htmlText, tid=tid)
  320. # print(videos)
  321. result['list'] = videos
  322. result['page'] = pg
  323. result['pagecount'] = 9999 if len(videos) >= pagecount else pg
  324. result['limit'] = 90
  325. result['total'] = 999999
  326. return result
  327. def detailContent(self, array):
  328. result = {}
  329. aid = array[0].split('||')
  330. tid = aid[0]
  331. logo = aid[3]
  332. lastVideo = aid[2]
  333. title = aid[1]
  334. id = aid[4]
  335. vod_year = aid[5]
  336. actors = aid[6] if len(aid) > 6 else ''
  337. brief = aid[7] if len(aid) > 7 else '' # get请求最长255,这个描述会有可能直接被干没了。
  338. fromId = 'CCTV'
  339. if tid == "栏目大全":
  340. lastUrl = 'https://api.cntv.cn/video/videoinfoByGuid?guid={0}&serviceId=tvcctv'.format(id)
  341. # htmlTxt = self.webReadFile(urlStr=lastUrl, header=self.header)
  342. htmlTxt = self.fetch(lastUrl).text
  343. topicId = json.loads(htmlTxt)['ctid']
  344. Url = "https://api.cntv.cn/NewVideo/getVideoListByColumn?id={0}&d=&p=1&n=100&sort=desc&mode=0&serviceId=tvcctv&t=json".format(
  345. topicId)
  346. # htmlTxt = self.webReadFile(urlStr=Url, header=self.header)
  347. htmlTxt = self.fetch(Url).text
  348. else:
  349. Url = 'https://api.cntv.cn/NewVideo/getVideoListByAlbumIdNew?id={0}&serviceId=tvcctv&p=1&n=100&mode=0&pub=1'.format(
  350. id)
  351. jRoot = ''
  352. videoList = []
  353. try:
  354. if tid == "搜索":
  355. fromId = '中央台'
  356. videoList = [title + "$" + lastVideo]
  357. else:
  358. # htmlTxt = self.webReadFile(urlStr=Url, header=self.header)
  359. htmlTxt = self.fetch(Url).text
  360. jRoot = json.loads(htmlTxt)
  361. data = jRoot['data']
  362. jsonList = data['list']
  363. videoList = self.get_EpisodesList(jsonList=jsonList)
  364. if len(videoList) < 1:
  365. # htmlTxt = self.webReadFile(urlStr=lastVideo, header=self.header)
  366. htmlTxt = self.fetch(lastVideo).text
  367. if tid == "电视剧" or tid == "纪录片":
  368. patternTxt = r"'title':\s*'(?P<title>.+?)',\n{0,1}\s*'brief':\s*'(.+?)',\n{0,1}\s*'img':\s*'(.+?)',\n{0,1}\s*'url':\s*'(?P<url>.+?)'"
  369. elif tid == "特别节目":
  370. patternTxt = r'class="tp1"><a\s*href="(?P<url>https://.+?)"\s*target="_blank"\s*title="(?P<title>.+?)"></a></div>'
  371. elif tid == "动画片":
  372. patternTxt = r"'title':\s*'(?P<title>.+?)',\n{0,1}\s*'img':\s*'(.+?)',\n{0,1}\s*'brief':\s*'(.+?)',\n{0,1}\s*'url':\s*'(?P<url>.+?)'"
  373. elif tid == "栏目大全":
  374. patternTxt = r'href="(?P<url>.+?)" target="_blank" alt="(?P<title>.+?)" title=".+?">'
  375. videoList = self.get_EpisodesList_re(htmlTxt=htmlTxt, patternTxt=patternTxt)
  376. fromId = '央视'
  377. except:
  378. pass
  379. if len(videoList) == 0:
  380. return {}
  381. vod = {
  382. "vod_id": array[0],
  383. "vod_name": title,
  384. "vod_pic": logo,
  385. "type_name": tid,
  386. "vod_year": vod_year,
  387. "vod_area": "",
  388. "vod_remarks": '',
  389. "vod_actor": actors,
  390. "vod_director": '',
  391. "vod_content": brief
  392. }
  393. vod['vod_play_from'] = fromId
  394. vod['vod_play_url'] = "#".join(videoList)
  395. result = {
  396. 'list': [
  397. vod
  398. ]
  399. }
  400. return result
  401. def get_lineList(self, Txt, mark, after):
  402. circuit = []
  403. origin = Txt.find(mark)
  404. while origin > 8:
  405. end = Txt.find(after, origin)
  406. circuit.append(Txt[origin:end])
  407. origin = Txt.find(mark, end)
  408. return circuit
  409. def get_RegexGetTextLine(self, Text, RegexText, Index):
  410. returnTxt = []
  411. pattern = re.compile(RegexText, re.M | re.S)
  412. ListRe = pattern.findall(Text)
  413. if len(ListRe) < 1:
  414. return returnTxt
  415. for value in ListRe:
  416. returnTxt.append(value)
  417. return returnTxt
  418. def searchContent(self, key, quick, pg=1):
  419. key = urllib.parse.quote(key)
  420. Url = 'https://search.cctv.com/ifsearch.php?page=1&qtext={0}&sort=relevance&pageSize=20&type=video&vtime=-1&datepid=1&channel=&pageflag=0&qtext_str={0}'.format(
  421. key)
  422. # htmlTxt = self.webReadFile(urlStr=Url, header=self.header)
  423. htmlTxt = self.fetch(Url).text
  424. videos = self.get_list_search(html=htmlTxt, tid='搜索')
  425. result = {
  426. 'list': videos
  427. }
  428. return result
  429. def playerContent(self, flag, id, vipFlags):
  430. result = {}
  431. url = ''
  432. parse = 0
  433. headers = {
  434. 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
  435. }
  436. if flag == 'CCTV':
  437. url = self.get_m3u8(urlTxt=id)
  438. else:
  439. try:
  440. # htmlTxt = self.webReadFile(urlStr=id, header=self.header)
  441. htmlTxt = self.fetch(id).text
  442. guid = self.get_RegexGetText(Text=htmlTxt, RegexText=r'var\sguid\s*=\s*"(.+?)";', Index=1)
  443. url = self.get_m3u8(urlTxt=guid)
  444. except:
  445. url = id
  446. parse = 1
  447. if url.find('https:') < 0:
  448. url = id
  449. parse = 1
  450. result["parse"] = parse # 1=嗅探,0=播放
  451. result["playUrl"] = ''
  452. result["url"] = url
  453. result["header"] = headers
  454. return result
  455. # 分类抓取地址:
  456. # 栏目大全:https://tv.cctv.com/lm/index.shtml?spm=C28340.Pu9TN9YUsfNZ.E2PQtIunpEaz.24
  457. # 电视剧:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=dsj&datafl=&datadq=&fc=%E7%94%B5%E8%A7%86%E5%89%A7&datanf=&dataszm=
  458. # 动画片:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=dhp&datafl=&datadq=&fc=%E5%8A%A8%E7%94%BB%E7%89%87&dataszm=
  459. # 记录片:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=jlp&datapd=&datafl=&fc=%E7%BA%AA%E5%BD%95%E7%89%87&datanf=&dataszm=
  460. # 特别节目:https://tv.cctv.com/yxg/index.shtml?spm=C28340.PlFTqGe6Zk8M.E2PQtIunpEaz.65#datacid=tbjm&datapd=&datafl=&fc=%E7%89%B9%E5%88%AB%E8%8A%82%E7%9B%AE&datajr=&dataszm=
  461. config = {
  462. "player": {},
  463. "filter": {
  464. "电视剧": [
  465. {"key": "datafl-sc", "name": "类型",
  466. "value": [{"n": "全部", "v": ""}, {"n": "谍战", "v": "谍战"}, {"n": "悬疑", "v": "悬疑"},
  467. {"n": "刑侦", "v": "刑侦"}, {"n": "历史", "v": "历史"}, {"n": "古装", "v": "古装"},
  468. {"n": "武侠", "v": "武侠"}, {"n": "军旅", "v": "军旅"}, {"n": "战争", "v": "战争"},
  469. {"n": "喜剧", "v": "喜剧"}, {"n": "青春", "v": "青春"}, {"n": "言情", "v": "言情"},
  470. {"n": "偶像", "v": "偶像"}, {"n": "家庭", "v": "家庭"}, {"n": "年代", "v": "年代"},
  471. {"n": "革命", "v": "革命"}, {"n": "农村", "v": "农村"}, {"n": "都市", "v": "都市"},
  472. {"n": "其他", "v": "其他"}]},
  473. {"key": "datadq-area", "name": "地区",
  474. "value": [{"n": "全部", "v": ""}, {"n": "中国大陆", "v": "中国大陆"}, {"n": "中国香港", "v": "香港"},
  475. {"n": "美国", "v": "美国"}, {"n": "欧洲", "v": "欧洲"}, {"n": "泰国", "v": "泰国"}]},
  476. {"key": "datanf-year", "name": "年份",
  477. "value": [{"n": "全部", "v": ""}, {"n": "2024", "v": "2024"}, {"n": "2023", "v": "2023"},
  478. {"n": "2022", "v": "2022"},
  479. {"n": "2021", "v": "2021"}, {"n": "2020", "v": "2020"}, {"n": "2019", "v": "2019"},
  480. {"n": "2018", "v": "2018"}, {"n": "2017", "v": "2017"}, {"n": "2016", "v": "2016"},
  481. {"n": "2015", "v": "2015"}, {"n": "2014", "v": "2014"}, {"n": "2013", "v": "2013"},
  482. {"n": "2012", "v": "2012"}, {"n": "2011", "v": "2011"}, {"n": "2010", "v": "2010"},
  483. {"n": "2009", "v": "2009"}, {"n": "2008", "v": "2008"}, {"n": "2007", "v": "2007"},
  484. {"n": "2006", "v": "2006"}, {"n": "2005", "v": "2005"}, {"n": "2004", "v": "2004"},
  485. {"n": "2003", "v": "2003"}, {"n": "2002", "v": "2002"}, {"n": "2001", "v": "2001"},
  486. {"n": "2000", "v": "2000"}, {"n": "1999", "v": "1999"}, {"n": "1998", "v": "1998"},
  487. {"n": "1997", "v": "1997"}]},
  488. {"key": "dataszm-letter", "name": "字母",
  489. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  490. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  491. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  492. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  493. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  494. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  495. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  496. ],
  497. "动画片": [
  498. {"key": "datafl-sc", "name": "类型",
  499. "value": [{"n": "全部", "v": ""}, {"n": "亲子", "v": "亲子"}, {"n": "搞笑", "v": "搞笑"},
  500. {"n": "冒险", "v": "冒险"}, {"n": "动作", "v": "动作"}, {"n": "宠物", "v": "宠物"},
  501. {"n": "体育", "v": "体育"}, {"n": "益智", "v": "益智"}, {"n": "历史", "v": "历史"},
  502. {"n": "教育", "v": "教育"}, {"n": "校园", "v": "校园"}, {"n": "言情", "v": "言情"},
  503. {"n": "武侠", "v": "武侠"}, {"n": "经典", "v": "经典"}, {"n": "未来", "v": "未来"},
  504. {"n": "古代", "v": "古代"}, {"n": "神话", "v": "神话"}, {"n": "真人", "v": "真人"},
  505. {"n": "励志", "v": "励志"}, {"n": "热血", "v": "热血"}, {"n": "奇幻", "v": "奇幻"},
  506. {"n": "童话", "v": "童话"}, {"n": "剧情", "v": "剧情"}, {"n": "夺宝", "v": "夺宝"},
  507. {"n": "其他", "v": "其他"}]},
  508. {"key": "datadq-area", "name": "地区",
  509. "value": [{"n": "全部", "v": ""}, {"n": "中国大陆", "v": "中国大陆"}, {"n": "美国", "v": "美国"},
  510. {"n": "欧洲", "v": "欧洲"}]},
  511. {"key": "dataszm-letter", "name": "字母",
  512. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  513. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  514. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  515. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  516. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  517. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  518. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  519. ],
  520. "纪录片": [
  521. {"key": "datapd-channel", "name": "频道",
  522. "value": [{"n": "全部", "v": ""}, {"n": "CCTV{1 综合", "v": "CCTV{1 综合"},
  523. {"n": "CCTV{2 财经", "v": "CCTV{2 财经"}, {"n": "CCTV{3 综艺", "v": "CCTV{3 综艺"},
  524. {"n": "CCTV{4 中文国际", "v": "CCTV{4 中文国际"}, {"n": "CCTV{5 体育", "v": "CCTV{5 体育"},
  525. {"n": "CCTV{6 电影", "v": "CCTV{6 电影"}, {"n": "CCTV{7 国防军事", "v": "CCTV{7 国防军事"},
  526. {"n": "CCTV{8 电视剧", "v": "CCTV{8 电视剧"}, {"n": "CCTV{9 纪录", "v": "CCTV{9 纪录"},
  527. {"n": "CCTV{10 科教", "v": "CCTV{10 科教"}, {"n": "CCTV{11 戏曲", "v": "CCTV{11 戏曲"},
  528. {"n": "CCTV{12 社会与法", "v": "CCTV{12 社会与法"},
  529. {"n": "CCTV{13 新闻", "v": "CCTV{13 新闻"}, {"n": "CCTV{14 少儿", "v": "CCTV{14 少儿"},
  530. {"n": "CCTV{15 音乐", "v": "CCTV{15 音乐"},
  531. {"n": "CCTV{17 农业农村", "v": "CCTV{17 农业农村"}]},
  532. {"key": "datafl-sc", "name": "类型",
  533. "value": [{"n": "全部", "v": ""}, {"n": "人文历史", "v": "人文历史"}, {"n": "人物", "v": "人物"},
  534. {"n": "军事", "v": "军事"}, {"n": "探索", "v": "探索"}, {"n": "社会", "v": "社会"},
  535. {"n": "时政", "v": "时政"}, {"n": "经济", "v": "经济"}, {"n": "科技", "v": "科技"}]},
  536. {"key": "datanf-year", "name": "年份",
  537. "value": [{"n": "全部", "v": ""}, {"n": "2024", "v": "2024"}, {"n": "2023", "v": "2023"},
  538. {"n": "2022", "v": "2022"},
  539. {"n": "2021", "v": "2021"}, {"n": "2020", "v": "2020"}, {"n": "2019", "v": "2019"},
  540. {"n": "2018", "v": "2018"}, {"n": "2017", "v": "2017"}, {"n": "2016", "v": "2016"},
  541. {"n": "2015", "v": "2015"}, {"n": "2014", "v": "2014"}, {"n": "2013", "v": "2013"},
  542. {"n": "2012", "v": "2012"}, {"n": "2011", "v": "2011"}, {"n": "2010", "v": "2010"},
  543. {"n": "2009", "v": "2009"}, {"n": "2008", "v": "2008"}]},
  544. {"key": "dataszm-letter", "name": "字母",
  545. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  546. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  547. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  548. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  549. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  550. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  551. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  552. ],
  553. "特别节目": [
  554. {"key": "datapd-channel", "name": "频道",
  555. "value": [{"n": "全部", "v": ""}, {"n": "CCTV{1 综合", "v": "CCTV{1 综合"},
  556. {"n": "CCTV{2 财经", "v": "CCTV{2 财经"}, {"n": "CCTV{3 综艺", "v": "CCTV{3 综艺"},
  557. {"n": "CCTV{4 中文国际", "v": "CCTV{4 中文国际"}, {"n": "CCTV{5 体育", "v": "CCTV{5 体育"},
  558. {"n": "CCTV{6 电影", "v": "CCTV{6 电影"}, {"n": "CCTV{7 国防军事", "v": "CCTV{7 国防军事"},
  559. {"n": "CCTV{8 电视剧", "v": "CCTV{8 电视剧"}, {"n": "CCTV{9 纪录", "v": "CCTV{9 纪录"},
  560. {"n": "CCTV{10 科教", "v": "CCTV{10 科教"}, {"n": "CCTV{11 戏曲", "v": "CCTV{11 戏曲"},
  561. {"n": "CCTV{12 社会与法", "v": "CCTV{12 社会与法"},
  562. {"n": "CCTV{13 新闻", "v": "CCTV{13 新闻"}, {"n": "CCTV{14 少儿", "v": "CCTV{14 少儿"},
  563. {"n": "CCTV{15 音乐", "v": "CCTV{15 音乐"},
  564. {"n": "CCTV{17 农业农村", "v": "CCTV{17 农业农村"}]},
  565. {"key": "datafl-sc", "name": "类型",
  566. "value": [{"n": "全部", "v": ""}, {"n": "全部", "v": "全部"}, {"n": "新闻", "v": "新闻"},
  567. {"n": "经济", "v": "经济"}, {"n": "综艺", "v": "综艺"}, {"n": "体育", "v": "体育"},
  568. {"n": "军事", "v": "军事"}, {"n": "影视", "v": "影视"}, {"n": "科教", "v": "科教"},
  569. {"n": "戏曲", "v": "戏曲"}, {"n": "青少", "v": "青少"}, {"n": "音乐", "v": "音乐"},
  570. {"n": "社会", "v": "社会"}, {"n": "公益", "v": "公益"}, {"n": "其他", "v": "其他"}]},
  571. {"key": "dataszm-letter", "name": "字母",
  572. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "C", "v": "C"}, {"n": "E", "v": "E"},
  573. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"}, {"n": "I", "v": "I"},
  574. {"n": "J", "v": "J"}, {"n": "K", "v": "K"}, {"n": "L", "v": "L"}, {"n": "M", "v": "M"},
  575. {"n": "N", "v": "N"}, {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  576. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"}, {"n": "U", "v": "U"},
  577. {"n": "V", "v": "V"}, {"n": "W", "v": "W"}, {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"},
  578. {"n": "Z", "v": "Z"}, {"n": "0-9", "v": "0-9"}]}
  579. ],
  580. "栏目大全": [{"key": "cid", "name": "频道",
  581. "value": [{"n": "全部", "v": ""}, {"n": "CCTV-1综合", "v": "EPGC1386744804340101"},
  582. {"n": "CCTV-2财经", "v": "EPGC1386744804340102"},
  583. {"n": "CCTV-3综艺", "v": "EPGC1386744804340103"},
  584. {"n": "CCTV-4中文国际", "v": "EPGC1386744804340104"},
  585. {"n": "CCTV-5体育", "v": "EPGC1386744804340107"},
  586. {"n": "CCTV-6电影", "v": "EPGC1386744804340108"},
  587. {"n": "CCTV-7国防军事", "v": "EPGC1386744804340109"},
  588. {"n": "CCTV-8电视剧", "v": "EPGC1386744804340110"},
  589. {"n": "CCTV-9纪录", "v": "EPGC1386744804340112"},
  590. {"n": "CCTV-10科教", "v": "EPGC1386744804340113"},
  591. {"n": "CCTV-11戏曲", "v": "EPGC1386744804340114"},
  592. {"n": "CCTV-12社会与法", "v": "EPGC1386744804340115"},
  593. {"n": "CCTV-13新闻", "v": "EPGC1386744804340116"},
  594. {"n": "CCTV-14少儿", "v": "EPGC1386744804340117"},
  595. {"n": "CCTV-15音乐", "v": "EPGC1386744804340118"},
  596. {"n": "CCTV-16奥林匹克", "v": "EPGC1634630207058998"},
  597. {"n": "CCTV-17农业农村", "v": "EPGC1563932742616872"},
  598. {"n": "CCTV-5+体育赛事", "v": "EPGC1468294755566101"}]},
  599. {"key": "fc", "name": "分类",
  600. "value": [{"n": "全部", "v": ""}, {"n": "新闻", "v": "新闻"}, {"n": "体育", "v": "体育"},
  601. {"n": "综艺", "v": "综艺"}, {"n": "健康", "v": "健康"}, {"n": "生活", "v": "生活"},
  602. {"n": "科教", "v": "科教"}, {"n": "经济", "v": "经济"}, {"n": "农业", "v": "农业"},
  603. {"n": "法治", "v": "法治"}, {"n": "军事", "v": "军事"}, {"n": "少儿", "v": "少儿"},
  604. {"n": "动画", "v": "动画"}, {"n": "纪实", "v": "纪实"}, {"n": "戏曲", "v": "戏曲"},
  605. {"n": "音乐", "v": "音乐"}, {"n": "影视", "v": "影视"}]},
  606. {"key": "fl", "name": "字母",
  607. "value": [{"n": "全部", "v": ""}, {"n": "A", "v": "A"}, {"n": "B", "v": "B"},
  608. {"n": "C", "v": "C"}, {"n": "D", "v": "D"}, {"n": "E", "v": "E"},
  609. {"n": "F", "v": "F"}, {"n": "G", "v": "G"}, {"n": "H", "v": "H"},
  610. {"n": "I", "v": "I"}, {"n": "J", "v": "J"}, {"n": "K", "v": "K"},
  611. {"n": "L", "v": "L"}, {"n": "M", "v": "M"}, {"n": "N", "v": "N"},
  612. {"n": "O", "v": "O"}, {"n": "P", "v": "P"}, {"n": "Q", "v": "Q"},
  613. {"n": "R", "v": "R"}, {"n": "S", "v": "S"}, {"n": "T", "v": "T"},
  614. {"n": "U", "v": "U"}, {"n": "V", "v": "V"}, {"n": "W", "v": "W"},
  615. {"n": "X", "v": "X"}, {"n": "Y", "v": "Y"}, {"n": "Z", "v": "Z"}]},
  616. ]
  617. }
  618. }
  619. header = {
  620. "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",
  621. "Host": "tv.cctv.com",
  622. "Referer": "https://tv.cctv.com/"
  623. }
  624. def localProxy(self, param):
  625. return [200, "video/MP2T", action, ""]
  626. # -----------------------------------------------自定义函数-----------------------------------------------
  627. # 访问网页
  628. def webReadFile(self, urlStr, header):
  629. html = ''
  630. req = urllib.request.Request(url=urlStr) # ,headers=header
  631. with urllib.request.urlopen(req) as response:
  632. html = response.read().decode('utf-8')
  633. return html
  634. # 判断网络地址是否存在
  635. def TestWebPage(self, urlStr, header):
  636. html = ''
  637. req = urllib.request.Request(url=urlStr, method='HEAD') # ,headers=header
  638. with urllib.request.urlopen(req) as response:
  639. html = response.getcode()
  640. return html
  641. # 正则取文本
  642. def get_RegexGetText(self, Text, RegexText, Index):
  643. returnTxt = ""
  644. Regex = re.search(RegexText, Text, re.M | re.S)
  645. if Regex is None:
  646. returnTxt = ""
  647. else:
  648. returnTxt = Regex.group(Index)
  649. return returnTxt
  650. # 取集数
  651. def get_EpisodesList(self, jsonList):
  652. videos = []
  653. for vod in jsonList:
  654. url = vod['guid']
  655. title = vod['title']
  656. if len(url) == 0:
  657. continue
  658. videos.append(title + "$" + url)
  659. return videos
  660. # 取集数
  661. def get_EpisodesList_re(self, htmlTxt, patternTxt):
  662. ListRe = re.finditer(patternTxt, htmlTxt, re.M | re.S)
  663. videos = []
  664. for vod in ListRe:
  665. url = vod.group('url')
  666. title = vod.group('title')
  667. if len(url) == 0:
  668. continue
  669. videos.append(title + "$" + url)
  670. return videos
  671. # 取剧集区
  672. def get_lineList(self, Txt, mark, after):
  673. circuit = []
  674. origin = Txt.find(mark)
  675. while origin > 8:
  676. end = Txt.find(after, origin)
  677. circuit.append(Txt[origin:end])
  678. origin = Txt.find(mark, end)
  679. return circuit
  680. # 正则取文本,返回数组
  681. def get_RegexGetTextLine(self, Text, RegexText, Index):
  682. returnTxt = []
  683. pattern = re.compile(RegexText, re.M | re.S)
  684. ListRe = pattern.findall(Text)
  685. if len(ListRe) < 1:
  686. return returnTxt
  687. for value in ListRe:
  688. returnTxt.append(value)
  689. return returnTxt
  690. # 删除html标签
  691. def removeHtml(self, txt):
  692. soup = re.compile(r'<[^>]+>', re.S)
  693. txt = soup.sub('', txt)
  694. return txt.replace("&nbsp;", " ")
  695. # 取m3u8
  696. def get_m3u8(self, urlTxt):
  697. url = "https://vdn.apps.cntv.cn/api/getHttpVideoInfo.do?pid={0}".format(urlTxt)
  698. # htmlTxt = self.webReadFile(urlStr=url, header=self.header)
  699. htmlTxt = self.fetch(url).text
  700. jo = json.loads(htmlTxt)
  701. link = jo['hls_url'].strip()
  702. html = self.webReadFile(urlStr=link, header=self.header)
  703. content = html.strip()
  704. arr = content.split('\n')
  705. urlPrefix = self.get_RegexGetText(Text=link, RegexText='(http[s]?://[a-zA-z0-9.]+)/', Index=1)
  706. subUrl = arr[-1].split('/')
  707. # subUrl[3] = '1200'
  708. subUrl[3] = '2000'
  709. # subUrl[-1] = '1200.m3u8'
  710. subUrl[-1] = '2000.m3u8'
  711. hdUrl = urlPrefix + '/'.join(subUrl)
  712. url = urlPrefix + arr[-1]
  713. hdRsp = self.TestWebPage(urlStr=hdUrl, header=self.header)
  714. if hdRsp == 200:
  715. url = hdUrl
  716. else:
  717. url = ''
  718. return url
  719. # 搜索
  720. def get_list_search(self, html, tid):
  721. jRoot = json.loads(html)
  722. jsonList = jRoot['list']
  723. videos = []
  724. for vod in jsonList:
  725. url = vod['urllink']
  726. title = self.removeHtml(txt=vod['title'])
  727. img = vod['imglink']
  728. id = vod['id']
  729. brief = vod['channel']
  730. year = vod['uploadtime']
  731. if len(url) == 0:
  732. continue
  733. guids = [tid, title, url, img, id, year, '', brief]
  734. guid = "||".join(guids)
  735. videos.append({
  736. "vod_id": guid,
  737. "vod_name": title,
  738. "vod_pic": img,
  739. "vod_remarks": year
  740. })
  741. return videos
  742. def get_list1(self, html, tid):
  743. jRoot = json.loads(html)
  744. videos = []
  745. data = jRoot['response']
  746. if data is None:
  747. return []
  748. jsonList = data['docs']
  749. for vod in jsonList:
  750. id = vod['lastVIDE']['videoSharedCode']
  751. desc = vod['lastVIDE']['videoTitle']
  752. title = vod['column_name']
  753. url = vod['column_website']
  754. img = vod['column_logo']
  755. year = vod['column_playdate']
  756. brief = vod['column_brief']
  757. actors = ''
  758. if len(url) == 0:
  759. continue
  760. guids = [tid, title, url, img, id, year, actors, brief]
  761. guid = "||".join(guids)
  762. # print(vod_id)
  763. videos.append({
  764. "vod_id": guid,
  765. "vod_name": title,
  766. "vod_pic": img,
  767. "vod_remarks": desc.split('》')[1].strip() if '》' in desc else desc.strip()
  768. })
  769. # print(videos)
  770. return videos
  771. # 分类取结果
  772. def get_list(self, html, tid):
  773. jRoot = json.loads(html)
  774. videos = []
  775. data = jRoot['data']
  776. if data is None:
  777. return []
  778. jsonList = data['list']
  779. for vod in jsonList:
  780. url = vod['url']
  781. title = vod['title']
  782. img = vod['image']
  783. id = vod['id']
  784. try:
  785. brief = vod['brief']
  786. except:
  787. brief = ''
  788. try:
  789. year = vod['year']
  790. except:
  791. year = ''
  792. try:
  793. actors = vod['actors']
  794. except:
  795. actors = ''
  796. if len(url) == 0:
  797. continue
  798. guids = [tid, title, url, img, id, year, actors, brief]
  799. guid = "||".join(guids)
  800. # print(vod_id)
  801. videos.append({
  802. "vod_id": guid,
  803. "vod_name": title,
  804. "vod_pic": img,
  805. "vod_remarks": ''
  806. })
  807. return videos
  808. if __name__ == '__main__':
  809. spider = Spider()
  810. spider.init()
  811. spider.init_api_ext_file()
  812. # url = 'https://api.cntv.cn/lanmu/columnSearch?&fl=&fc=%E6%96%B0%E9%97%BB&cid=&p=1&n=20&serviceId=tvcctv&t=jsonp&cb=Callback'
  813. # url = 'https://api.cntv.cn/lanmu/columnSearch?&fl=&fc=&cid=&p=1&n=20&serviceId=tvcctv&t=json&cb=ko'
  814. # r = spider.fetch(url)
  815. # print(r.text)
  816. # home_content = spider.homeContent(None)
  817. # print(home_content)
  818. # cate_content = spider.categoryContent('栏目大全', 1, {'cid': 'n'}, {})
  819. # print(cate_content)
  820. # vid = cate_content['list'][0]['vod_id']
  821. # print(vid)
  822. # detail_content = spider.detailContent([vid])
  823. # print(detail_content)
  824. #
  825. # vod_play_from = detail_content['list'][0]['vod_play_from']
  826. # vod_play_url = detail_content['list'][0]['vod_play_url']
  827. # print(vod_play_from, vod_play_url)
  828. # _url = vod_play_url.split('#')[0].split('$')[1]
  829. # print(_url)
  830. # play = spider.playerContent(vod_play_from, _url, None)
  831. # print(play)