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.

132 lines
5.0 KiB

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