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.

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