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.

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