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.

231 lines
7.8 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. IE_NAME = 'appletrailers'
  11. _VALID_URL = r'https?://(?:www\.|movie)?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/trailers/magnolia/blackthorn/',
  65. 'info_dict': {
  66. 'id': 'blackthorn',
  67. },
  68. 'playlist_mincount': 2,
  69. }, {
  70. 'url': 'http://trailers.apple.com/ca/metropole/autrui/',
  71. 'only_matching': True,
  72. }, {
  73. 'url': 'http://movietrailers.apple.com/trailers/focus_features/kuboandthetwostrings/',
  74. 'only_matching': True,
  75. }]
  76. _JSON_RE = r'iTunes.playURL\((.*?)\);'
  77. def _real_extract(self, url):
  78. mobj = re.match(self._VALID_URL, url)
  79. movie = mobj.group('movie')
  80. uploader_id = mobj.group('company')
  81. playlist_url = compat_urlparse.urljoin(url, 'includes/playlists/itunes.inc')
  82. def fix_html(s):
  83. s = re.sub(r'(?s)<script[^<]*?>.*?</script>', '', s)
  84. s = re.sub(r'<img ([^<]*?)/?>', r'<img \1/>', s)
  85. # The ' in the onClick attributes are not escaped, it couldn't be parsed
  86. # like: http://trailers.apple.com/trailers/wb/gravity/
  87. def _clean_json(m):
  88. return 'iTunes.playURL(%s);' % m.group(1).replace('\'', '&#39;')
  89. s = re.sub(self._JSON_RE, _clean_json, s)
  90. s = '<html>%s</html>' % s
  91. return s
  92. doc = self._download_xml(playlist_url, movie, transform_source=fix_html)
  93. playlist = []
  94. for li in doc.findall('./div/ul/li'):
  95. on_click = li.find('.//a').attrib['onClick']
  96. trailer_info_json = self._search_regex(self._JSON_RE,
  97. on_click, 'trailer info')
  98. trailer_info = json.loads(trailer_info_json)
  99. first_url = trailer_info.get('url')
  100. if not first_url:
  101. continue
  102. title = trailer_info['title']
  103. video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
  104. thumbnail = li.find('.//img').attrib['src']
  105. upload_date = trailer_info['posted'].replace('-', '')
  106. runtime = trailer_info['runtime']
  107. m = re.search(r'(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime)
  108. duration = None
  109. if m:
  110. duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
  111. trailer_id = first_url.split('/')[-1].rpartition('_')[0].lower()
  112. settings_json_url = compat_urlparse.urljoin(url, 'includes/settings/%s.json' % trailer_id)
  113. settings = self._download_json(settings_json_url, trailer_id, 'Downloading settings json')
  114. formats = []
  115. for format in settings['metadata']['sizes']:
  116. # The src is a file pointing to the real video file
  117. format_url = re.sub(r'_(\d*p.mov)', r'_h\1', format['src'])
  118. formats.append({
  119. 'url': format_url,
  120. 'format': format['type'],
  121. 'width': int_or_none(format['width']),
  122. 'height': int_or_none(format['height']),
  123. })
  124. self._sort_formats(formats)
  125. playlist.append({
  126. '_type': 'video',
  127. 'id': video_id,
  128. 'formats': formats,
  129. 'title': title,
  130. 'duration': duration,
  131. 'thumbnail': thumbnail,
  132. 'upload_date': upload_date,
  133. 'uploader_id': uploader_id,
  134. 'http_headers': {
  135. 'User-Agent': 'QuickTime compatible (youtube-dl)',
  136. },
  137. })
  138. return {
  139. '_type': 'playlist',
  140. 'id': movie,
  141. 'entries': playlist,
  142. }
  143. class AppleTrailersSectionIE(InfoExtractor):
  144. IE_NAME = 'appletrailers:section'
  145. _SECTIONS = {
  146. 'justadded': {
  147. 'feed_path': 'just_added',
  148. 'title': 'Just Added',
  149. },
  150. 'exclusive': {
  151. 'feed_path': 'exclusive',
  152. 'title': 'Exclusive',
  153. },
  154. 'justhd': {
  155. 'feed_path': 'just_hd',
  156. 'title': 'Just HD',
  157. },
  158. 'mostpopular': {
  159. 'feed_path': 'most_pop',
  160. 'title': 'Most Popular',
  161. },
  162. 'moviestudios': {
  163. 'feed_path': 'studios',
  164. 'title': 'Movie Studios',
  165. },
  166. }
  167. _VALID_URL = r'https?://(?:www\.)?trailers\.apple\.com/#section=(?P<id>%s)' % '|'.join(_SECTIONS)
  168. _TESTS = [{
  169. 'url': 'http://trailers.apple.com/#section=justadded',
  170. 'info_dict': {
  171. 'title': 'Just Added',
  172. 'id': 'justadded',
  173. },
  174. 'playlist_mincount': 80,
  175. }, {
  176. 'url': 'http://trailers.apple.com/#section=exclusive',
  177. 'info_dict': {
  178. 'title': 'Exclusive',
  179. 'id': 'exclusive',
  180. },
  181. 'playlist_mincount': 80,
  182. }, {
  183. 'url': 'http://trailers.apple.com/#section=justhd',
  184. 'info_dict': {
  185. 'title': 'Just HD',
  186. 'id': 'justhd',
  187. },
  188. 'playlist_mincount': 80,
  189. }, {
  190. 'url': 'http://trailers.apple.com/#section=mostpopular',
  191. 'info_dict': {
  192. 'title': 'Most Popular',
  193. 'id': 'mostpopular',
  194. },
  195. 'playlist_mincount': 80,
  196. }, {
  197. 'url': 'http://trailers.apple.com/#section=moviestudios',
  198. 'info_dict': {
  199. 'title': 'Movie Studios',
  200. 'id': 'moviestudios',
  201. },
  202. 'playlist_mincount': 80,
  203. }]
  204. def _real_extract(self, url):
  205. section = self._match_id(url)
  206. section_data = self._download_json(
  207. 'http://trailers.apple.com/trailers/home/feeds/%s.json' % self._SECTIONS[section]['feed_path'],
  208. section)
  209. entries = [
  210. self.url_result('http://trailers.apple.com' + e['location'])
  211. for e in section_data]
  212. return self.playlist_result(entries, section, self._SECTIONS[section]['title'])