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.

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