stv.py 3.2 KB

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