go.py 11 KB

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