]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tver.py
[ThumbnailsConvertor] Allow conditional conversion
[yt-dlp.git] / yt_dlp / extractor / tver.py
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 join_nonempty,
5 smuggle_url,
6 str_or_none,
7 strip_or_none,
8 traverse_obj,
9 )
10
11
12 class TVerIE(InfoExtractor):
13 _VALID_URL = r'https?://(?:www\.)?tver\.jp/(?:(?P<type>lp|corner|series|episodes?|feature|tokyo2020/video)/)+(?P<id>[a-zA-Z0-9]+)'
14 _TESTS = [{
15 'skip': 'videos are only available for 7 days',
16 'url': 'https://tver.jp/episodes/ep83nf3w4p',
17 'info_dict': {
18 'title': '家事ヤロウ!!! 売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着!',
19 'description': 'md5:dc2c06b6acc23f1e7c730c513737719b',
20 'series': '家事ヤロウ!!!',
21 'episode': '売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着!',
22 'alt_title': '売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着!',
23 'channel': 'テレビ朝日',
24 'onair_label': '5月3日(火)放送分',
25 'ext_title': '家事ヤロウ!!! 売り場席巻のチーズSP&財前直見×森泉親子の脱東京暮らし密着! テレビ朝日 5月3日(火)放送分',
26 },
27 'add_ie': ['BrightcoveNew'],
28 }, {
29 'url': 'https://tver.jp/corner/f0103888',
30 'only_matching': True,
31 }, {
32 'url': 'https://tver.jp/lp/f0033031',
33 'only_matching': True,
34 }]
35 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
36 _PLATFORM_UID = None
37 _PLATFORM_TOKEN = None
38
39 def _real_initialize(self):
40 create_response = self._download_json(
41 'https://platform-api.tver.jp/v2/api/platform_users/browser/create', None,
42 note='Creating session', data=b'device_type=pc', headers={
43 'Origin': 'https://s.tver.jp',
44 'Referer': 'https://s.tver.jp/',
45 'Content-Type': 'application/x-www-form-urlencoded',
46 })
47 self._PLATFORM_UID = traverse_obj(create_response, ('result', 'platform_uid'))
48 self._PLATFORM_TOKEN = traverse_obj(create_response, ('result', 'platform_token'))
49
50 def _real_extract(self, url):
51 video_id, video_type = self._match_valid_url(url).group('id', 'type')
52 if video_type not in {'series', 'episodes'}:
53 webpage = self._download_webpage(url, video_id, note='Resolving to new URL')
54 video_id = self._match_id(self._search_regex(
55 (r'canonical"\s*href="(https?://tver\.jp/[^"]+)"', r'&link=(https?://tver\.jp/[^?&]+)[?&]'),
56 webpage, 'url regex'))
57 video_info = self._download_json(
58 f'https://statics.tver.jp/content/episode/{video_id}.json', video_id,
59 query={'v': '5'}, headers={
60 'Origin': 'https://tver.jp',
61 'Referer': 'https://tver.jp/',
62 })
63 p_id = video_info['video']['accountID']
64 r_id = traverse_obj(video_info, ('video', ('videoRefID', 'videoID')), get_all=False)
65 if not r_id:
66 raise ExtractorError('Failed to extract reference ID for Brightcove')
67 if not r_id.isdigit():
68 r_id = f'ref:{r_id}'
69
70 additional_info = self._download_json(
71 f'https://platform-api.tver.jp/service/api/v1/callEpisode/{video_id}?require_data=mylist,later[epefy106ur],good[epefy106ur],resume[epefy106ur]',
72 video_id, fatal=False,
73 query={
74 'platform_uid': self._PLATFORM_UID,
75 'platform_token': self._PLATFORM_TOKEN,
76 }, headers={
77 'x-tver-platform-type': 'web'
78 })
79
80 additional_content_info = traverse_obj(
81 additional_info, ('result', 'episode', 'content'), get_all=False) or {}
82 episode = strip_or_none(additional_content_info.get('title'))
83 series = str_or_none(additional_content_info.get('seriesTitle'))
84 title = (
85 join_nonempty(series, episode, delim=' ')
86 or str_or_none(video_info.get('title')))
87 provider = str_or_none(additional_content_info.get('productionProviderName'))
88 onair_label = str_or_none(additional_content_info.get('broadcastDateLabel'))
89
90 return {
91 '_type': 'url_transparent',
92 'title': title,
93 'series': series,
94 'episode': episode,
95 # an another title which is considered "full title" for some viewers
96 'alt_title': join_nonempty(title, provider, onair_label, delim=' '),
97 'channel': provider,
98 'description': str_or_none(video_info.get('description')),
99 'url': smuggle_url(
100 self.BRIGHTCOVE_URL_TEMPLATE % (p_id, r_id), {'geo_countries': ['JP']}),
101 'ie_key': 'BrightcoveNew',
102 }