]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/tver.py
[spotify] Detect iframe embeds (#3430)
[yt-dlp.git] / yt_dlp / extractor / tver.py
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 smuggle_url,
5 str_or_none,
6 traverse_obj,
7 )
8
9
10 class TVerIE(InfoExtractor):
11 _VALID_URL = r'https?://(?:www\.)?tver\.jp/(?:(?P<type>lp|corner|series|episodes?|feature|tokyo2020/video)/)+(?P<id>[a-zA-Z0-9]+)'
12 _TESTS = [{
13 'skip': 'videos are only available for 7 days',
14 'url': 'https://tver.jp/episodes/ephss8yveb',
15 'info_dict': {
16 'title': '#44 料理と値段と店主にびっくり オモてなしすぎウマい店 2時間SP',
17 'description': 'md5:66985373a66fed8ad3cd595a3cfebb13',
18 },
19 'add_ie': ['BrightcoveNew'],
20 }, {
21 'skip': 'videos are only available for 7 days',
22 'url': 'https://tver.jp/lp/episodes/ep6f16g26p',
23 'info_dict': {
24 # sorry but this is "correct"
25 'title': '4月11日(月)23時06分 ~ 放送予定',
26 'description': 'md5:4029cc5f4b1e8090dfc5b7bd2bc5cd0b',
27 },
28 'add_ie': ['BrightcoveNew'],
29 }, {
30 'url': 'https://tver.jp/corner/f0103888',
31 'only_matching': True,
32 }, {
33 'url': 'https://tver.jp/lp/f0033031',
34 'only_matching': True,
35 }]
36 BRIGHTCOVE_URL_TEMPLATE = 'http://players.brightcove.net/%s/default_default/index.html?videoId=%s'
37 _PLATFORM_UID = None
38 _PLATFORM_TOKEN = None
39
40 def _real_initialize(self):
41 create_response = self._download_json(
42 'https://platform-api.tver.jp/v2/api/platform_users/browser/create', None,
43 note='Creating session', data=b'device_type=pc', headers={
44 'Origin': 'https://s.tver.jp',
45 'Referer': 'https://s.tver.jp/',
46 'Content-Type': 'application/x-www-form-urlencoded',
47 })
48 self._PLATFORM_UID = traverse_obj(create_response, ('result', 'platform_uid'))
49 self._PLATFORM_TOKEN = traverse_obj(create_response, ('result', 'platform_token'))
50
51 def _real_extract(self, url):
52 video_id, video_type = self._match_valid_url(url).group('id', 'type')
53 if video_type not in {'series', 'episodes'}:
54 webpage = self._download_webpage(url, video_id, note='Resolving to new URL')
55 video_id = self._match_id(self._search_regex(
56 (r'canonical"\s*href="(https?://tver\.jp/[^"]+)"', r'&link=(https?://tver\.jp/[^?&]+)[?&]'),
57 webpage, 'url regex'))
58 video_info = self._download_json(
59 f'https://statics.tver.jp/content/episode/{video_id}.json', video_id,
60 query={'v': '5'}, headers={
61 'Origin': 'https://tver.jp',
62 'Referer': 'https://tver.jp/',
63 })
64 p_id = video_info['video']['accountID']
65 r_id = traverse_obj(video_info, ('video', ('videoRefID', 'videoID')), get_all=False)
66 if not r_id:
67 raise ExtractorError('Failed to extract reference ID for Brightcove')
68 if not r_id.isdigit():
69 r_id = f'ref:{r_id}'
70
71 additional_info = self._download_json(
72 f'https://platform-api.tver.jp/service/api/v1/callEpisode/{video_id}?require_data=mylist,later[epefy106ur],good[epefy106ur],resume[epefy106ur]',
73 video_id, fatal=False,
74 query={
75 'platform_uid': self._PLATFORM_UID,
76 'platform_token': self._PLATFORM_TOKEN,
77 }, headers={
78 'x-tver-platform-type': 'web'
79 })
80
81 return {
82 '_type': 'url_transparent',
83 'title': str_or_none(video_info.get('title')),
84 'description': str_or_none(video_info.get('description')),
85 'url': smuggle_url(
86 self.BRIGHTCOVE_URL_TEMPLATE % (p_id, r_id), {'geo_countries': ['JP']}),
87 'series': traverse_obj(
88 additional_info, ('result', ('episode', 'series'), 'content', ('seriesTitle', 'title')),
89 get_all=False),
90 'ie_key': 'BrightcoveNew',
91 }