cntv.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_urllib_request
  5. class CntvIE(InfoExtractor):
  6. IE_NAME = 'cntv'
  7. IE_DESC = '央视网'
  8. _VALID_URL = r'''(?x)
  9. (?:
  10. http://(?:ent|tv|sports|english|chunwan)\.(?:cntv|cctv)\.(?:com|cn)/
  11. (?:
  12. (?:video|special)/(?:[_A-Za-z0-9]+)|(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)
  13. )
  14. )
  15. /(?P<id>[A-Za-z0-9]+)(index)?(?:\.shtml|)
  16. '''
  17. _TESTS = [{
  18. 'url': 'http://sports.cntv.cn/2016/02/12/ARTIaBRxv4rTT1yWf1frW2wi160212.shtml',
  19. 'md5': 'd0f8c3e043fe43534517ed47c831fd4e',
  20. 'info_dict': {
  21. 'id': '5ecdbeab623f4973b40ff25f18b174e8',
  22. 'ext': 'flv',
  23. 'title': '[NBA]二少联手砍下46分 雷霆主场击败鹈鹕(快讯)',
  24. 'description': 'md5:7e14a5328dc5eb3d1cd6afbbe0574e95',
  25. 'uploader': 'songjunjie'
  26. }
  27. },{
  28. 'url': 'http://tv.cctv.com/2016/02/05/VIDEUS7apq3lKrHG9Dncm03B160205.shtml',
  29. 'md5': '3bde627de65b6bad064f3e67ec9e71a1',
  30. 'info_dict': {
  31. 'id': 'efc5d49e5b3b4ab2b34f3a502b73d3ae',
  32. 'ext': 'flv',
  33. 'title': '[赛车]“车王”舒马赫恢复情况成谜(快讯)',
  34. 'description': '2月4日,蒙特泽莫罗透露了关于“车王”舒马赫恢复情况,但情况是否属实遭到了质疑。',
  35. 'uploader': 'shujun'
  36. }
  37. },{
  38. 'url': 'http://english.cntv.cn/special/four_comprehensives/index.shtml',
  39. 'md5': 'f37a56d5f16647eae24c3e618f8d23a2',
  40. 'info_dict': {
  41. 'id': '4bb9bb4db7a6471ba85fdeda5af0381e',
  42. 'ext': 'flv',
  43. 'title': 'NHnews008 ANNUAL POLITICAL SEASON',
  44. 'description': 'Four Comprehensives',
  45. 'uploader': 'zhangyunlei'
  46. }
  47. },{
  48. 'url': 'http://ent.cntv.cn/2016/01/18/ARTIjprSSJH8DryTVr5Bx8Wb160118.shtml',
  49. 'md5': '37ad2407b5f28336ddf26aacea570ee5',
  50. 'info_dict': {
  51. 'id': '45336758dd474ef39dc6887f115e0375',
  52. 'ext': 'flv',
  53. 'title': '《爱国之恋》中国梦新歌展播 综艺视频精切',
  54. 'description': 'md5:856836ae2660f000e458ead597b1e2bd',
  55. 'uploader': 'baiyuqing'
  56. }
  57. },{
  58. 'url': 'http://tv.cntv.cn/video/C39296/e0210d949f113ddfb38d31f00a4e5c44',
  59. 'md5': 'd7188ecc2a2f8447c70023895724c31c',
  60. 'info_dict': {
  61. 'id': 'e0210d949f113ddfb38d31f00a4e5c44',
  62. 'ext': 'flv',
  63. 'title': '《传奇故事》 20150409 金钱“魔术”(下)',
  64. 'description': '本期节目主要内容: 2014年10月23日凌晨,浙江余杭警方出动2000多名警力,一举摧毁1040阳光工程传销组织,抓获65名涉嫌传销的组织头目,教育遣返400多名参与传销人员。',
  65. 'uploader': '陆银凯'
  66. }
  67. }]
  68. def _real_extract(self, url):
  69. video_id = self._match_id(url)
  70. webpage = self._download_webpage(url, video_id)
  71. video_id_regex = [r'var guid[ ="]+([a-zA-Z0-9]+)"',
  72. r'"videoCenterId"[ ,"]+([a-zA-Z0-9]+)"',
  73. r'"changePlayer\(\'([a-zA-Z0-9]+)\'\)\"']
  74. video_id = self._search_regex(video_id_regex, webpage, 'video_id', default=None)
  75. if video_id is None:
  76. video_id = self._match_id(url)
  77. # refer to youku.py
  78. def retrieve_data(req_url, note):
  79. req = compat_urllib_request.Request(req_url)
  80. cn_verification_proxy = self._downloader.params.get('cn_verification_proxy')
  81. if cn_verification_proxy:
  82. req.add_header('Ytdl-request-proxy', cn_verification_proxy)
  83. raw_data = self._download_json(req, video_id, note=note)
  84. return raw_data
  85. get_info_url = \
  86. 'http://vdn.apps.cntv.cn/api/getHttpVideoInfo.do' + \
  87. '?pid=%s' % video_id + \
  88. '&tz=-8' + \
  89. '&from=000tv' + \
  90. '&url=%s' % url + \
  91. '&idl=32' + \
  92. '&idlr=32' + \
  93. '&modifyed=false'
  94. data = retrieve_data(
  95. get_info_url,
  96. 'Downloading JSON metadata')
  97. title = data.get('title')
  98. uploader = data.get('editer_name')
  99. hls_url = data.get('hls_url')
  100. formats = self._extract_m3u8_formats(hls_url, video_id, 'flv')
  101. self._sort_formats(formats)
  102. description = self._html_search_meta('description', webpage)
  103. return {
  104. 'id': video_id,
  105. 'title': title,
  106. 'uploader': uploader,
  107. 'formats': formats,
  108. 'description': description,
  109. }