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.

128 lines
4.7 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|(?=&)))'''
  12. _TESTS = [{
  13. 'url': 'http://edition.cnn.com/video/?/video/sports/2013/06/09/nadal-1-on-1.cnn',
  14. 'file': 'sports_2013_06_09_nadal-1-on-1.cnn.mp4',
  15. 'md5': '3e6121ea48df7e2259fe73a0628605c4',
  16. 'info_dict': {
  17. 'title': 'Nadal wins 8th French Open title',
  18. 'description': 'World Sport\'s Amanda Davies chats with 2013 French Open champion Rafael Nadal.',
  19. 'duration': 135,
  20. 'upload_date': '20130609',
  21. },
  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. "file": "us_2013_08_21_sot-student-gives-epic-speech.georgia-institute-of-technology.mp4",
  26. "md5": "b5cc60c60a3477d185af8f19a2a26f4e",
  27. "info_dict": {
  28. "title": "Student's epic speech stuns new freshmen",
  29. "description": "A Georgia Tech student welcomes the incoming freshmen with an epic speech backed by music from \"2001: A Space Odyssey.\"",
  30. "upload_date": "20130821",
  31. }
  32. }]
  33. def _real_extract(self, url):
  34. mobj = re.match(self._VALID_URL, url)
  35. path = mobj.group('path')
  36. page_title = mobj.group('title')
  37. info_url = 'http://cnn.com/video/data/3.0/%s/index.xml' % path
  38. info = self._download_xml(info_url, page_title)
  39. formats = []
  40. rex = re.compile(r'''(?x)
  41. (?P<width>[0-9]+)x(?P<height>[0-9]+)
  42. (?:_(?P<bitrate>[0-9]+)k)?
  43. ''')
  44. for f in info.findall('files/file'):
  45. video_url = 'http://ht.cdn.turner.com/cnn/big%s' % (f.text.strip())
  46. fdct = {
  47. 'format_id': f.attrib['bitrate'],
  48. 'url': video_url,
  49. }
  50. mf = rex.match(f.attrib['bitrate'])
  51. if mf:
  52. fdct['width'] = int(mf.group('width'))
  53. fdct['height'] = int(mf.group('height'))
  54. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  55. else:
  56. mf = rex.search(f.text)
  57. if mf:
  58. fdct['width'] = int(mf.group('width'))
  59. fdct['height'] = int(mf.group('height'))
  60. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  61. else:
  62. mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
  63. if mi:
  64. if mi.group(1) == 'audio':
  65. fdct['vcodec'] = 'none'
  66. fdct['ext'] = 'm4a'
  67. else:
  68. fdct['tbr'] = int(mi.group(1))
  69. formats.append(fdct)
  70. self._sort_formats(formats)
  71. thumbnails = [{
  72. 'height': int(t.attrib['height']),
  73. 'width': int(t.attrib['width']),
  74. 'url': t.text,
  75. } for t in info.findall('images/image')]
  76. metas_el = info.find('metas')
  77. upload_date = (
  78. metas_el.attrib.get('version') if metas_el is not None else None)
  79. duration_el = info.find('length')
  80. duration = parse_duration(duration_el.text)
  81. return {
  82. 'id': info.attrib['id'],
  83. 'title': info.find('headline').text,
  84. 'formats': formats,
  85. 'thumbnails': thumbnails,
  86. 'description': info.find('description').text,
  87. 'duration': duration,
  88. 'upload_date': upload_date,
  89. }
  90. class CNNBlogsIE(InfoExtractor):
  91. _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
  92. _TEST = {
  93. 'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
  94. 'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
  95. 'info_dict': {
  96. 'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
  97. 'ext': 'mp4',
  98. 'title': 'Criminalizing journalism?',
  99. 'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
  100. 'upload_date': '20140209',
  101. },
  102. 'add_ie': ['CNN'],
  103. }
  104. def _real_extract(self, url):
  105. webpage = self._download_webpage(url, url_basename(url))
  106. cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
  107. return {
  108. '_type': 'url',
  109. 'url': cnn_url,
  110. 'ie_key': CNNIE.ie_key(),
  111. }