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.

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