]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/stv.py
[youtube:comments] Add more options for limiting number of comments extracted (#1626)
[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._parse_json(self._search_regex(
49 r'<script[^>]+id="__NEXT_DATA__"[^>]*>({.+?})</script>',
50 webpage, 'next data', default='{}'), video_id,
51 fatal=False) or {}).get('props') or {}
52 player_api_cache = try_get(
53 props, lambda x: x['initialReduxState']['playerApiCache']) or {}
54
55 api_path, resp = None, {}
56 for k, v in player_api_cache.items():
57 if k.startswith('/episodes/') or k.startswith('/shortform/'):
58 api_path, resp = k, v
59 break
60 else:
61 episode_id = str_or_none(try_get(
62 props, lambda x: x['pageProps']['episodeId']))
63 api_path = '/%s/%s' % (self._PTYPE_MAP[ptype], episode_id or video_id)
64
65 result = resp.get('results')
66 if not result:
67 resp = self._download_json(
68 'https://player.api.stv.tv/v1' + api_path, video_id)
69 result = resp['results']
70
71 video = result['video']
72 video_id = compat_str(video['id'])
73
74 subtitles = {}
75 _subtitles = result.get('_subtitles') or {}
76 for ext, sub_url in _subtitles.items():
77 subtitles.setdefault('en', []).append({
78 'ext': 'vtt' if ext == 'webvtt' else ext,
79 'url': sub_url,
80 })
81
82 programme = result.get('programme') or {}
83
84 return {
85 '_type': 'url_transparent',
86 'id': video_id,
87 'url': smuggle_url(self.BRIGHTCOVE_URL_TEMPLATE % video_id, {'geo_countries': ['GB']}),
88 'description': result.get('summary'),
89 'duration': float_or_none(video.get('length'), 1000),
90 'subtitles': subtitles,
91 'view_count': int_or_none(result.get('views')),
92 'series': programme.get('name') or programme.get('shortName'),
93 'ie_key': 'BrightcoveNew',
94 }