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.

143 lines
5.1 KiB

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