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