You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
5.8 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. int_or_none,
  6. parse_duration,
  7. url_basename,
  8. )
  9. class CNNIE(InfoExtractor):
  10. _VALID_URL = r'''(?x)https?://(?:(?:edition|www)\.)?cnn\.com/video/(?:data/.+?|\?)/
  11. (?P<path>.+?/(?P<title>[^/]+?)(?:\.cnn(?:-ap)?|(?=&)))'''
  12. _TESTS = [{
  13. 'url': 'http://edition.cnn.com/video/?/video/sports/2013/06/09/nadal-1-on-1.cnn',
  14. 'md5': '3e6121ea48df7e2259fe73a0628605c4',
  15. 'info_dict': {
  16. 'id': 'sports/2013/06/09/nadal-1-on-1.cnn',
  17. 'ext': 'mp4',
  18. 'title': 'Nadal wins 8th French Open title',
  19. 'description': 'World Sport\'s Amanda Davies chats with 2013 French Open champion Rafael Nadal.',
  20. 'duration': 135,
  21. 'upload_date': '20130609',
  22. },
  23. }, {
  24. "url": "http://edition.cnn.com/video/?/video/us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology&utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+rss%2Fcnn_topstories+%28RSS%3A+Top+Stories%29",
  25. "md5": "b5cc60c60a3477d185af8f19a2a26f4e",
  26. "info_dict": {
  27. 'id': 'us/2013/08/21/sot-student-gives-epic-speech.georgia-institute-of-technology',
  28. 'ext': 'mp4',
  29. "title": "Student's epic speech stuns new freshmen",
  30. "description": "A Georgia Tech student welcomes the incoming freshmen with an epic speech backed by music from \"2001: A Space Odyssey.\"",
  31. "upload_date": "20130821",
  32. }
  33. }]
  34. def _real_extract(self, url):
  35. mobj = re.match(self._VALID_URL, url)
  36. path = mobj.group('path')
  37. page_title = mobj.group('title')
  38. info_url = 'http://cnn.com/video/data/3.0/%s/index.xml' % path
  39. info = self._download_xml(info_url, page_title)
  40. formats = []
  41. rex = re.compile(r'''(?x)
  42. (?P<width>[0-9]+)x(?P<height>[0-9]+)
  43. (?:_(?P<bitrate>[0-9]+)k)?
  44. ''')
  45. for f in info.findall('files/file'):
  46. video_url = 'http://ht.cdn.turner.com/cnn/big%s' % (f.text.strip())
  47. fdct = {
  48. 'format_id': f.attrib['bitrate'],
  49. 'url': video_url,
  50. }
  51. mf = rex.match(f.attrib['bitrate'])
  52. if mf:
  53. fdct['width'] = int(mf.group('width'))
  54. fdct['height'] = int(mf.group('height'))
  55. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  56. else:
  57. mf = rex.search(f.text)
  58. if mf:
  59. fdct['width'] = int(mf.group('width'))
  60. fdct['height'] = int(mf.group('height'))
  61. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  62. else:
  63. mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
  64. if mi:
  65. if mi.group(1) == 'audio':
  66. fdct['vcodec'] = 'none'
  67. fdct['ext'] = 'm4a'
  68. else:
  69. fdct['tbr'] = int(mi.group(1))
  70. formats.append(fdct)
  71. self._sort_formats(formats)
  72. thumbnails = [{
  73. 'height': int(t.attrib['height']),
  74. 'width': int(t.attrib['width']),
  75. 'url': t.text,
  76. } for t in info.findall('images/image')]
  77. metas_el = info.find('metas')
  78. upload_date = (
  79. metas_el.attrib.get('version') if metas_el is not None else None)
  80. duration_el = info.find('length')
  81. duration = parse_duration(duration_el.text)
  82. return {
  83. 'id': info.attrib['id'],
  84. 'title': info.find('headline').text,
  85. 'formats': formats,
  86. 'thumbnails': thumbnails,
  87. 'description': info.find('description').text,
  88. 'duration': duration,
  89. 'upload_date': upload_date,
  90. }
  91. class CNNBlogsIE(InfoExtractor):
  92. _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
  93. _TEST = {
  94. 'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
  95. 'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
  96. 'info_dict': {
  97. 'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
  98. 'ext': 'mp4',
  99. 'title': 'Criminalizing journalism?',
  100. 'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
  101. 'upload_date': '20140209',
  102. },
  103. 'add_ie': ['CNN'],
  104. }
  105. def _real_extract(self, url):
  106. webpage = self._download_webpage(url, url_basename(url))
  107. cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
  108. return {
  109. '_type': 'url',
  110. 'url': cnn_url,
  111. 'ie_key': CNNIE.ie_key(),
  112. }
  113. class CNNArticleIE(InfoExtractor):
  114. _VALID_URL = r'https?://(?:(?:edition|www)\.)?cnn\.com/(?!video/)'
  115. _TEST = {
  116. 'url': 'http://www.cnn.com/2014/12/21/politics/obama-north-koreas-hack-not-war-but-cyber-vandalism/',
  117. 'md5': '275b326f85d80dff7592a9820f5dc887',
  118. 'info_dict': {
  119. 'id': 'bestoftv/2014/12/21/sotu-crowley-president-obama-north-korea-not-going-to-be-intimidated.cnn',
  120. 'ext': 'mp4',
  121. 'title': 'Obama: We\'re not going to be intimidated',
  122. 'description': 'md5:e735586f3dc936075fa654a4d91b21f9',
  123. 'upload_date': '20141220',
  124. },
  125. 'add_ie': ['CNN'],
  126. }
  127. def _real_extract(self, url):
  128. webpage = self._download_webpage(url, url_basename(url))
  129. cnn_url = self._html_search_regex(r"video:\s*'([^']+)'", webpage, 'cnn url')
  130. return {
  131. '_type': 'url',
  132. 'url': 'http://cnn.com/video/?/video/' + cnn_url,
  133. 'ie_key': CNNIE.ie_key(),
  134. }