go.py 9.4 KB

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