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.

268 lines
11 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .adobepass import AdobePassIE
  5. from ..utils import (
  6. int_or_none,
  7. determine_ext,
  8. parse_age_limit,
  9. urlencode_postdata,
  10. ExtractorError,
  11. )
  12. class GoIE(AdobePassIE):
  13. _SITE_INFO = {
  14. 'abc': {
  15. 'brand': '001',
  16. 'requestor_id': 'ABC',
  17. },
  18. 'freeform': {
  19. 'brand': '002',
  20. 'requestor_id': 'ABCFamily',
  21. },
  22. 'watchdisneychannel': {
  23. 'brand': '004',
  24. 'resource_id': 'Disney',
  25. },
  26. 'watchdisneyjunior': {
  27. 'brand': '008',
  28. 'resource_id': 'DisneyJunior',
  29. },
  30. 'watchdisneyxd': {
  31. 'brand': '009',
  32. 'resource_id': 'DisneyXD',
  33. },
  34. 'disneynow': {
  35. 'brand': '011',
  36. 'resource_id': 'Disney',
  37. }
  38. }
  39. _VALID_URL = r'''(?x)
  40. https?://
  41. (?:
  42. (?:(?P<sub_domain>%s)\.)?go|
  43. (?P<sub_domain_2>abc|freeform|disneynow)
  44. )\.com/
  45. (?:
  46. (?:[^/]+/)*(?P<id>[Vv][Dd][Kk][Aa]\w+)|
  47. (?:[^/]+/)*(?P<display_id>[^/?\#]+)
  48. )
  49. ''' % '|'.join(list(_SITE_INFO.keys()))
  50. _TESTS = [{
  51. 'url': 'http://abc.go.com/shows/designated-survivor/video/most-recent/VDKA3807643',
  52. 'info_dict': {
  53. 'id': 'VDKA3807643',
  54. 'ext': 'mp4',
  55. 'title': 'The Traitor in the White House',
  56. 'description': 'md5:05b009d2d145a1e85d25111bd37222e8',
  57. },
  58. 'params': {
  59. # m3u8 download
  60. 'skip_download': True,
  61. },
  62. 'skip': 'This content is no longer available.',
  63. }, {
  64. 'url': 'http://watchdisneyxd.go.com/doraemon',
  65. 'info_dict': {
  66. 'title': 'Doraemon',
  67. 'id': 'SH55574025',
  68. },
  69. 'playlist_mincount': 51,
  70. }, {
  71. 'url': 'http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood',
  72. 'info_dict': {
  73. 'id': 'VDKA3609139',
  74. 'ext': 'mp4',
  75. 'title': 'This Guilty Blood',
  76. 'description': 'md5:f18e79ad1c613798d95fdabfe96cd292',
  77. 'age_limit': 14,
  78. },
  79. 'params': {
  80. 'geo_bypass_ip_block': '3.244.239.0/24',
  81. # m3u8 download
  82. 'skip_download': True,
  83. },
  84. }, {
  85. 'url': 'https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet',
  86. 'info_dict': {
  87. 'id': 'VDKA13435179',
  88. 'ext': 'mp4',
  89. 'title': 'The Bet',
  90. 'description': 'md5:c66de8ba2e92c6c5c113c3ade84ab404',
  91. 'age_limit': 14,
  92. },
  93. 'params': {
  94. 'geo_bypass_ip_block': '3.244.239.0/24',
  95. # m3u8 download
  96. 'skip_download': True,
  97. },
  98. }, {
  99. 'url': 'http://abc.go.com/shows/the-catch/episode-guide/season-01/10-the-wedding',
  100. 'only_matching': True,
  101. }, {
  102. 'url': 'http://abc.go.com/shows/world-news-tonight/episode-guide/2017-02/17-021717-intense-stand-off-between-man-with-rifle-and-police-in-oakland',
  103. 'only_matching': True,
  104. }, {
  105. # brand 004
  106. 'url': 'http://disneynow.go.com/shows/big-hero-6-the-series/season-01/episode-10-mr-sparkles-loses-his-sparkle/vdka4637915',
  107. 'only_matching': True,
  108. }, {
  109. # brand 008
  110. 'url': 'http://disneynow.go.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
  111. 'only_matching': True,
  112. }, {
  113. 'url': 'https://disneynow.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
  114. 'only_matching': True,
  115. }]
  116. def _extract_videos(self, brand, video_id='-1', show_id='-1'):
  117. display_id = video_id if video_id != '-1' else show_id
  118. return self._download_json(
  119. 'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/%s/-1/%s/-1/-1.json' % (brand, show_id, video_id),
  120. display_id)['video']
  121. def _real_extract(self, url):
  122. mobj = re.match(self._VALID_URL, url)
  123. sub_domain = mobj.group('sub_domain') or mobj.group('sub_domain_2')
  124. video_id, display_id = mobj.group('id', 'display_id')
  125. site_info = self._SITE_INFO.get(sub_domain, {})
  126. brand = site_info.get('brand')
  127. if not video_id or not site_info:
  128. webpage = self._download_webpage(url, display_id or video_id)
  129. video_id = self._search_regex(
  130. (
  131. # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
  132. # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
  133. r'data-video-id=["\']*(VDKA\w+)',
  134. # https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet
  135. r'\b(?:video)?id["\']\s*:\s*["\'](VDKA\w+)'
  136. ), webpage, 'video id', default=video_id)
  137. if not site_info:
  138. brand = self._search_regex(
  139. (r'data-brand=\s*["\']\s*(\d+)',
  140. r'data-page-brand=\s*["\']\s*(\d+)'), webpage, 'brand',
  141. default='004')
  142. site_info = next(
  143. si for _, si in self._SITE_INFO.items()
  144. if si.get('brand') == brand)
  145. if not video_id:
  146. # show extraction works for Disney, DisneyJunior and DisneyXD
  147. # ABC and Freeform has different layout
  148. show_id = self._search_regex(r'data-show-id=["\']*(SH\d+)', webpage, 'show id')
  149. videos = self._extract_videos(brand, show_id=show_id)
  150. show_title = self._search_regex(r'data-show-title="([^"]+)"', webpage, 'show title', fatal=False)
  151. entries = []
  152. for video in videos:
  153. entries.append(self.url_result(
  154. video['url'], 'Go', video.get('id'), video.get('title')))
  155. entries.reverse()
  156. return self.playlist_result(entries, show_id, show_title)
  157. video_data = self._extract_videos(brand, video_id)[0]
  158. video_id = video_data['id']
  159. title = video_data['title']
  160. formats = []
  161. for asset in video_data.get('assets', {}).get('asset', []):
  162. asset_url = asset.get('value')
  163. if not asset_url:
  164. continue
  165. format_id = asset.get('format')
  166. ext = determine_ext(asset_url)
  167. if ext == 'm3u8':
  168. video_type = video_data.get('type')
  169. data = {
  170. 'video_id': video_data['id'],
  171. 'video_type': video_type,
  172. 'brand': brand,
  173. 'device': '001',
  174. }
  175. if video_data.get('accesslevel') == '1':
  176. requestor_id = site_info.get('requestor_id', 'DisneyChannels')
  177. resource = site_info.get('resource_id') or self._get_mvpd_resource(
  178. requestor_id, title, video_id, None)
  179. auth = self._extract_mvpd_auth(
  180. url, video_id, requestor_id, resource)
  181. data.update({
  182. 'token': auth,
  183. 'token_type': 'ap',
  184. 'adobe_requestor_id': requestor_id,
  185. })
  186. else:
  187. self._initialize_geo_bypass({'countries': ['US']})
  188. entitlement = self._download_json(
  189. 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
  190. video_id, data=urlencode_postdata(data))
  191. errors = entitlement.get('errors', {}).get('errors', [])
  192. if errors:
  193. for error in errors:
  194. if error.get('code') == 1002:
  195. self.raise_geo_restricted(
  196. error['message'], countries=['US'])
  197. error_message = ', '.join([error['message'] for error in errors])
  198. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
  199. asset_url += '?' + entitlement['uplynkData']['sessionKey']
  200. formats.extend(self._extract_m3u8_formats(
  201. asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
  202. else:
  203. f = {
  204. 'format_id': format_id,
  205. 'url': asset_url,
  206. 'ext': ext,
  207. }
  208. if re.search(r'(?:/mp4/source/|_source\.mp4)', asset_url):
  209. f.update({
  210. 'format_id': ('%s-' % format_id if format_id else '') + 'SOURCE',
  211. 'preference': 1,
  212. })
  213. else:
  214. mobj = re.search(r'/(\d+)x(\d+)/', asset_url)
  215. if mobj:
  216. height = int(mobj.group(2))
  217. f.update({
  218. 'format_id': ('%s-' % format_id if format_id else '') + '%dP' % height,
  219. 'width': int(mobj.group(1)),
  220. 'height': height,
  221. })
  222. formats.append(f)
  223. self._sort_formats(formats)
  224. subtitles = {}
  225. for cc in video_data.get('closedcaption', {}).get('src', []):
  226. cc_url = cc.get('value')
  227. if not cc_url:
  228. continue
  229. ext = determine_ext(cc_url)
  230. if ext == 'xml':
  231. ext = 'ttml'
  232. subtitles.setdefault(cc.get('lang'), []).append({
  233. 'url': cc_url,
  234. 'ext': ext,
  235. })
  236. thumbnails = []
  237. for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
  238. thumbnail_url = thumbnail.get('value')
  239. if not thumbnail_url:
  240. continue
  241. thumbnails.append({
  242. 'url': thumbnail_url,
  243. 'width': int_or_none(thumbnail.get('width')),
  244. 'height': int_or_none(thumbnail.get('height')),
  245. })
  246. return {
  247. 'id': video_id,
  248. 'title': title,
  249. 'description': video_data.get('longdescription') or video_data.get('description'),
  250. 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
  251. 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
  252. 'episode_number': int_or_none(video_data.get('episodenumber')),
  253. 'series': video_data.get('show', {}).get('title'),
  254. 'season_number': int_or_none(video_data.get('season', {}).get('num')),
  255. 'thumbnails': thumbnails,
  256. 'formats': formats,
  257. 'subtitles': subtitles,
  258. }