]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/stv.py
Update upstream source from tag 'upstream/2019.09.28'
[youtubedl] / youtube_dl / extractor / stv.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8 compat_parse_qs,
9 compat_urllib_parse_urlparse
10 )
11 from ..utils import (
12 extract_attributes,
13 float_or_none,
14 int_or_none,
15 str_or_none,
16 )
17
18
19 class STVPlayerIE(InfoExtractor):
20 IE_NAME = 'stv:player'
21 _VALID_URL = r'https?://player\.stv\.tv/(?P<type>episode|video)/(?P<id>[a-z0-9]{4})'
22 _TEST = {
23 'url': 'https://player.stv.tv/video/7srz/victoria/interview-with-the-cast-ahead-of-new-victoria/',
24 'md5': '2ad867d4afd641fa14187596e0fbc91b',
25 'info_dict': {
26 'id': '6016487034001',
27 'ext': 'mp4',
28 'upload_date': '20190321',
29 'title': 'Interview with the cast ahead of new Victoria',
30 'description': 'Nell Hudson and Lily Travers tell us what to expect in the new season of Victoria.',
31 'timestamp': 1553179628,
32 'uploader_id': '1486976045',
33 },
34 'skip': 'this resource is unavailable outside of the UK',
35 }
36 _PUBLISHER_ID = '1486976045'
37 _PTYPE_MAP = {
38 'episode': 'episodes',
39 'video': 'shortform',
40 }
41
42 def _real_extract(self, url):
43 ptype, video_id = re.match(self._VALID_URL, url).groups()
44 webpage = self._download_webpage(url, video_id)
45
46 qs = compat_parse_qs(compat_urllib_parse_urlparse(self._search_regex(
47 r'itemprop="embedURL"[^>]+href="([^"]+)',
48 webpage, 'embed URL', default=None)).query)
49 publisher_id = qs.get('publisherID', [None])[0] or self._PUBLISHER_ID
50
51 player_attr = extract_attributes(self._search_regex(
52 r'(<[^>]+class="bcplayer"[^>]+>)', webpage, 'player', default=None)) or {}
53
54 info = {}
55 duration = ref_id = series = video_id = None
56 api_ref_id = player_attr.get('data-player-api-refid')
57 if api_ref_id:
58 resp = self._download_json(
59 'https://player.api.stv.tv/v1/%s/%s' % (self._PTYPE_MAP[ptype], api_ref_id),
60 api_ref_id, fatal=False)
61 if resp:
62 result = resp.get('results') or {}
63 video = result.get('video') or {}
64 video_id = str_or_none(video.get('id'))
65 ref_id = video.get('guid')
66 duration = video.get('length')
67 programme = result.get('programme') or {}
68 series = programme.get('name') or programme.get('shortName')
69 subtitles = {}
70 _subtitles = result.get('_subtitles') or {}
71 for ext, sub_url in _subtitles.items():
72 subtitles.setdefault('en', []).append({
73 'ext': 'vtt' if ext == 'webvtt' else ext,
74 'url': sub_url,
75 })
76 info.update({
77 'description': result.get('summary'),
78 'subtitles': subtitles,
79 'view_count': int_or_none(result.get('views')),
80 })
81 if not video_id:
82 video_id = qs.get('videoId', [None])[0] or self._search_regex(
83 r'<link\s+itemprop="url"\s+href="(\d+)"',
84 webpage, 'video id', default=None) or 'ref:' + (ref_id or player_attr['data-refid'])
85
86 info.update({
87 '_type': 'url_transparent',
88 'duration': float_or_none(duration or player_attr.get('data-duration'), 1000),
89 'id': video_id,
90 'ie_key': 'BrightcoveNew',
91 'series': series or player_attr.get('data-programme-name'),
92 'url': 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s' % (publisher_id, video_id),
93 })
94 return info