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.

122 lines
4.7 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(r'data-video-id=["\']VDKA(\w+)', webpage, 'video id')
  42. brand = self._BRANDS[sub_domain]
  43. video_data = self._download_json(
  44. 'http://api.contents.watchabc.go.com/vp2/ws/contents/3000/videos/%s/001/-1/-1/-1/%s/-1/-1.json' % (brand, video_id),
  45. video_id)['video'][0]
  46. title = video_data['title']
  47. formats = []
  48. for asset in video_data.get('assets', {}).get('asset', []):
  49. asset_url = asset.get('value')
  50. if not asset_url:
  51. continue
  52. format_id = asset.get('format')
  53. ext = determine_ext(asset_url)
  54. if ext == 'm3u8':
  55. video_type = video_data.get('type')
  56. if video_type == 'lf':
  57. entitlement = self._download_json(
  58. 'https://api.entitlement.watchabc.go.com/vp2/ws-secure/entitlement/2020/authorize.json',
  59. video_id, data=urlencode_postdata({
  60. 'video_id': video_data['id'],
  61. 'video_type': video_type,
  62. 'brand': brand,
  63. 'device': '001',
  64. }))
  65. errors = entitlement.get('errors', {}).get('errors', [])
  66. if errors:
  67. error_message = ', '.join([error['message'] for error in errors])
  68. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message), expected=True)
  69. asset_url += '?' + entitlement['uplynkData']['sessionKey']
  70. formats.extend(self._extract_m3u8_formats(
  71. asset_url, video_id, 'mp4', m3u8_id=format_id or 'hls', fatal=False))
  72. else:
  73. formats.append({
  74. 'format_id': format_id,
  75. 'url': asset_url,
  76. 'ext': ext,
  77. })
  78. self._sort_formats(formats)
  79. subtitles = {}
  80. for cc in video_data.get('closedcaption', {}).get('src', []):
  81. cc_url = cc.get('value')
  82. if not cc_url:
  83. continue
  84. ext = determine_ext(cc_url)
  85. if ext == 'xml':
  86. ext = 'ttml'
  87. subtitles.setdefault(cc.get('lang'), []).append({
  88. 'url': cc_url,
  89. 'ext': ext,
  90. })
  91. thumbnails = []
  92. for thumbnail in video_data.get('thumbnails', {}).get('thumbnail', []):
  93. thumbnail_url = thumbnail.get('value')
  94. if not thumbnail_url:
  95. continue
  96. thumbnails.append({
  97. 'url': thumbnail_url,
  98. 'width': int_or_none(thumbnail.get('width')),
  99. 'height': int_or_none(thumbnail.get('height')),
  100. })
  101. return {
  102. 'id': video_id,
  103. 'title': title,
  104. 'description': video_data.get('longdescription') or video_data.get('description'),
  105. 'duration': int_or_none(video_data.get('duration', {}).get('value'), 1000),
  106. 'age_limit': parse_age_limit(video_data.get('tvrating', {}).get('rating')),
  107. 'episode_number': int_or_none(video_data.get('episodenumber')),
  108. 'series': video_data.get('show', {}).get('title'),
  109. 'season_number': int_or_none(video_data.get('season', {}).get('num')),
  110. 'thumbnails': thumbnails,
  111. 'formats': formats,
  112. 'subtitles': subtitles,
  113. }