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.

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