logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

stv.py (3447B)


  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_str,
  7. float_or_none,
  8. int_or_none,
  9. smuggle_url,
  10. str_or_none,
  11. try_get,
  12. )
  13. class STVPlayerIE(InfoExtractor):
  14. IE_NAME = 'stv:player'
  15. _VALID_URL = r'https?://player\.stv\.tv/(?P<type>episode|video)/(?P<id>[a-z0-9]{4})'
  16. _TESTS = [{
  17. # shortform
  18. 'url': 'https://player.stv.tv/video/4gwd/emmerdale/60-seconds-on-set-with-laura-norton/',
  19. 'md5': '5adf9439c31d554f8be0707c7abe7e0a',
  20. 'info_dict': {
  21. 'id': '5333973339001',
  22. 'ext': 'mp4',
  23. 'upload_date': '20170301',
  24. 'title': '60 seconds on set with Laura Norton',
  25. 'description': "How many questions can Laura - a.k.a Kerry Wyatt - answer in 60 seconds? Let\'s find out!",
  26. 'timestamp': 1488388054,
  27. 'uploader_id': '1486976045',
  28. },
  29. 'skip': 'this resource is unavailable outside of the UK',
  30. }, {
  31. # episodes
  32. 'url': 'https://player.stv.tv/episode/4125/jennifer-saunders-memory-lane',
  33. 'only_matching': True,
  34. }]
  35. BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/1486976045/default_default/index.html?videoId=%s'
  36. _PTYPE_MAP = {
  37. 'episode': 'episodes',
  38. 'video': 'shortform',
  39. }
  40. def _real_extract(self, url):
  41. ptype, video_id = re.match(self._VALID_URL, url).groups()
  42. webpage = self._download_webpage(url, video_id, fatal=False) or ''
  43. props = (self._parse_json(self._search_regex(
  44. r'<script[^>]+id="__NEXT_DATA__"[^>]*>({.+?})</script>',
  45. webpage, 'next data', default='{}'), video_id,
  46. fatal=False) or {}).get('props') or {}
  47. player_api_cache = try_get(
  48. props, lambda x: x['initialReduxState']['playerApiCache']) or {}
  49. api_path, resp = None, {}
  50. for k, v in player_api_cache.items():
  51. if k.startswith('/episodes/') or k.startswith('/shortform/'):
  52. api_path, resp = k, v
  53. break
  54. else:
  55. episode_id = str_or_none(try_get(
  56. props, lambda x: x['pageProps']['episodeId']))
  57. api_path = '/%s/%s' % (self._PTYPE_MAP[ptype], episode_id or video_id)
  58. result = resp.get('results')
  59. if not result:
  60. resp = self._download_json(
  61. 'https://player.api.stv.tv/v1' + api_path, video_id)
  62. result = resp['results']
  63. video = result['video']
  64. video_id = compat_str(video['id'])
  65. subtitles = {}
  66. _subtitles = result.get('_subtitles') or {}
  67. for ext, sub_url in _subtitles.items():
  68. subtitles.setdefault('en', []).append({
  69. 'ext': 'vtt' if ext == 'webvtt' else ext,
  70. 'url': sub_url,
  71. })
  72. programme = result.get('programme') or {}
  73. return {
  74. '_type': 'url_transparent',
  75. 'id': video_id,
  76. 'url': smuggle_url(self.BRIGHTCOVE_URL_TEMPLATE % video_id, {'geo_countries': ['GB']}),
  77. 'description': result.get('summary'),
  78. 'duration': float_or_none(video.get('length'), 1000),
  79. 'subtitles': subtitles,
  80. 'view_count': int_or_none(result.get('views')),
  81. 'series': programme.get('name') or programme.get('shortName'),
  82. 'ie_key': 'BrightcoveNew',
  83. }