adobetv.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. import functools
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. float_or_none,
  7. int_or_none,
  8. ISO639Utils,
  9. join_nonempty,
  10. OnDemandPagedList,
  11. parse_duration,
  12. str_or_none,
  13. str_to_int,
  14. unified_strdate,
  15. )
  16. class AdobeTVBaseIE(InfoExtractor):
  17. def _call_api(self, path, video_id, query, note=None):
  18. return self._download_json(
  19. 'http://tv.adobe.com/api/v4/' + path,
  20. video_id, note, query=query)['data']
  21. def _parse_subtitles(self, video_data, url_key):
  22. subtitles = {}
  23. for translation in video_data.get('translations', []):
  24. vtt_path = translation.get(url_key)
  25. if not vtt_path:
  26. continue
  27. lang = translation.get('language_w3c') or ISO639Utils.long2short(translation['language_medium'])
  28. subtitles.setdefault(lang, []).append({
  29. 'ext': 'vtt',
  30. 'url': vtt_path,
  31. })
  32. return subtitles
  33. def _parse_video_data(self, video_data):
  34. video_id = compat_str(video_data['id'])
  35. title = video_data['title']
  36. s3_extracted = False
  37. formats = []
  38. for source in video_data.get('videos', []):
  39. source_url = source.get('url')
  40. if not source_url:
  41. continue
  42. f = {
  43. 'format_id': source.get('quality_level'),
  44. 'fps': int_or_none(source.get('frame_rate')),
  45. 'height': int_or_none(source.get('height')),
  46. 'tbr': int_or_none(source.get('video_data_rate')),
  47. 'width': int_or_none(source.get('width')),
  48. 'url': source_url,
  49. }
  50. original_filename = source.get('original_filename')
  51. if original_filename:
  52. if not (f.get('height') and f.get('width')):
  53. mobj = re.search(r'_(\d+)x(\d+)', original_filename)
  54. if mobj:
  55. f.update({
  56. 'height': int(mobj.group(2)),
  57. 'width': int(mobj.group(1)),
  58. })
  59. if original_filename.startswith('s3://') and not s3_extracted:
  60. formats.append({
  61. 'format_id': 'original',
  62. 'quality': 1,
  63. 'url': original_filename.replace('s3://', 'https://s3.amazonaws.com/'),
  64. })
  65. s3_extracted = True
  66. formats.append(f)
  67. return {
  68. 'id': video_id,
  69. 'title': title,
  70. 'description': video_data.get('description'),
  71. 'thumbnail': video_data.get('thumbnail'),
  72. 'upload_date': unified_strdate(video_data.get('start_date')),
  73. 'duration': parse_duration(video_data.get('duration')),
  74. 'view_count': str_to_int(video_data.get('playcount')),
  75. 'formats': formats,
  76. 'subtitles': self._parse_subtitles(video_data, 'vtt'),
  77. }
  78. class AdobeTVEmbedIE(AdobeTVBaseIE):
  79. IE_NAME = 'adobetv:embed'
  80. _VALID_URL = r'https?://tv\.adobe\.com/embed/\d+/(?P<id>\d+)'
  81. _TEST = {
  82. 'url': 'https://tv.adobe.com/embed/22/4153',
  83. 'md5': 'c8c0461bf04d54574fc2b4d07ac6783a',
  84. 'info_dict': {
  85. 'id': '4153',
  86. 'ext': 'flv',
  87. 'title': 'Creating Graphics Optimized for BlackBerry',
  88. 'description': 'md5:eac6e8dced38bdaae51cd94447927459',
  89. 'thumbnail': r're:https?://.*\.jpg$',
  90. 'upload_date': '20091109',
  91. 'duration': 377,
  92. 'view_count': int,
  93. },
  94. }
  95. def _real_extract(self, url):
  96. video_id = self._match_id(url)
  97. video_data = self._call_api(
  98. 'episode/' + video_id, video_id, {'disclosure': 'standard'})[0]
  99. return self._parse_video_data(video_data)
  100. class AdobeTVIE(AdobeTVBaseIE):
  101. IE_NAME = 'adobetv'
  102. _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?watch/(?P<show_urlname>[^/]+)/(?P<id>[^/]+)'
  103. _TEST = {
  104. 'url': 'http://tv.adobe.com/watch/the-complete-picture-with-julieanne-kost/quick-tip-how-to-draw-a-circle-around-an-object-in-photoshop/',
  105. 'md5': '9bc5727bcdd55251f35ad311ca74fa1e',
  106. 'info_dict': {
  107. 'id': '10981',
  108. 'ext': 'mp4',
  109. 'title': 'Quick Tip - How to Draw a Circle Around an Object in Photoshop',
  110. 'description': 'md5:99ec318dc909d7ba2a1f2b038f7d2311',
  111. 'thumbnail': r're:https?://.*\.jpg$',
  112. 'upload_date': '20110914',
  113. 'duration': 60,
  114. 'view_count': int,
  115. },
  116. }
  117. def _real_extract(self, url):
  118. language, show_urlname, urlname = self._match_valid_url(url).groups()
  119. if not language:
  120. language = 'en'
  121. video_data = self._call_api(
  122. 'episode/get', urlname, {
  123. 'disclosure': 'standard',
  124. 'language': language,
  125. 'show_urlname': show_urlname,
  126. 'urlname': urlname,
  127. })[0]
  128. return self._parse_video_data(video_data)
  129. class AdobeTVPlaylistBaseIE(AdobeTVBaseIE):
  130. _PAGE_SIZE = 25
  131. def _fetch_page(self, display_id, query, page):
  132. page += 1
  133. query['page'] = page
  134. for element_data in self._call_api(
  135. self._RESOURCE, display_id, query, 'Download Page %d' % page):
  136. yield self._process_data(element_data)
  137. def _extract_playlist_entries(self, display_id, query):
  138. return OnDemandPagedList(functools.partial(
  139. self._fetch_page, display_id, query), self._PAGE_SIZE)
  140. class AdobeTVShowIE(AdobeTVPlaylistBaseIE):
  141. IE_NAME = 'adobetv:show'
  142. _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?show/(?P<id>[^/]+)'
  143. _TEST = {
  144. 'url': 'http://tv.adobe.com/show/the-complete-picture-with-julieanne-kost',
  145. 'info_dict': {
  146. 'id': '36',
  147. 'title': 'The Complete Picture with Julieanne Kost',
  148. 'description': 'md5:fa50867102dcd1aa0ddf2ab039311b27',
  149. },
  150. 'playlist_mincount': 136,
  151. }
  152. _RESOURCE = 'episode'
  153. _process_data = AdobeTVBaseIE._parse_video_data
  154. def _real_extract(self, url):
  155. language, show_urlname = self._match_valid_url(url).groups()
  156. if not language:
  157. language = 'en'
  158. query = {
  159. 'disclosure': 'standard',
  160. 'language': language,
  161. 'show_urlname': show_urlname,
  162. }
  163. show_data = self._call_api(
  164. 'show/get', show_urlname, query)[0]
  165. return self.playlist_result(
  166. self._extract_playlist_entries(show_urlname, query),
  167. str_or_none(show_data.get('id')),
  168. show_data.get('show_name'),
  169. show_data.get('show_description'))
  170. class AdobeTVChannelIE(AdobeTVPlaylistBaseIE):
  171. IE_NAME = 'adobetv:channel'
  172. _VALID_URL = r'https?://tv\.adobe\.com/(?:(?P<language>fr|de|es|jp)/)?channel/(?P<id>[^/]+)(?:/(?P<category_urlname>[^/]+))?'
  173. _TEST = {
  174. 'url': 'http://tv.adobe.com/channel/development',
  175. 'info_dict': {
  176. 'id': 'development',
  177. },
  178. 'playlist_mincount': 96,
  179. }
  180. _RESOURCE = 'show'
  181. def _process_data(self, show_data):
  182. return self.url_result(
  183. show_data['url'], 'AdobeTVShow', str_or_none(show_data.get('id')))
  184. def _real_extract(self, url):
  185. language, channel_urlname, category_urlname = self._match_valid_url(url).groups()
  186. if not language:
  187. language = 'en'
  188. query = {
  189. 'channel_urlname': channel_urlname,
  190. 'language': language,
  191. }
  192. if category_urlname:
  193. query['category_urlname'] = category_urlname
  194. return self.playlist_result(
  195. self._extract_playlist_entries(channel_urlname, query),
  196. channel_urlname)
  197. class AdobeTVVideoIE(AdobeTVBaseIE):
  198. IE_NAME = 'adobetv:video'
  199. _VALID_URL = r'https?://video\.tv\.adobe\.com/v/(?P<id>\d+)'
  200. _EMBED_REGEX = [r'<iframe[^>]+src=[\'"](?P<url>(?:https?:)?//video\.tv\.adobe\.com/v/\d+[^"]+)[\'"]']
  201. _TEST = {
  202. # From https://helpx.adobe.com/acrobat/how-to/new-experience-acrobat-dc.html?set=acrobat--get-started--essential-beginners
  203. 'url': 'https://video.tv.adobe.com/v/2456/',
  204. 'md5': '43662b577c018ad707a63766462b1e87',
  205. 'info_dict': {
  206. 'id': '2456',
  207. 'ext': 'mp4',
  208. 'title': 'New experience with Acrobat DC',
  209. 'description': 'New experience with Acrobat DC',
  210. 'duration': 248.667,
  211. },
  212. }
  213. def _real_extract(self, url):
  214. video_id = self._match_id(url)
  215. webpage = self._download_webpage(url, video_id)
  216. video_data = self._parse_json(self._search_regex(
  217. r'var\s+bridge\s*=\s*([^;]+);', webpage, 'bridged data'), video_id)
  218. title = video_data['title']
  219. formats = []
  220. sources = video_data.get('sources') or []
  221. for source in sources:
  222. source_src = source.get('src')
  223. if not source_src:
  224. continue
  225. formats.append({
  226. 'filesize': int_or_none(source.get('kilobytes') or None, invscale=1000),
  227. 'format_id': join_nonempty(source.get('format'), source.get('label')),
  228. 'height': int_or_none(source.get('height') or None),
  229. 'tbr': int_or_none(source.get('bitrate') or None),
  230. 'width': int_or_none(source.get('width') or None),
  231. 'url': source_src,
  232. })
  233. # For both metadata and downloaded files the duration varies among
  234. # formats. I just pick the max one
  235. duration = max(filter(None, [
  236. float_or_none(source.get('duration'), scale=1000)
  237. for source in sources]))
  238. return {
  239. 'id': video_id,
  240. 'formats': formats,
  241. 'title': title,
  242. 'description': video_data.get('description'),
  243. 'thumbnail': video_data.get('video', {}).get('poster'),
  244. 'duration': duration,
  245. 'subtitles': self._parse_subtitles(video_data, 'vttPath'),
  246. }