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.

97 lines
3.8 KiB

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