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.

138 lines
5.3 KiB

  1. import re
  2. import xml.etree.ElementTree
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_urlparse,
  7. determine_ext,
  8. )
  9. class AppleTrailersIE(InfoExtractor):
  10. _VALID_URL = r'https?://(?:www\.)?trailers.apple.com/trailers/(?P<company>[^/]+)/(?P<movie>[^/]+)'
  11. _TEST = {
  12. u"url": u"http://trailers.apple.com/trailers/wb/manofsteel/",
  13. u"playlist": [
  14. {
  15. u"file": u"manofsteel-trailer4.mov",
  16. u"md5": u"d97a8e575432dbcb81b7c3acb741f8a8",
  17. u"info_dict": {
  18. u"duration": 111,
  19. u"title": u"Trailer 4",
  20. u"upload_date": u"20130523",
  21. u"uploader_id": u"wb",
  22. },
  23. },
  24. {
  25. u"file": u"manofsteel-trailer3.mov",
  26. u"md5": u"b8017b7131b721fb4e8d6f49e1df908c",
  27. u"info_dict": {
  28. u"duration": 182,
  29. u"title": u"Trailer 3",
  30. u"upload_date": u"20130417",
  31. u"uploader_id": u"wb",
  32. },
  33. },
  34. {
  35. u"file": u"manofsteel-trailer.mov",
  36. u"md5": u"d0f1e1150989b9924679b441f3404d48",
  37. u"info_dict": {
  38. u"duration": 148,
  39. u"title": u"Trailer",
  40. u"upload_date": u"20121212",
  41. u"uploader_id": u"wb",
  42. },
  43. },
  44. {
  45. u"file": u"manofsteel-teaser.mov",
  46. u"md5": u"5fe08795b943eb2e757fa95cb6def1cb",
  47. u"info_dict": {
  48. u"duration": 93,
  49. u"title": u"Teaser",
  50. u"upload_date": u"20120721",
  51. u"uploader_id": u"wb",
  52. },
  53. }
  54. ]
  55. }
  56. _JSON_RE = r'iTunes.playURL\((.*?)\);'
  57. def _real_extract(self, url):
  58. mobj = re.match(self._VALID_URL, url)
  59. movie = mobj.group('movie')
  60. uploader_id = mobj.group('company')
  61. playlist_url = compat_urlparse.urljoin(url, u'includes/playlists/itunes.inc')
  62. playlist_snippet = self._download_webpage(playlist_url, movie)
  63. playlist_cleaned = re.sub(r'(?s)<script[^<]*?>.*?</script>', u'', playlist_snippet)
  64. playlist_cleaned = re.sub(r'<img ([^<]*?)>', r'<img \1/>', playlist_cleaned)
  65. # The ' in the onClick attributes are not escaped, it couldn't be parsed
  66. # with xml.etree.ElementTree.fromstring
  67. # like: http://trailers.apple.com/trailers/wb/gravity/
  68. def _clean_json(m):
  69. return u'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
  70. playlist_cleaned = re.sub(self._JSON_RE, _clean_json, playlist_cleaned)
  71. playlist_html = u'<html>' + playlist_cleaned + u'</html>'
  72. doc = xml.etree.ElementTree.fromstring(playlist_html)
  73. playlist = []
  74. for li in doc.findall('./div/ul/li'):
  75. on_click = li.find('.//a').attrib['onClick']
  76. trailer_info_json = self._search_regex(self._JSON_RE,
  77. on_click, u'trailer info')
  78. trailer_info = json.loads(trailer_info_json)
  79. title = trailer_info['title']
  80. video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
  81. thumbnail = li.find('.//img').attrib['src']
  82. upload_date = trailer_info['posted'].replace('-', '')
  83. runtime = trailer_info['runtime']
  84. m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
  85. duration = None
  86. if m:
  87. duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
  88. first_url = trailer_info['url']
  89. trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
  90. settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
  91. settings_json = self._download_webpage(settings_json_url, trailer_id, u'Downloading settings json')
  92. settings = json.loads(settings_json)
  93. formats = []
  94. for format in settings['metadata']['sizes']:
  95. # The src is a file pointing to the real video file
  96. format_url = re.sub(r'_(\d*p.mov)', r'_h\1', format['src'])
  97. formats.append({
  98. 'url': format_url,
  99. 'ext': determine_ext(format_url),
  100. 'format': format['type'],
  101. 'width': format['width'],
  102. 'height': int(format['height']),
  103. })
  104. formats = sorted(formats, key=lambda f: (f['height'], f['width']))
  105. info = {
  106. '_type': 'video',
  107. 'id': video_id,
  108. 'title': title,
  109. 'formats': formats,
  110. 'title': title,
  111. 'duration': duration,
  112. 'thumbnail': thumbnail,
  113. 'upload_date': upload_date,
  114. 'uploader_id': uploader_id,
  115. 'user_agent': 'QuickTime compatible (youtube-dl)',
  116. }
  117. # TODO: Remove when #980 has been merged
  118. info['url'] = formats[-1]['url']
  119. info['ext'] = formats[-1]['ext']
  120. playlist.append(info)
  121. return {
  122. '_type': 'playlist',
  123. 'id': movie,
  124. 'entries': playlist,
  125. }