appletrailers.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import re
  2. import json
  3. from .common import InfoExtractor
  4. from ..compat import compat_urlparse
  5. from ..utils import (
  6. int_or_none,
  7. parse_duration,
  8. unified_strdate,
  9. )
  10. class AppleTrailersIE(InfoExtractor):
  11. IE_NAME = 'appletrailers'
  12. _VALID_URL = r'https?://(?:www\.|movie)?trailers\.apple\.com/(?:trailers|ca)/(?P<company>[^/]+)/(?P<movie>[^/]+)'
  13. _TESTS = [{
  14. 'url': 'http://trailers.apple.com/trailers/wb/manofsteel/',
  15. 'info_dict': {
  16. 'id': '5111',
  17. 'title': 'Man of Steel',
  18. },
  19. 'playlist': [
  20. {
  21. 'md5': 'd97a8e575432dbcb81b7c3acb741f8a8',
  22. 'info_dict': {
  23. 'id': 'manofsteel-trailer4',
  24. 'ext': 'mov',
  25. 'duration': 111,
  26. 'title': 'Trailer 4',
  27. 'upload_date': '20130523',
  28. 'uploader_id': 'wb',
  29. },
  30. },
  31. {
  32. 'md5': 'b8017b7131b721fb4e8d6f49e1df908c',
  33. 'info_dict': {
  34. 'id': 'manofsteel-trailer3',
  35. 'ext': 'mov',
  36. 'duration': 182,
  37. 'title': 'Trailer 3',
  38. 'upload_date': '20130417',
  39. 'uploader_id': 'wb',
  40. },
  41. },
  42. {
  43. 'md5': 'd0f1e1150989b9924679b441f3404d48',
  44. 'info_dict': {
  45. 'id': 'manofsteel-trailer',
  46. 'ext': 'mov',
  47. 'duration': 148,
  48. 'title': 'Trailer',
  49. 'upload_date': '20121212',
  50. 'uploader_id': 'wb',
  51. },
  52. },
  53. {
  54. 'md5': '5fe08795b943eb2e757fa95cb6def1cb',
  55. 'info_dict': {
  56. 'id': 'manofsteel-teaser',
  57. 'ext': 'mov',
  58. 'duration': 93,
  59. 'title': 'Teaser',
  60. 'upload_date': '20120721',
  61. 'uploader_id': 'wb',
  62. },
  63. },
  64. ]
  65. }, {
  66. 'url': 'http://trailers.apple.com/trailers/magnolia/blackthorn/',
  67. 'info_dict': {
  68. 'id': '4489',
  69. 'title': 'Blackthorn',
  70. },
  71. 'playlist_mincount': 2,
  72. 'expected_warnings': ['Unable to download JSON metadata'],
  73. }, {
  74. # json data only available from http://trailers.apple.com/trailers/feeds/data/15881.json
  75. 'url': 'http://trailers.apple.com/trailers/fox/kungfupanda3/',
  76. 'info_dict': {
  77. 'id': '15881',
  78. 'title': 'Kung Fu Panda 3',
  79. },
  80. 'playlist_mincount': 4,
  81. }, {
  82. 'url': 'http://trailers.apple.com/ca/metropole/autrui/',
  83. 'only_matching': True,
  84. }, {
  85. 'url': 'http://movietrailers.apple.com/trailers/focus_features/kuboandthetwostrings/',
  86. 'only_matching': True,
  87. }]
  88. _JSON_RE = r'iTunes.playURL\((.*?)\);'
  89. def _real_extract(self, url):
  90. mobj = self._match_valid_url(url)
  91. movie = mobj.group('movie')
  92. uploader_id = mobj.group('company')
  93. webpage = self._download_webpage(url, movie)
  94. film_id = self._search_regex(r"FilmId\s*=\s*'(\d+)'", webpage, 'film id')
  95. film_data = self._download_json(
  96. 'http://trailers.apple.com/trailers/feeds/data/%s.json' % film_id,
  97. film_id, fatal=False)
  98. if film_data:
  99. entries = []
  100. for clip in film_data.get('clips', []):
  101. clip_title = clip['title']
  102. formats = []
  103. for version, version_data in clip.get('versions', {}).items():
  104. for size, size_data in version_data.get('sizes', {}).items():
  105. src = size_data.get('src')
  106. if not src:
  107. continue
  108. formats.append({
  109. 'format_id': '%s-%s' % (version, size),
  110. 'url': re.sub(r'_(\d+p\.mov)', r'_h\1', src),
  111. 'width': int_or_none(size_data.get('width')),
  112. 'height': int_or_none(size_data.get('height')),
  113. 'language': version[:2],
  114. })
  115. entries.append({
  116. 'id': movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', clip_title).lower(),
  117. 'formats': formats,
  118. 'title': clip_title,
  119. 'thumbnail': clip.get('screen') or clip.get('thumb'),
  120. 'duration': parse_duration(clip.get('runtime') or clip.get('faded')),
  121. 'upload_date': unified_strdate(clip.get('posted')),
  122. 'uploader_id': uploader_id,
  123. })
  124. page_data = film_data.get('page', {})
  125. return self.playlist_result(entries, film_id, page_data.get('movie_title'))
  126. playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc')
  127. def fix_html(s):
  128. s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s)
  129. s = re.sub(r'<img ([^<]*?)/?>', r'<img \1/>', s)
  130. # The ' in the onClick attributes are not escaped, it couldn't be parsed
  131. # like: http://trailers.apple.com/trailers/wb/gravity/
  132. def _clean_json(m):
  133. return 'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
  134. s = re.sub(self._JSON_RE, _clean_json, s)
  135. s = '<html>%s</html>' % s
  136. return s
  137. doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
  138. playlist = []
  139. for li in doc.findall('./div/ul/li'):
  140. on_click = li.find('.//a').attrib['onClick']
  141. trailer_info_json = self._search_regex(self._JSON_RE,
  142. on_click, 'trailer info')
  143. trailer_info = json.loads(trailer_info_json)
  144. first_url = trailer_info.get('url')
  145. if not first_url:
  146. continue
  147. title = trailer_info['title']
  148. video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
  149. thumbnail = li.find('.//img').attrib['src']
  150. upload_date = trailer_info['posted'].replace('-', '')
  151. runtime = trailer_info['runtime']
  152. m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
  153. duration = None
  154. if m:
  155. duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
  156. trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
  157. settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
  158. settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
  159. formats = []
  160. for format in settings['metadata']['sizes']:
  161. # The src is a file pointing to the real video file
  162. format_url = re.sub(r'_(\d*p\.mov)', r'_h\1', format['src'])
  163. formats.append({
  164. 'url': format_url,
  165. 'format': format['type'],
  166. 'width': int_or_none(format['width']),
  167. 'height': int_or_none(format['height']),
  168. })
  169. playlist.append({
  170. '_type': 'video',
  171. 'id': video_id,
  172. 'formats': formats,
  173. 'title': title,
  174. 'duration': duration,
  175. 'thumbnail': thumbnail,
  176. 'upload_date': upload_date,
  177. 'uploader_id': uploader_id,
  178. 'http_headers': {
  179. 'User-Agent': 'QuickTime compatible (hypervideo)',
  180. },
  181. })
  182. return {
  183. '_type': 'playlist',
  184. 'id': movie,
  185. 'entries': playlist,
  186. }
  187. class AppleTrailersSectionIE(InfoExtractor):
  188. IE_NAME = 'appletrailers:section'
  189. _SECTIONS = {
  190. 'justadded': {
  191. 'feed_path': 'just_added',
  192. 'title': 'Just Added',
  193. },
  194. 'exclusive': {
  195. 'feed_path': 'exclusive',
  196. 'title': 'Exclusive',
  197. },
  198. 'justhd': {
  199. 'feed_path': 'just_hd',
  200. 'title': 'Just HD',
  201. },
  202. 'mostpopular': {
  203. 'feed_path': 'most_pop',
  204. 'title': 'Most Popular',
  205. },
  206. 'moviestudios': {
  207. 'feed_path': 'studios',
  208. 'title': 'Movie Studios',
  209. },
  210. }
  211. _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>%s)' % '|'.join(_SECTIONS)
  212. _TESTS = [{
  213. 'url': 'http://trailers.apple.com/#section=justadded',
  214. 'info_dict': {
  215. 'title': 'Just Added',
  216. 'id': 'justadded',
  217. },
  218. 'playlist_mincount': 80,
  219. }, {
  220. 'url': 'http://trailers.apple.com/#section=exclusive',
  221. 'info_dict': {
  222. 'title': 'Exclusive',
  223. 'id': 'exclusive',
  224. },
  225. 'playlist_mincount': 80,
  226. }, {
  227. 'url': 'http://trailers.apple.com/#section=justhd',
  228. 'info_dict': {
  229. 'title': 'Just HD',
  230. 'id': 'justhd',
  231. },
  232. 'playlist_mincount': 80,
  233. }, {
  234. 'url': 'http://trailers.apple.com/#section=mostpopular',
  235. 'info_dict': {
  236. 'title': 'Most Popular',
  237. 'id': 'mostpopular',
  238. },
  239. 'playlist_mincount': 30,
  240. }, {
  241. 'url': 'http://trailers.apple.com/#section=moviestudios',
  242. 'info_dict': {
  243. 'title': 'Movie Studios',
  244. 'id': 'moviestudios',
  245. },
  246. 'playlist_mincount': 80,
  247. }]
  248. def _real_extract(self, url):
  249. section = self._match_id(url)
  250. section_data = self._download_json(
  251. 'http://trailers.apple.com/trailers/home/feeds/%s.json' % self._SECTIONS[section]['feed_path'],
  252. section)
  253. entries = [
  254. self.url_result('http://trailers.apple.com' + e['location'])
  255. for e in section_data]
  256. return self.playlist_result(entries, section, self._SECTIONS[section]['title'])