]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tver.py
[cleanup] Misc fixes
[yt-dlp.git] / yt_dlp / extractor / tver.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4
5 from .common import InfoExtractor
6 from ..compat import compat_str
7 from ..utils import (
8 ExtractorError,
9 int_or_none,
10 remove_start,
11 smuggle_url,
12 traverse_obj,
13 )
14
15
16 class TVerIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?tver\.jp/(?P<path>corner|episode|feature|lp|tokyo2020/video)/(?P<id>[fc]?\d+)'
18 # videos are only available for 7 days
19 _TESTS = [{
20 'url': 'https://tver.jp/corner/f0062178',
21 'only_matching': True,
22 }, {
23 'url': 'https://tver.jp/feature/f0062413',
24 'only_matching': True,
25 }, {
26 'url': 'https://tver.jp/episode/79622438',
27 'only_matching': True,
28 }, {
29 # subtitle = ' '
30 'url': 'https://tver.jp/corner/f0068870',
31 'only_matching': True,
32 }, {
33 'url': 'https://tver.jp/lp/f0009694',
34 'only_matching': True,
35 }, {
36 'url': 'https://tver.jp/lp/c0000239',
37 'only_matching': True,
38 }, {
39 'url': 'https://tver.jp/tokyo2020/video/6264525510001',
40 'only_matching': True,
41 }]
42 _TOKEN = None
43 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
44
45 def _real_initialize(self):
46 self._TOKEN = self._download_json(
47 'https://tver.jp/api/access_token.php', None)['token']
48
49 def _real_extract(self, url):
50 path, video_id = self._match_valid_url(url).groups()
51 if path == 'lp':
52 webpage = self._download_webpage(url, video_id)
53 redirect_path = self._search_regex(r'to_href="([^"]+)', webpage, 'redirect path')
54 path, video_id = self._match_valid_url(f'https://tver.jp{redirect_path}').groups()
55 api_response = self._download_json(f'https://api.tver.jp/v4/{path}/{video_id}', video_id, query={'token': self._TOKEN})
56 p_id = traverse_obj(api_response, ('main', 'publisher_id'))
57 if not p_id:
58 error_msg, expected = traverse_obj(api_response, ('episode', 0, 'textbar', 0, ('text', 'longer')), get_all=False), True
59 if not error_msg:
60 error_msg, expected = 'Failed to extract publisher ID', False
61 raise ExtractorError(error_msg, expected=expected)
62 service = remove_start(traverse_obj(api_response, ('main', 'service')), 'ts_')
63
64 r_id = traverse_obj(api_response, ('main', 'reference_id'))
65 if service not in ('tx', 'russia2018', 'sebare2018live', 'gorin'):
66 r_id = 'ref:' + r_id
67 bc_url = smuggle_url(
68 self.BRIGHTCOVE_URL_TEMPLATE % (p_id, r_id),
69 {'geo_countries': ['JP']})
70
71 return {
72 '_type': 'url_transparent',
73 'description': traverse_obj(api_response, ('main', 'note', 0, 'text'), expected_type=compat_str),
74 'episode_number': int_or_none(traverse_obj(api_response, ('main', 'ext', 'episode_number'), expected_type=compat_str)),
75 'url': bc_url,
76 'ie_key': 'BrightcoveNew',
77 }