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.

125 lines
4.9 KiB

8 years ago
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. determine_ext,
  8. parse_age_limit,
  9. urlencode_postdata,
  10. ExtractorError,
  11. )
  12. class GoIE(InfoExtractor):
  13. _BRANDS = {
  14. 'abc': '001',
  15. 'freeform': '002',
  16. 'watchdisneychannel': '004',
  17. 'watchdisneyjunior': '008',
  18. 'watchdisneyxd': '009',
  19. }
  20. _VALID_URL = r'https?://(?:(?P<sub_domain>%s)\.)?go\.com/(?:[^/]+/)*(?:vdka(?P<id>\w+)|season-\d+/\d+-(?P<display_id>[^/?#]+))' % '|'.join(_BRANDS.keys())
  21. _TESTS = [{
  22. 'url': 'http://abc.go.com/shows/castle/video/most-recent/vdka0_g86w5onx',
  23. 'info_dict': {
  24. 'id': '0_g86w5onx',
  25. 'ext': 'mp4',
  26. 'title': 'Sneak Peek: Language Arts',
  27. 'description': 'md5:7dcdab3b2d17e5217c953256af964e9c',
  28. },
  29. 'params': {
  30. # m3u8 download
  31. 'skip_download': True,
  32. },
  33. }, {
  34. 'url': 'http://abc.go.com/shows/after-paradise/video/most-recent/vdka3335601',
  35. 'only_matching': True,
  36. }]
  37. def _real_extract(self, url):
  38. sub_domain, video_id, display_id = re.match(self._VALID_URL, url).groups()
  39. if not video_id:
  40. webpage = self._download_webpage(url, display_id)
  41. video_id = self._search_regex(
  42. # There may be inner quotes, e.g. data-video-id="'VDKA3609139'"
  43. # from http://freeform.go.com/shows/shadowhunters/episodes/season-2/1-this-guilty-blood
  44. r'data-video-id=["\']*VDKA(\w+)', webpage, 'video id')
  45. brand = self._BRANDS[sub_domain]
  46. video_data = self._download_json(
  47. 'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (brand, video_id),
  48. video_id)['video'][0]
  49. title = video_data['title']
  50. formats = []
  51. for asset in video_data.get('assets', {}).get('asset', []):
  52. asset_url = asset.get('value')
  53. if not asset_url:
  54. continue
  55. format_id = asset.get('format')
  56. ext = determine_ext(asset_url)
  57. if ext == 'm3u8':
  58. video_type = video_data.get('type')
  59. if video_type == 'lf':
  60. entitlement = self._download_json(
  61. 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
  62. video_id, data=urlencode_postdata({
  63. 'video_id': video_data['id'],
  64. 'video_type': video_type,
  65. 'brand': brand,
  66. 'device': '001',
  67. }))
  68. errors = entitlement.get('errors', {}).get('errors', [])
  69. if errors:
  70. error_message = ', '.join([error['message'] for error in errors])
  71. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
  72. asset_url += '?' + entitlement['uplynkData']['sessionKey']
  73. formats.extend(self._extract_m3u8_formats(
  74. asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
  75. else:
  76. formats.append({
  77. 'format_id': format_id,
  78. 'url': asset_url,
  79. 'ext': ext,
  80. })
  81. self._sort_formats(formats)
  82. subtitles = {}
  83. for cc in video_data.get('closedcaption', {}).get('src', []):
  84. cc_url = cc.get('value')
  85. if not cc_url:
  86. continue
  87. ext = determine_ext(cc_url)
  88. if ext == 'xml':
  89. ext = 'ttml'
  90. subtitles.setdefault(cc.get('lang'), []).append({
  91. 'url': cc_url,
  92. 'ext': ext,
  93. })
  94. thumbnails = []
  95. for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
  96. thumbnail_url = thumbnail.get('value')
  97. if not thumbnail_url:
  98. continue
  99. thumbnails.append({
  100. 'url': thumbnail_url,
  101. 'width': int_or_none(thumbnail.get('width')),
  102. 'height': int_or_none(thumbnail.get('height')),
  103. })
  104. return {
  105. 'id': video_id,
  106. 'title': title,
  107. 'description': video_data.get('longdescription') or video_data.get('description'),
  108. 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
  109. 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
  110. 'episode_number': int_or_none(video_data.get('episodenumber')),
  111. 'series': video_data.get('show', {}).get('title'),
  112. 'season_number': int_or_none(video_data.get('season', {}).get('num')),
  113. 'thumbnails': thumbnails,
  114. 'formats': formats,
  115. 'subtitles': subtitles,
  116. }