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.

146 lines
5.2 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|ca)/(?P<company>[^/]+)/(?P<movie>[^/]+)'
  11. _TESTS = [{
  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. 'url': 'http://trailers.apple.com/ca/metropole/autrui/',
  64. 'only_matching': True,
  65. }]
  66. _JSON_RE = r'iTunes.playURL\((.*?)\);'
  67. def _real_extract(self, url):
  68. mobj = re.match(self._VALID_URL, url)
  69. movie = mobj.group('movie')
  70. uploader_id = mobj.group('company')
  71. playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc')
  72. def fix_html(s):
  73. s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s)
  74. s = re.sub(r'<img ([^<]*?)>', r'<img \1/>', s)
  75. # The ' in the onClick attributes are not escaped, it couldn't be parsed
  76. # like: http://trailers.apple.com/trailers/wb/gravity/
  77. def _clean_json(m):
  78. return 'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
  79. s = re.sub(self._JSON_RE, _clean_json, s)
  80. s = '<html>%s</html>' % s
  81. return s
  82. doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
  83. playlist = []
  84. for li in doc.findall('./div/ul/li'):
  85. on_click = li.find('.//a').attrib['onClick']
  86. trailer_info_json = self._search_regex(self._JSON_RE,
  87. on_click, 'trailer info')
  88. trailer_info = json.loads(trailer_info_json)
  89. title = trailer_info['title']
  90. video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
  91. thumbnail = li.find('.//img').attrib['src']
  92. upload_date = trailer_info['posted'].replace('-', '')
  93. runtime = trailer_info['runtime']
  94. m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
  95. duration = None
  96. if m:
  97. duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
  98. first_url = trailer_info['url']
  99. trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
  100. settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
  101. settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
  102. formats = []
  103. for format in settings['metadata']['sizes']:
  104. # The src is a file pointing to the real video file
  105. format_url = re.sub(r'_(\d*p.mov)', r'_h\1', format['src'])
  106. formats.append({
  107. 'url': format_url,
  108. 'format': format['type'],
  109. 'width': int_or_none(format['width']),
  110. 'height': int_or_none(format['height']),
  111. })
  112. self._sort_formats(formats)
  113. playlist.append({
  114. '_type': 'video',
  115. 'id': video_id,
  116. 'formats': formats,
  117. 'title': title,
  118. 'duration': duration,
  119. 'thumbnail': thumbnail,
  120. 'upload_date': upload_date,
  121. 'uploader_id': uploader_id,
  122. 'http_headers': {
  123. 'User-Agent': 'QuickTime compatible (youtube-dl)',
  124. },
  125. })
  126. return {
  127. '_type': 'playlist',
  128. 'id': movie,
  129. 'entries': playlist,
  130. }