[ie/dplay] Fix extractors (#10471)
[yt-dlp3.git] / yt_dlp / extractor / tver.py
blob8105db41cd707c40f35706c93c1802f16381914f
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,
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': 'テレビ朝日',
25 'add_ie': ['BrightcoveNew'],
26 }, {
27 'url': 'https://tver.jp/corner/f0103888',
28 'only_matching': True,
29 }, {
30 'url': 'https://tver.jp/lp/f0033031',
31 'only_matching': True,
33 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
34 _PLATFORM_UID = None
35 _PLATFORM_TOKEN = None
37 def _real_initialize(self):
38 create_response = self._download_json(
39 'https://platform-api.tver.jp/v2/api/platform_users/browser/create', None,
40 note='Creating session', data=b'device_type=pc', headers={
41 'Origin': 'https://s.tver.jp',
42 'Referer': 'https://s.tver.jp/',
43 'Content-Type': 'application/x-www-form-urlencoded',
45 self._PLATFORM_UID = traverse_obj(create_response, ('result', 'platform_uid'))
46 self._PLATFORM_TOKEN = traverse_obj(create_response, ('result', 'platform_token'))
48 def _real_extract(self, url):
49 video_id, video_type = self._match_valid_url(url).group('id', 'type')
50 if video_type not in {'series', 'episodes'}:
51 webpage = self._download_webpage(url, video_id, note='Resolving to new URL')
52 video_id = self._match_id(self._search_regex(
53 (r'canonical"\s*href="(https?://tver\.jp/[^"]+)"', r'&link=(https?://tver\.jp/[^?&]+)[?&]'),
54 webpage, 'url regex'))
56 episode_info = self._download_json(
57 f'https://platform-api.tver.jp/service/api/v1/callEpisode/{video_id}?require_data=mylist,later[epefy106ur],good[epefy106ur],resume[epefy106ur]',
58 video_id, fatal=False,
59 query={
60 'platform_uid': self._PLATFORM_UID,
61 'platform_token': self._PLATFORM_TOKEN,
62 }, headers={
63 'x-tver-platform-type': 'web',
65 episode_content = traverse_obj(
66 episode_info, ('result', 'episode', 'content')) or {}
68 video_info = self._download_json(
69 f'https://statics.tver.jp/content/episode/{video_id}.json', video_id,
70 query={
71 'v': str_or_none(episode_content.get('version')) or '5',
72 }, headers={
73 'Origin': 'https://tver.jp',
74 'Referer': 'https://tver.jp/',
76 p_id = video_info['video']['accountID']
77 r_id = traverse_obj(video_info, ('video', ('videoRefID', 'videoID')), get_all=False)
78 if not r_id:
79 raise ExtractorError('Failed to extract reference ID for Brightcove')
80 if not r_id.isdigit():
81 r_id = f'ref:{r_id}'
83 episode = strip_or_none(episode_content.get('title'))
84 series = str_or_none(episode_content.get('seriesTitle'))
85 title = (
86 join_nonempty(series, episode, delim=' ')
87 or str_or_none(video_info.get('title')))
88 provider = str_or_none(episode_content.get('productionProviderName'))
89 onair_label = str_or_none(episode_content.get('broadcastDateLabel'))
91 return {
92 '_type': 'url_transparent',
93 'title': title,
94 'series': series,
95 'episode': episode,
96 # an another title which is considered "full title" for some viewers
97 'alt_title': join_nonempty(title, provider, onair_label, delim=' '),
98 'channel': provider,
99 'description': str_or_none(video_info.get('description')),
100 'url': smuggle_url(
101 self.BRIGHTCOVE_URL_TEMPLATE % (p_id, r_id), {'geo_countries': ['JP']}),
102 'ie_key': 'BrightcoveNew',