appletrailers.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import json
  2. import re
  3. import urllib.parse
  4. from .common import InfoExtractor
  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. f'http://trailers.apple.com/trailers/feeds/data/{film_id}.json',
  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': f'{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 = urllib.parse.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({});'.format(m.group(1).replace('\'', '&#39;'))
  134. s = re.sub(self._JSON_RE, _clean_json, s)
  135. return f'<html>{s}</html>'
  136. doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
  137. playlist = []
  138. for li in doc.findall('./div/ul/li'):
  139. on_click = li.find('.//a').attrib['onClick']
  140. trailer_info_json = self._search_regex(self._JSON_RE,
  141. on_click, 'trailer info')
  142. trailer_info = json.loads(trailer_info_json)
  143. first_url = trailer_info.get('url')
  144. if not first_url:
  145. continue
  146. title = trailer_info['title']
  147. video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
  148. thumbnail = li.find('.//img').attrib['src']
  149. upload_date = trailer_info['posted'].replace('-', '')
  150. runtime = trailer_info['runtime']
  151. m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
  152. duration = None
  153. if m:
  154. duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
  155. trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
  156. settings_json_url = urllib.parse.urljoin(url, f'includes/settings/{trailer_id}.json')
  157. settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
  158. formats = []
  159. for fmt in settings['metadata']['sizes']:
  160. # The src is a file pointing to the real video file
  161. format_url = re.sub(r'_(\d*p\.mov)', r'_h\1', fmt['src'])
  162. formats.append({
  163. 'url': format_url,
  164. 'format': fmt['type'],
  165. 'width': int_or_none(fmt['width']),
  166. 'height': int_or_none(fmt['height']),
  167. })
  168. playlist.append({
  169. '_type': 'video',
  170. 'id': video_id,
  171. 'formats': formats,
  172. 'title': title,
  173. 'duration': duration,
  174. 'thumbnail': thumbnail,
  175. 'upload_date': upload_date,
  176. 'uploader_id': uploader_id,
  177. 'http_headers': {
  178. 'User-Agent': 'QuickTime compatible (yt-dlp)',
  179. },
  180. })
  181. return {
  182. '_type': 'playlist',
  183. 'id': movie,
  184. 'entries': playlist,
  185. }
  186. class AppleTrailersSectionIE(InfoExtractor):
  187. IE_NAME = 'appletrailers:section'
  188. _SECTIONS = {
  189. 'justadded': {
  190. 'feed_path': 'just_added',
  191. 'title': 'Just Added',
  192. },
  193. 'exclusive': {
  194. 'feed_path': 'exclusive',
  195. 'title': 'Exclusive',
  196. },
  197. 'justhd': {
  198. 'feed_path': 'just_hd',
  199. 'title': 'Just HD',
  200. },
  201. 'mostpopular': {
  202. 'feed_path': 'most_pop',
  203. 'title': 'Most Popular',
  204. },
  205. 'moviestudios': {
  206. 'feed_path': 'studios',
  207. 'title': 'Movie Studios',
  208. },
  209. }
  210. _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>{})'.format('|'.join(_SECTIONS))
  211. _TESTS = [{
  212. 'url': 'http://trailers.apple.com/#section=justadded',
  213. 'info_dict': {
  214. 'title': 'Just Added',
  215. 'id': 'justadded',
  216. },
  217. 'playlist_mincount': 80,
  218. }, {
  219. 'url': 'http://trailers.apple.com/#section=exclusive',
  220. 'info_dict': {
  221. 'title': 'Exclusive',
  222. 'id': 'exclusive',
  223. },
  224. 'playlist_mincount': 80,
  225. }, {
  226. 'url': 'http://trailers.apple.com/#section=justhd',
  227. 'info_dict': {
  228. 'title': 'Just HD',
  229. 'id': 'justhd',
  230. },
  231. 'playlist_mincount': 80,
  232. }, {
  233. 'url': 'http://trailers.apple.com/#section=mostpopular',
  234. 'info_dict': {
  235. 'title': 'Most Popular',
  236. 'id': 'mostpopular',
  237. },
  238. 'playlist_mincount': 30,
  239. }, {
  240. 'url': 'http://trailers.apple.com/#section=moviestudios',
  241. 'info_dict': {
  242. 'title': 'Movie Studios',
  243. 'id': 'moviestudios',
  244. },
  245. 'playlist_mincount': 80,
  246. }]
  247. def _real_extract(self, url):
  248. section = self._match_id(url)
  249. section_data = self._download_json(
  250. 'http://trailers.apple.com/trailers/home/feeds/{}.json'.format(self._SECTIONS[section]['feed_path']),
  251. section)
  252. entries = [
  253. self.url_result('http://trailers.apple.com' + e['location'])
  254. for e in section_data]
  255. return self.playlist_result(entries, section, self._SECTIONS[section]['title'])