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.

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