appletrailers.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. IE_NAME = 'appletrailers'
  11. _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/(?:trailers|ca)/(?P<company>[^/]+)/(?P<movie>[^/]+)'
  12. _TESTS = [{
  13. 'url': 'http://trailers.apple.com/trailers/wb/manofsteel/',
  14. 'info_dict': {
  15. 'id': 'manofsteel',
  16. },
  17. 'playlist': [
  18. {
  19. 'md5': 'd97a8e575432dbcb81b7c3acb741f8a8',
  20. 'info_dict': {
  21. 'id': 'manofsteel-trailer4',
  22. 'ext': 'mov',
  23. 'duration': 111,
  24. 'title': 'Trailer 4',
  25. 'upload_date': '20130523',
  26. 'uploader_id': 'wb',
  27. },
  28. },
  29. {
  30. 'md5': 'b8017b7131b721fb4e8d6f49e1df908c',
  31. 'info_dict': {
  32. 'id': 'manofsteel-trailer3',
  33. 'ext': 'mov',
  34. 'duration': 182,
  35. 'title': 'Trailer 3',
  36. 'upload_date': '20130417',
  37. 'uploader_id': 'wb',
  38. },
  39. },
  40. {
  41. 'md5': 'd0f1e1150989b9924679b441f3404d48',
  42. 'info_dict': {
  43. 'id': 'manofsteel-trailer',
  44. 'ext': 'mov',
  45. 'duration': 148,
  46. 'title': 'Trailer',
  47. 'upload_date': '20121212',
  48. 'uploader_id': 'wb',
  49. },
  50. },
  51. {
  52. 'md5': '5fe08795b943eb2e757fa95cb6def1cb',
  53. 'info_dict': {
  54. 'id': 'manofsteel-teaser',
  55. 'ext': 'mov',
  56. 'duration': 93,
  57. 'title': 'Teaser',
  58. 'upload_date': '20120721',
  59. 'uploader_id': 'wb',
  60. },
  61. },
  62. ]
  63. }, {
  64. 'url': 'http://trailers.apple.com/ca/metropole/autrui/',
  65. 'only_matching': True,
  66. }]
  67. _JSON_RE = r'iTunes.playURL\((.*?)\);'
  68. def _real_extract(self, url):
  69. mobj = re.match(self._VALID_URL, url)
  70. movie = mobj.group('movie')
  71. uploader_id = mobj.group('company')
  72. playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc')
  73. def fix_html(s):
  74. s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s)
  75. s = re.sub(r'<img ([^<]*?)>', r'<img \1/>', s)
  76. # The ' in the onClick attributes are not escaped, it couldn't be parsed
  77. # like: http://trailers.apple.com/trailers/wb/gravity/
  78. def _clean_json(m):
  79. return 'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
  80. s = re.sub(self._JSON_RE, _clean_json, s)
  81. s = '<html>%s</html>' % s
  82. return s
  83. doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
  84. playlist = []
  85. for li in doc.findall('./div/ul/li'):
  86. on_click = li.find('.//a').attrib['onClick']
  87. trailer_info_json = self._search_regex(self._JSON_RE,
  88. on_click, 'trailer info')
  89. trailer_info = json.loads(trailer_info_json)
  90. title = trailer_info['title']
  91. video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
  92. thumbnail = li.find('.//img').attrib['src']
  93. upload_date = trailer_info['posted'].replace('-', '')
  94. runtime = trailer_info['runtime']
  95. m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
  96. duration = None
  97. if m:
  98. duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
  99. first_url = trailer_info['url']
  100. trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
  101. settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
  102. settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
  103. formats = []
  104. for format in settings['metadata']['sizes']:
  105. # The src is a file pointing to the real video file
  106. format_url = re.sub(r'_(\d*p.mov)', r'_h\1', format['src'])
  107. formats.append({
  108. 'url': format_url,
  109. 'format': format['type'],
  110. 'width': int_or_none(format['width']),
  111. 'height': int_or_none(format['height']),
  112. })
  113. self._sort_formats(formats)
  114. playlist.append({
  115. '_type': 'video',
  116. 'id': video_id,
  117. 'formats': formats,
  118. 'title': title,
  119. 'duration': duration,
  120. 'thumbnail': thumbnail,
  121. 'upload_date': upload_date,
  122. 'uploader_id': uploader_id,
  123. 'http_headers': {
  124. 'User-Agent': 'QuickTime compatible (youtube-dl)',
  125. },
  126. })
  127. return {
  128. '_type': 'playlist',
  129. 'id': movie,
  130. 'entries': playlist,
  131. }
  132. class AppleTrailersSectionIE(InfoExtractor):
  133. IE_NAME = 'appletrailers:section'
  134. _SECTIONS = {
  135. 'justadded': {
  136. 'feed_path': 'just_added',
  137. 'title': 'Just Added',
  138. },
  139. 'exclusive': {
  140. 'feed_path': 'exclusive',
  141. 'title': 'Exclusive',
  142. },
  143. 'justhd': {
  144. 'feed_path': 'just_hd',
  145. 'title': 'Just HD',
  146. },
  147. 'mostpopular': {
  148. 'feed_path': 'most_pop',
  149. 'title': 'Most Popular',
  150. },
  151. 'moviestudios': {
  152. 'feed_path': 'studios',
  153. 'title': 'Movie Studios',
  154. },
  155. }
  156. _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>%s)' % '|'.join(_SECTIONS)
  157. _TESTS = [{
  158. 'url': 'http://trailers.apple.com/#section=justadded',
  159. 'info_dict': {
  160. 'title': 'Just Added',
  161. 'id': 'justadded',
  162. },
  163. 'playlist_mincount': 80,
  164. }, {
  165. 'url': 'http://trailers.apple.com/#section=exclusive',
  166. 'info_dict': {
  167. 'title': 'Exclusive',
  168. 'id': 'exclusive',
  169. },
  170. 'playlist_mincount': 80,
  171. }, {
  172. 'url': 'http://trailers.apple.com/#section=justhd',
  173. 'info_dict': {
  174. 'title': 'Just HD',
  175. 'id': 'justhd',
  176. },
  177. 'playlist_mincount': 80,
  178. }, {
  179. 'url': 'http://trailers.apple.com/#section=mostpopular',
  180. 'info_dict': {
  181. 'title': 'Most Popular',
  182. 'id': 'mostpopular',
  183. },
  184. 'playlist_mincount': 80,
  185. }, {
  186. 'url': 'http://trailers.apple.com/#section=moviestudios',
  187. 'info_dict': {
  188. 'title': 'Movie Studios',
  189. 'id': 'moviestudios',
  190. },
  191. 'playlist_mincount': 80,
  192. }]
  193. def _real_extract(self, url):
  194. section = self._match_id(url)
  195. section_data = self._download_json(
  196. 'http://trailers.apple.com/trailers/home/feeds/%s.json' % self._SECTIONS[section]['feed_path'],
  197. section)
  198. entries = [
  199. self.url_result('http://trailers.apple.com' + e['location'])
  200. for e in section_data]
  201. return self.playlist_result(entries, section, self._SECTIONS[section]['title'])