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.

99 lines
3.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. )
  8. class CNNIE(InfoExtractor):
  9. _VALID_URL = r'''(?x)https?://((edition|www)\.)?cnn\.com/video/(data/.+?|\?)/
  10. (?P<path>.+?/(?P<title>[^/]+?)(?:\.cnn|(?=&)))'''
  11. _TESTS = [{
  12. 'url': 'http://edition.cnn.com/video/?/video/sports/2013/06/09/nadal-1-on-1.cnn',
  13. 'file': 'sports_2013_06_09_nadal-1-on-1.cnn.mp4',
  14. 'md5': '3e6121ea48df7e2259fe73a0628605c4',
  15. 'info_dict': {
  16. 'title': 'Nadal wins 8th French Open title',
  17. 'description': 'World Sport\'s Amanda Davies chats with 2013 French Open champion Rafael Nadal.',
  18. 'duration': 135,
  19. 'upload_date': '20130609',
  20. },
  21. },
  22. {
  23. u"url": u"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",
  24. u"file": u"us_2013_08_21_sot-student-gives-epic-speech.georgia-institute-of-technology.mp4",
  25. u"md5": u"b5cc60c60a3477d185af8f19a2a26f4e",
  26. u"info_dict": {
  27. u"title": "Student's epic speech stuns new freshmen",
  28. u"description": "A Georgia Tech student welcomes the incoming freshmen with an epic speech backed by music from \"2001: A Space Odyssey.\""
  29. }
  30. }]
  31. def _real_extract(self, url):
  32. mobj = re.match(self._VALID_URL, url)
  33. path = mobj.group('path')
  34. page_title = mobj.group('title')
  35. info_url = 'http://cnn.com/video/data/3.0/%s/index.xml' % path
  36. info = self._download_xml(info_url, page_title)
  37. formats = []
  38. rex = re.compile(r'''(?x)
  39. (?P<width>[0-9]+)x(?P<height>[0-9]+)
  40. (?:_(?P<bitrate>[0-9]+)k)?
  41. ''')
  42. for f in info.findall('files/file'):
  43. video_url = 'http://ht.cdn.turner.com/cnn/big%s' % (f.text.strip())
  44. fdct = {
  45. 'format_id': f.attrib['bitrate'],
  46. 'url': video_url,
  47. }
  48. mf = rex.match(f.attrib['bitrate'])
  49. if mf:
  50. fdct['width'] = int(mf.group('width'))
  51. fdct['height'] = int(mf.group('height'))
  52. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  53. else:
  54. mf = rex.search(f.text)
  55. if mf:
  56. fdct['width'] = int(mf.group('width'))
  57. fdct['height'] = int(mf.group('height'))
  58. fdct['tbr'] = int_or_none(mf.group('bitrate'))
  59. else:
  60. mi = re.match(r'ios_(audio|[0-9]+)$', f.attrib['bitrate'])
  61. if mi:
  62. if mi.group(1) == 'audio':
  63. fdct['vcodec'] = 'none'
  64. fdct['ext'] = 'm4a'
  65. else:
  66. fdct['tbr'] = int(mi.group(1))
  67. formats.append(fdct)
  68. self._sort_formats(formats)
  69. thumbnails = sorted([((int(t.attrib['height']),int(t.attrib['width'])), t.text) for t in info.findall('images/image')])
  70. thumbs_dict = [{'resolution': res, 'url': t_url} for (res, t_url) in thumbnails]
  71. metas_el = info.find('metas')
  72. upload_date = (
  73. metas_el.attrib.get('version') if metas_el is not None else None)
  74. duration_el = info.find('length')
  75. duration = parse_duration(duration_el.text)
  76. return {
  77. 'id': info.attrib['id'],
  78. 'title': info.find('headline').text,
  79. 'formats': formats,
  80. 'thumbnail': thumbnails[-1][1],
  81. 'thumbnails': thumbs_dict,
  82. 'description': info.find('description').text,
  83. 'duration': duration,
  84. 'upload_date': upload_date,
  85. }