go.py 11 KB

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