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.

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