cnn.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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?://(?:(?P<sub_domain>edition|www|money)\.)?cnn\.com/(?:video/(?:data/.+?|\?)/)?videos?/
  11. (?P<path>.+?/(?P<title>[^/]+?)(?:\.(?:[a-z\-]+)|(?=&)))'''
  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. 'url': 'http://www.cnn.com/video/data/2.0/video/living/2014/12/22/growing-america-nashville-salemtown-board-episode-1.hln.html',
  35. 'md5': 'f14d02ebd264df951feb2400e2c25a1b',
  36. 'info_dict': {
  37. 'id': 'living/2014/12/22/growing-america-nashville-salemtown-board-episode-1.hln',
  38. 'ext': 'mp4',
  39. 'title': 'Nashville Ep. 1: Hand crafted skateboards',
  40. 'description': 'md5:e7223a503315c9f150acac52e76de086',
  41. 'upload_date': '20141222',
  42. }
  43. }, {
  44. 'url': 'http://money.cnn.com/video/news/2016/08/19/netflix-stunning-stats.cnnmoney/index.html',
  45. 'md5': '52a515dc1b0f001cd82e4ceda32be9d1',
  46. 'info_dict': {
  47. 'id': '/video/news/2016/08/19/netflix-stunning-stats.cnnmoney',
  48. 'ext': 'mp4',
  49. 'title': '5 stunning stats about Netflix',
  50. 'description': 'Did you know that Netflix has more than 80 million members? Here are five facts about the online video distributor that you probably didn\'t know.',
  51. 'upload_date': '20160819',
  52. }
  53. }, {
  54. 'url': 'http://cnn.com/video/?/video/politics/2015/03/27/pkg-arizona-senator-church-attendance-mandatory.ktvk',
  55. 'only_matching': True,
  56. }, {
  57. 'url': 'http://cnn.com/video/?/video/us/2015/04/06/dnt-baker-refuses-anti-gay-order.wkmg',
  58. 'only_matching': True,
  59. }, {
  60. 'url': 'http://edition.cnn.com/videos/arts/2016/04/21/olympic-games-cultural-a-z-brazil.cnn',
  61. 'only_matching': True,
  62. }]
  63. _CONFIG = {
  64. # http://edition.cnn.com/.element/apps/cvp/3.0/cfg/spider/cnn/expansion/config.xml
  65. 'edition': {
  66. 'data_src': 'http://edition.cnn.com/video/data/3.0/video/%s/index.xml',
  67. 'media_src': 'http://pmd.cdn.turner.com/cnn/big',
  68. },
  69. # http://money.cnn.com/.element/apps/cvp2/cfg/config.xml
  70. 'money': {
  71. 'data_src': 'http://money.cnn.com/video/data/4.0/video/%s.xml',
  72. 'media_src': 'http://ht3.cdn.turner.com/money/big',
  73. },
  74. }
  75. def _real_extract(self, url):
  76. sub_domain, path, page_title = re.match(self._VALID_URL, url).groups()
  77. if sub_domain not in ('money', 'edition'):
  78. sub_domain = 'edition'
  79. config = self._CONFIG[sub_domain]
  80. info_url = config['data_src'] % path
  81. info = self._download_xml(info_url, page_title)
  82. formats = []
  83. rex = re.compile(r'''(?x)
  84. (?P<width>[0-9]+)x(?P<height>[0-9]+)
  85. (?:_(?P<bitrate>[0-9]+)k)?
  86. ''')
  87. for f in info.findall('files/file'):
  88. video_url = config['media_src'] + f.text.strip()
  89. fdct = {
  90. 'format_id': f.attrib['bitrate'],
  91. 'url': video_url,
  92. }
  93. mf = rex.match(f.attrib['bitrate'])
  94. if mf:
  95. fdct['width'] = int(mf.group('width'))
  96. fdct['height'] = int(mf.group('height'))
  97. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  98. else:
  99. mf = rex.search(f.text)
  100. if mf:
  101. fdct['width'] = int(mf.group('width'))
  102. fdct['height'] = int(mf.group('height'))
  103. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  104. else:
  105. mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
  106. if mi:
  107. if mi.group(1) == 'audio':
  108. fdct['vcodec'] = 'none'
  109. fdct['ext'] = 'm4a'
  110. else:
  111. fdct['tbr'] = int(mi.group(1))
  112. formats.append(fdct)
  113. self._sort_formats(formats)
  114. thumbnails = [{
  115. 'height': int(t.attrib['height']),
  116. 'width': int(t.attrib['width']),
  117. 'url': t.text,
  118. } for t in info.findall('images/image')]
  119. metas_el = info.find('metas')
  120. upload_date = (
  121. metas_el.attrib.get('version') if metas_el is not None else None)
  122. duration_el = info.find('length')
  123. duration = parse_duration(duration_el.text)
  124. return {
  125. 'id': info.attrib['id'],
  126. 'title': info.find('headline').text,
  127. 'formats': formats,
  128. 'thumbnails': thumbnails,
  129. 'description': info.find('description').text,
  130. 'duration': duration,
  131. 'upload_date': upload_date,
  132. }
  133. class CNNBlogsIE(InfoExtractor):
  134. _VALID_URL = r'https?://[^\.]+\.blogs\.cnn\.com/.+'
  135. _TEST = {
  136. 'url': 'http://reliablesources.blogs.cnn.com/2014/02/09/criminalizing-journalism/',
  137. 'md5': '3e56f97b0b6ffb4b79f4ea0749551084',
  138. 'info_dict': {
  139. 'id': 'bestoftv/2014/02/09/criminalizing-journalism.cnn',
  140. 'ext': 'mp4',
  141. 'title': 'Criminalizing journalism?',
  142. 'description': 'Glenn Greenwald responds to comments made this week on Capitol Hill that journalists could be criminal accessories.',
  143. 'upload_date': '20140209',
  144. },
  145. 'add_ie': ['CNN'],
  146. }
  147. def _real_extract(self, url):
  148. webpage = self._download_webpage(url, url_basename(url))
  149. cnn_url = self._html_search_regex(r'data-url="(.+?)"', webpage, 'cnn url')
  150. return {
  151. '_type': 'url',
  152. 'url': cnn_url,
  153. 'ie_key': CNNIE.ie_key(),
  154. }
  155. class CNNArticleIE(InfoExtractor):
  156. _VALID_URL = r'https?://(?:(?:edition|www)\.)?cnn\.com/(?!videos?/)'
  157. _TEST = {
  158. 'url': 'http://www.cnn.com/2014/12/21/politics/obama-north-koreas-hack-not-war-but-cyber-vandalism/',
  159. 'md5': '689034c2a3d9c6dc4aa72d65a81efd01',
  160. 'info_dict': {
  161. 'id': 'bestoftv/2014/12/21/ip-north-korea-obama.cnn',
  162. 'ext': 'mp4',
  163. 'title': 'Obama: Cyberattack not an act of war',
  164. 'description': 'md5:51ce6750450603795cad0cdfbd7d05c5',
  165. 'upload_date': '20141221',
  166. },
  167. 'add_ie': ['CNN'],
  168. }
  169. def _real_extract(self, url):
  170. webpage = self._download_webpage(url, url_basename(url))
  171. cnn_url = self._html_search_regex(r"video:\s*'([^']+)'", webpage, 'cnn url')
  172. return {
  173. '_type': 'url',
  174. 'url': 'http://cnn.com/video/?/video/' + cnn_url,
  175. 'ie_key': CNNIE.ie_key(),
  176. }