go.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. import re
  2. from .adobepass import AdobePassIE
  3. from ..utils import (
  4. ExtractorError,
  5. determine_ext,
  6. int_or_none,
  7. join_nonempty,
  8. parse_age_limit,
  9. remove_end,
  10. remove_start,
  11. traverse_obj,
  12. try_get,
  13. unified_timestamp,
  14. urlencode_postdata,
  15. )
  16. class GoIE(AdobePassIE):
  17. _SITE_INFO = {
  18. 'abc': {
  19. 'brand': '001',
  20. 'requestor_id': 'ABC',
  21. },
  22. 'freeform': {
  23. 'brand': '002',
  24. 'requestor_id': 'ABCFamily',
  25. },
  26. 'watchdisneychannel': {
  27. 'brand': '004',
  28. 'resource_id': 'Disney',
  29. },
  30. 'watchdisneyjunior': {
  31. 'brand': '008',
  32. 'resource_id': 'DisneyJunior',
  33. },
  34. 'watchdisneyxd': {
  35. 'brand': '009',
  36. 'resource_id': 'DisneyXD',
  37. },
  38. 'disneynow': {
  39. 'brand': '011',
  40. 'resource_id': 'Disney',
  41. },
  42. 'fxnow.fxnetworks': {
  43. 'brand': '025',
  44. 'requestor_id': 'dtci',
  45. },
  46. }
  47. _VALID_URL = r'''(?x)
  48. https?://
  49. (?P<sub_domain>
  50. (?:{}\.)?go|fxnow\.fxnetworks|
  51. (?:www\.)?(?:abc|freeform|disneynow)
  52. )\.com/
  53. (?:
  54. (?:[^/]+/)*(?P<id>[Vv][Dd][Kk][Aa]\w+)|
  55. (?:[^/]+/)*(?P<display_id>[^/?\#]+)
  56. )
  57. '''.format(r'\.|'.join(list(_SITE_INFO.keys())))
  58. _TESTS = [{
  59. 'url': 'http://abc.go.com/shows/designated-survivor/video/most-recent/VDKA3807643',
  60. 'info_dict': {
  61. 'id': 'VDKA3807643',
  62. 'ext': 'mp4',
  63. 'title': 'The Traitor in the White House',
  64. 'description': 'md5:05b009d2d145a1e85d25111bd37222e8',
  65. },
  66. 'params': {
  67. # m3u8 download
  68. 'skip_download': True,
  69. },
  70. 'skip': 'This content is no longer available.',
  71. }, {
  72. 'url': 'https://disneynow.com/shows/big-hero-6-the-series',
  73. 'info_dict': {
  74. 'title': 'Doraemon',
  75. 'id': 'SH55574025',
  76. },
  77. 'playlist_mincount': 51,
  78. }, {
  79. 'url': 'http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood',
  80. 'info_dict': {
  81. 'id': 'VDKA3609139',
  82. 'title': 'This Guilty Blood',
  83. 'description': 'md5:f18e79ad1c613798d95fdabfe96cd292',
  84. 'age_limit': 14,
  85. 'episode': 'Episode 1',
  86. 'upload_date': '20170102',
  87. 'season': 'Season 2',
  88. 'thumbnail': 'http://cdn1.edgedatg.com/aws/v2/abcf/Shadowhunters/video/201/ae5f75608d86bf88aa4f9f4aa76ab1b7/579x325-Q100_ae5f75608d86bf88aa4f9f4aa76ab1b7.jpg',
  89. 'duration': 2544,
  90. 'season_number': 2,
  91. 'series': 'Shadowhunters',
  92. 'episode_number': 1,
  93. 'timestamp': 1483387200,
  94. 'ext': 'mp4',
  95. },
  96. 'params': {
  97. 'geo_bypass_ip_block': '3.244.239.0/24',
  98. # m3u8 download
  99. 'skip_download': True,
  100. },
  101. }, {
  102. 'url': 'https://abc.com/shows/the-rookie/episode-guide/season-04/12-the-knock',
  103. 'info_dict': {
  104. 'id': 'VDKA26050359',
  105. 'title': 'The Knock',
  106. 'description': 'md5:0c2947e3ada4c31f28296db7db14aa64',
  107. 'age_limit': 14,
  108. 'ext': 'mp4',
  109. 'thumbnail': 'http://cdn1.edgedatg.com/aws/v2/abc/TheRookie/video/412/daf830d06e83b11eaf5c0a299d993ae3/1556x876-Q75_daf830d06e83b11eaf5c0a299d993ae3.jpg',
  110. 'episode': 'Episode 12',
  111. 'season_number': 4,
  112. 'season': 'Season 4',
  113. 'timestamp': 1642975200,
  114. 'episode_number': 12,
  115. 'upload_date': '20220123',
  116. 'series': 'The Rookie',
  117. 'duration': 2572,
  118. },
  119. 'params': {
  120. 'geo_bypass_ip_block': '3.244.239.0/24',
  121. # m3u8 download
  122. 'skip_download': True,
  123. },
  124. }, {
  125. 'url': 'https://fxnow.fxnetworks.com/shows/better-things/video/vdka12782841',
  126. 'info_dict': {
  127. 'id': 'VDKA12782841',
  128. 'title': 'First Look: Better Things - Season 2',
  129. 'description': 'md5:fa73584a95761c605d9d54904e35b407',
  130. 'ext': 'mp4',
  131. 'age_limit': 14,
  132. 'upload_date': '20170825',
  133. 'duration': 161,
  134. 'series': 'Better Things',
  135. 'thumbnail': 'http://cdn1.edgedatg.com/aws/v2/fx/BetterThings/video/12782841/b6b05e58264121cc2c98811318e6d507/1556x876-Q75_b6b05e58264121cc2c98811318e6d507.jpg',
  136. 'timestamp': 1503661074,
  137. },
  138. 'params': {
  139. 'geo_bypass_ip_block': '3.244.239.0/24',
  140. # m3u8 download
  141. 'skip_download': True,
  142. },
  143. }, {
  144. 'url': 'http://abc.go.com/shows/the-catch/episode-guide/season-01/10-the-wedding',
  145. 'only_matching': True,
  146. }, {
  147. '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',
  148. 'only_matching': True,
  149. }, {
  150. # brand 004
  151. 'url': 'http://disneynow.go.com/shows/big-hero-6-the-series/season-01/episode-10-mr-sparkles-loses-his-sparkle/vdka4637915',
  152. 'only_matching': True,
  153. }, {
  154. # brand 008
  155. 'url': 'http://disneynow.go.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
  156. 'only_matching': True,
  157. }, {
  158. 'url': 'https://disneynow.com/shows/minnies-bow-toons/video/happy-campers/vdka4872013',
  159. 'only_matching': True,
  160. }, {
  161. 'url': 'https://www.freeform.com/shows/cruel-summer/episode-guide/season-01/01-happy-birthday-jeanette-turner',
  162. 'only_matching': True,
  163. }]
  164. def _extract_videos(self, brand, video_id='-1', show_id='-1'):
  165. display_id = video_id if video_id != '-1' else show_id
  166. return self._download_json(
  167. f'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/{brand}/001/-1/{show_id}/-1/{video_id}/-1/-1.json',
  168. display_id)['video']
  169. def _real_extract(self, url):
  170. mobj = self._match_valid_url(url)
  171. sub_domain = remove_start(remove_end(mobj.group('sub_domain') or '', '.go'), 'www.')
  172. video_id, display_id = mobj.group('id', 'display_id')
  173. site_info = self._SITE_INFO.get(sub_domain, {})
  174. brand = site_info.get('brand')
  175. if not video_id or not site_info:
  176. webpage = self._download_webpage(url, display_id or video_id)
  177. data = self._parse_json(
  178. self._search_regex(
  179. r'["\']__abc_com__["\']\s*\]\s*=\s*({.+?})\s*;', webpage,
  180. 'data', default='{}'),
  181. display_id or video_id, fatal=False)
  182. # https://abc.com/shows/modern-family/episode-guide/season-01/101-pilot
  183. layout = try_get(data, lambda x: x['page']['content']['video']['layout'], dict)
  184. video_id = None
  185. if layout:
  186. video_id = try_get(
  187. layout,
  188. (lambda x: x['videoid'], lambda x: x['video']['id']),
  189. str)
  190. if not video_id:
  191. video_id = self._search_regex(
  192. (
  193. # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
  194. # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
  195. r'data-video-id=["\']*(VDKA\w+)',
  196. # page.analytics.videoIdCode
  197. r'\bvideoIdCode["\']\s*:\s*["\']((?:vdka|VDKA)\w+)',
  198. # https://abc.com/shows/the-rookie/episode-guide/season-02/03-the-bet
  199. r'\b(?:video)?id["\']\s*:\s*["\'](VDKA\w+)',
  200. ), webpage, 'video id', default=video_id)
  201. if not site_info:
  202. brand = self._search_regex(
  203. (r'data-brand=\s*["\']\s*(\d+)',
  204. r'data-page-brand=\s*["\']\s*(\d+)'), webpage, 'brand',
  205. default='004')
  206. site_info = next(
  207. si for _, si in self._SITE_INFO.items()
  208. if si.get('brand') == brand)
  209. if not video_id:
  210. # show extraction works for Disney, DisneyJunior and DisneyXD
  211. # ABC and Freeform has different layout
  212. show_id = self._search_regex(r'data-show-id=["\']*(SH\d+)', webpage, 'show id')
  213. videos = self._extract_videos(brand, show_id=show_id)
  214. show_title = self._search_regex(r'data-show-title="([^"]+)"', webpage, 'show title', fatal=False)
  215. entries = []
  216. for video in videos:
  217. entries.append(self.url_result(
  218. video['url'], 'Go', video.get('id'), video.get('title')))
  219. entries.reverse()
  220. return self.playlist_result(entries, show_id, show_title)
  221. video_data = self._extract_videos(brand, video_id)[0]
  222. video_id = video_data['id']
  223. title = video_data['title']
  224. formats = []
  225. subtitles = {}
  226. for asset in video_data.get('assets', {}).get('asset', []):
  227. asset_url = asset.get('value')
  228. if not asset_url:
  229. continue
  230. format_id = asset.get('format')
  231. ext = determine_ext(asset_url)
  232. if ext == 'm3u8':
  233. video_type = video_data.get('type')
  234. data = {
  235. 'video_id': video_data['id'],
  236. 'video_type': video_type,
  237. 'brand': brand,
  238. 'device': '001',
  239. }
  240. if video_data.get('accesslevel') == '1':
  241. requestor_id = site_info.get('requestor_id', 'DisneyChannels')
  242. resource = site_info.get('resource_id') or self._get_mvpd_resource(
  243. requestor_id, title, video_id, None)
  244. auth = self._extract_mvpd_auth(
  245. url, video_id, requestor_id, resource)
  246. data.update({
  247. 'token': auth,
  248. 'token_type': 'ap',
  249. 'adobe_requestor_id': requestor_id,
  250. })
  251. else:
  252. self._initialize_geo_bypass({'countries': ['US']})
  253. entitlement = self._download_json(
  254. 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
  255. video_id, data=urlencode_postdata(data))
  256. errors = entitlement.get('errors', {}).get('errors', [])
  257. if errors:
  258. for error in errors:
  259. if error.get('code') == 1002:
  260. self.raise_geo_restricted(
  261. error['message'], countries=['US'])
  262. error_message = ', '.join([error['message'] for error in errors])
  263. raise ExtractorError(f'{self.IE_NAME} said: {error_message}', expected=True)
  264. asset_url += '?' + entitlement['uplynkData']['sessionKey']
  265. fmts, subs = self._extract_m3u8_formats_and_subtitles(
  266. asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False)
  267. formats.extend(fmts)
  268. self._merge_subtitles(subs, target=subtitles)
  269. else:
  270. f = {
  271. 'format_id': format_id,
  272. 'url': asset_url,
  273. 'ext': ext,
  274. }
  275. if re.search(r'(?:/mp4/source/|_source\.mp4)', asset_url):
  276. f.update({
  277. 'format_id': (f'{format_id}-' if format_id else '') + 'SOURCE',
  278. 'quality': 1,
  279. })
  280. else:
  281. mobj = re.search(r'/(\d+)x(\d+)/', asset_url)
  282. if mobj:
  283. height = int(mobj.group(2))
  284. f.update({
  285. 'format_id': join_nonempty(format_id, f'{height}P'),
  286. 'width': int(mobj.group(1)),
  287. 'height': height,
  288. })
  289. formats.append(f)
  290. for cc in video_data.get('closedcaption', {}).get('src', []):
  291. cc_url = cc.get('value')
  292. if not cc_url:
  293. continue
  294. ext = determine_ext(cc_url)
  295. if ext == 'xml':
  296. ext = 'ttml'
  297. subtitles.setdefault(cc.get('lang'), []).append({
  298. 'url': cc_url,
  299. 'ext': ext,
  300. })
  301. thumbnails = []
  302. for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
  303. thumbnail_url = thumbnail.get('value')
  304. if not thumbnail_url:
  305. continue
  306. thumbnails.append({
  307. 'url': thumbnail_url,
  308. 'width': int_or_none(thumbnail.get('width')),
  309. 'height': int_or_none(thumbnail.get('height')),
  310. })
  311. return {
  312. 'id': video_id,
  313. 'title': title,
  314. 'description': video_data.get('longdescription') or video_data.get('description'),
  315. 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
  316. 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
  317. 'episode_number': int_or_none(video_data.get('episodenumber')),
  318. 'series': video_data.get('show', {}).get('title'),
  319. 'season_number': int_or_none(video_data.get('season', {}).get('num')),
  320. 'thumbnails': thumbnails,
  321. 'formats': formats,
  322. 'subtitles': subtitles,
  323. 'timestamp': unified_timestamp(traverse_obj(video_data, ('airdates', 'airdate', 0))),
  324. }