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.

135 lines
4.9 KiB

  1. from __future__ import unicode_literals
  2. import re
  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. "url": "http://trailers.apple.com/trailers/wb/manofsteel/",
  13. "playlist": [
  14. {
  15. "file": "manofsteel-trailer4.mov",
  16. "md5": "d97a8e575432dbcb81b7c3acb741f8a8",
  17. "info_dict": {
  18. "duration": 111,
  19. "title": "Trailer 4",
  20. "upload_date": "20130523",
  21. "uploader_id": "wb",
  22. },
  23. },
  24. {
  25. "file": "manofsteel-trailer3.mov",
  26. "md5": "b8017b7131b721fb4e8d6f49e1df908c",
  27. "info_dict": {
  28. "duration": 182,
  29. "title": "Trailer 3",
  30. "upload_date": "20130417",
  31. "uploader_id": "wb",
  32. },
  33. },
  34. {
  35. "file": "manofsteel-trailer.mov",
  36. "md5": "d0f1e1150989b9924679b441f3404d48",
  37. "info_dict": {
  38. "duration": 148,
  39. "title": "Trailer",
  40. "upload_date": "20121212",
  41. "uploader_id": "wb",
  42. },
  43. },
  44. {
  45. "file": "manofsteel-teaser.mov",
  46. "md5": "5fe08795b943eb2e757fa95cb6def1cb",
  47. "info_dict": {
  48. "duration": 93,
  49. "title": "Teaser",
  50. "upload_date": "20120721",
  51. "uploader_id": "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. def fix_html(s):
  63. s = re.sub(r'(?s)<script[^<]*?>.*?</script>', u'', s)
  64. s = re.sub(r'<img ([^<]*?)>', r'<img \1/>', s)
  65. # The ' in the onClick attributes are not escaped, it couldn't be parsed
  66. # like: http://trailers.apple.com/trailers/wb/gravity/
  67. def _clean_json(m):
  68. return u'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
  69. s = re.sub(self._JSON_RE, _clean_json, s)
  70. s = u'<html>' + s + u'</html>'
  71. return s
  72. doc = self._download_xml(playlist_url, movie, transform_source=fix_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. self._sort_formats(formats)
  105. playlist.append({
  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. return {
  118. '_type': 'playlist',
  119. 'id': movie,
  120. 'entries': playlist,
  121. }