You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
9.0 KiB

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