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