go.py 9.2 KB

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