]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tver.py
[utils] Better traceback for `ExtractorError`
[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)/(?P<id>f?\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 _TOKEN = None
34 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
35
36 def _real_initialize(self):
37 self._TOKEN = self._download_json(
38 'https://tver.jp/api/access_token.php', None)['token']
39
40 def _real_extract(self, url):
41 path, video_id = self._match_valid_url(url).groups()
42 api_response = self._download_json(
43 'https://api.tver.jp/v4/' + path, video_id,
44 query={'token': self._TOKEN})
45 p_id = traverse_obj(api_response, ('main', 'publisher_id'))
46 if not p_id:
47 error_msg, expected = traverse_obj(api_response, ('episode', 0, 'textbar', 0, ('text', 'longer')), get_all=False), True
48 if not error_msg:
49 error_msg, expected = 'Failed to extract publisher ID', False
50 raise ExtractorError(error_msg, expected=expected)
51 service = remove_start(traverse_obj(api_response, ('main', 'service')), 'ts_')
52
53 r_id = traverse_obj(api_response, ('main', 'reference_id'))
54 if service not in ('tx', 'russia2018', 'sebare2018live', 'gorin'):
55 r_id = 'ref:' + r_id
56 bc_url = smuggle_url(
57 self.BRIGHTCOVE_URL_TEMPLATE % (p_id, r_id),
58 {'geo_countries': ['JP']})
59
60 return {
61 '_type': 'url_transparent',
62 'description': traverse_obj(api_response, ('main', 'note', 0, 'text'), expected_type=compat_str),
63 'episode_number': int_or_none(traverse_obj(api_response, ('main', 'ext', 'episode_number'), expected_type=compat_str)),
64 'url': bc_url,
65 'ie_key': 'BrightcoveNew',
66 }