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.

167 lines
6.7 KiB

  1. import re
  2. import xml.etree.ElementTree
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. determine_ext,
  6. ExtractorError,
  7. )
  8. class AppleTrailersIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:www\.)?trailers.apple.com/trailers/(?P<company>[^/]+)/(?P<movie>[^/]+)'
  10. _TEST = {
  11. u"url": u"http://trailers.apple.com/trailers/wb/manofsteel/",
  12. u"playlist": [
  13. {
  14. u"file": u"manofsteel-trailer4.mov",
  15. u"md5": u"11874af099d480cc09e103b189805d5f",
  16. u"info_dict": {
  17. u"duration": 111,
  18. u"thumbnail": u"http://trailers.apple.com/trailers/wb/manofsteel/images/thumbnail_11624.jpg",
  19. u"title": u"Trailer 4",
  20. u"upload_date": u"20130523",
  21. u"uploader_id": u"wb",
  22. },
  23. },
  24. {
  25. u"file": u"manofsteel-trailer3.mov",
  26. u"md5": u"07a0a262aae5afe68120eed61137ab34",
  27. u"info_dict": {
  28. u"duration": 182,
  29. u"thumbnail": u"http://trailers.apple.com/trailers/wb/manofsteel/images/thumbnail_10793.jpg",
  30. u"title": u"Trailer 3",
  31. u"upload_date": u"20130417",
  32. u"uploader_id": u"wb",
  33. },
  34. },
  35. {
  36. u"file": u"manofsteel-trailer.mov",
  37. u"md5": u"e401fde0813008e3307e54b6f384cff1",
  38. u"info_dict": {
  39. u"duration": 148,
  40. u"thumbnail": u"http://trailers.apple.com/trailers/wb/manofsteel/images/thumbnail_8703.jpg",
  41. u"title": u"Trailer",
  42. u"upload_date": u"20121212",
  43. u"uploader_id": u"wb",
  44. },
  45. },
  46. {
  47. u"file": u"manofsteel-teaser.mov",
  48. u"md5": u"76b392f2ae9e7c98b22913c10a639c97",
  49. u"info_dict": {
  50. u"duration": 93,
  51. u"thumbnail": u"http://trailers.apple.com/trailers/wb/manofsteel/images/thumbnail_6899.jpg",
  52. u"title": u"Teaser",
  53. u"upload_date": u"20120721",
  54. u"uploader_id": u"wb",
  55. },
  56. }
  57. ]
  58. }
  59. def _real_extract(self, url):
  60. mobj = re.match(self._VALID_URL, url)
  61. movie = mobj.group('movie')
  62. uploader_id = mobj.group('company')
  63. playlist_url = url.partition(u'?')[0] + u'/includes/playlists/web.inc'
  64. playlist_snippet = self._download_webpage(playlist_url, movie)
  65. playlist_cleaned = re.sub(r'(?s)<script>.*?</script>', u'', playlist_snippet)
  66. playlist_html = u'<html>' + playlist_cleaned + u'</html>'
  67. size_cache = {}
  68. doc = xml.etree.ElementTree.fromstring(playlist_html)
  69. playlist = []
  70. for li in doc.findall('./div/ul/li'):
  71. title = li.find('.//h3').text
  72. video_id = movie + '-' + re.sub(r'[^a-zA-Z0-9]', '', title).lower()
  73. thumbnail = li.find('.//img').attrib['src']
  74. date_el = li.find('.//p')
  75. upload_date = None
  76. m = re.search(r':\s?(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/(?P<year>[0-9]{2})', date_el.text)
  77. if m:
  78. upload_date = u'20' + m.group('year') + m.group('month') + m.group('day')
  79. runtime_el = date_el.find('./br')
  80. m = re.search(r':\s?(?P<minutes>[0-9]+):(?P<seconds>[0-9]{1,2})', runtime_el.tail)
  81. duration = None
  82. if m:
  83. duration = 60 * int(m.group('minutes')) + int(m.group('seconds'))
  84. formats = []
  85. for formats_el in li.findall('.//a'):
  86. if formats_el.attrib['class'] != 'OverlayPanel':
  87. continue
  88. target = formats_el.attrib['target']
  89. format_code = formats_el.text
  90. if 'Automatic' in format_code:
  91. continue
  92. size_q = formats_el.attrib['href']
  93. size_id = size_q.rpartition('#videos-')[2]
  94. if size_id not in size_cache:
  95. size_url = url + size_q
  96. sizepage_html = self._download_webpage(
  97. size_url, movie,
  98. note=u'Downloading size info %s' % size_id,
  99. errnote=u'Error while downloading size info %s' % size_id,
  100. )
  101. _doc = xml.etree.ElementTree.fromstring(sizepage_html)
  102. size_cache[size_id] = _doc
  103. sizepage_doc = size_cache[size_id]
  104. links = sizepage_doc.findall('.//{http://www.w3.org/1999/xhtml}ul/{http://www.w3.org/1999/xhtml}li/{http://www.w3.org/1999/xhtml}a')
  105. for vid_a in links:
  106. href = vid_a.get('href')
  107. if not href.endswith(target):
  108. continue
  109. detail_q = href.partition('#')[0]
  110. detail_url = url + '/' + detail_q
  111. m = re.match(r'includes/(?P<detail_id>[^/]+)/', detail_q)
  112. detail_id = m.group('detail_id')
  113. detail_html = self._download_webpage(
  114. detail_url, movie,
  115. note=u'Downloading detail %s %s' % (detail_id, size_id),
  116. errnote=u'Error while downloading detail %s %s' % (detail_id, size_id)
  117. )
  118. detail_doc = xml.etree.ElementTree.fromstring(detail_html)
  119. movie_link_el = detail_doc.find('.//{http://www.w3.org/1999/xhtml}a')
  120. assert movie_link_el.get('class') == 'movieLink'
  121. movie_link = movie_link_el.get('href').partition('?')[0].replace('_', '_h')
  122. ext = determine_ext(movie_link)
  123. assert ext == 'mov'
  124. formats.append({
  125. 'format': format_code,
  126. 'ext': ext,
  127. 'url': movie_link,
  128. })
  129. info = {
  130. '_type': 'video',
  131. 'id': video_id,
  132. 'title': title,
  133. 'formats': formats,
  134. 'title': title,
  135. 'duration': duration,
  136. 'thumbnail': thumbnail,
  137. 'upload_date': upload_date,
  138. 'uploader_id': uploader_id,
  139. 'user_agent': 'QuickTime compatible (youtube-dl)',
  140. }
  141. # TODO: Remove when #980 has been merged
  142. info['url'] = formats[-1]['url']
  143. info['ext'] = formats[-1]['ext']
  144. playlist.append(info)
  145. return {
  146. '_type': 'playlist',
  147. 'id': movie,
  148. 'entries': playlist,
  149. }