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.

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