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.

153 lines
6.0 KiB

8 years ago
  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+)|season-\d+/\d+-(?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. def _real_extract(self, url):
  53. sub_domain, video_id, display_id = re.match(self._VALID_URL, url).groups()
  54. if not video_id:
  55. webpage = self._download_webpage(url, display_id)
  56. video_id = self._search_regex(
  57. # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
  58. # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
  59. r'data-video-id=["\']*VDKA(\w+)', webpage, 'video id')
  60. site_info = self._SITE_INFO[sub_domain]
  61. brand = site_info['brand']
  62. video_data = self._download_json(
  63. 'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (brand, video_id),
  64. video_id)['video'][0]
  65. title = video_data['title']
  66. formats = []
  67. for asset in video_data.get('assets', {}).get('asset', []):
  68. asset_url = asset.get('value')
  69. if not asset_url:
  70. continue
  71. format_id = asset.get('format')
  72. ext = determine_ext(asset_url)
  73. if ext == 'm3u8':
  74. video_type = video_data.get('type')
  75. if video_type == 'lf':
  76. data = {
  77. 'video_id': video_data['id'],
  78. 'video_type': video_type,
  79. 'brand': brand,
  80. 'device': '001',
  81. }
  82. if video_data.get('accesslevel') == '1':
  83. requestor_id = site_info['requestor_id']
  84. resource = self._get_mvpd_resource(
  85. requestor_id, title, video_id, None)
  86. auth = self._extract_mvpd_auth(
  87. url, video_id, requestor_id, resource)
  88. data.update({
  89. 'token': auth,
  90. 'token_type': 'ap',
  91. 'adobe_requestor_id': requestor_id,
  92. })
  93. entitlement = self._download_json(
  94. 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
  95. video_id, data=urlencode_postdata(data), headers=self.geo_verification_headers())
  96. errors = entitlement.get('errors', {}).get('errors', [])
  97. if errors:
  98. error_message = ', '.join([error['message'] for error in errors])
  99. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
  100. asset_url += '?' + entitlement['uplynkData']['sessionKey']
  101. formats.extend(self._extract_m3u8_formats(
  102. asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
  103. else:
  104. formats.append({
  105. 'format_id': format_id,
  106. 'url': asset_url,
  107. 'ext': ext,
  108. })
  109. self._sort_formats(formats)
  110. subtitles = {}
  111. for cc in video_data.get('closedcaption', {}).get('src', []):
  112. cc_url = cc.get('value')
  113. if not cc_url:
  114. continue
  115. ext = determine_ext(cc_url)
  116. if ext == 'xml':
  117. ext = 'ttml'
  118. subtitles.setdefault(cc.get('lang'), []).append({
  119. 'url': cc_url,
  120. 'ext': ext,
  121. })
  122. thumbnails = []
  123. for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
  124. thumbnail_url = thumbnail.get('value')
  125. if not thumbnail_url:
  126. continue
  127. thumbnails.append({
  128. 'url': thumbnail_url,
  129. 'width': int_or_none(thumbnail.get('width')),
  130. 'height': int_or_none(thumbnail.get('height')),
  131. })
  132. return {
  133. 'id': video_id,
  134. 'title': title,
  135. 'description': video_data.get('longdescription') or video_data.get('description'),
  136. 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
  137. 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
  138. 'episode_number': int_or_none(video_data.get('episodenumber')),
  139. 'series': video_data.get('show', {}).get('title'),
  140. 'season_number': int_or_none(video_data.get('season', {}).get('num')),
  141. 'thumbnails': thumbnails,
  142. 'formats': formats,
  143. 'subtitles': subtitles,
  144. }