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