]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/pandoratv.py
[ie/box] Fix formats extraction (#8649)
[yt-dlp.git] / yt_dlp / extractor / pandoratv.py
1 from .common import InfoExtractor
2 from ..compat import (
3 compat_str,
4 )
5 from ..utils import (
6 ExtractorError,
7 float_or_none,
8 parse_duration,
9 parse_qs,
10 str_to_int,
11 urlencode_postdata,
12 )
13
14
15 class PandoraTVIE(InfoExtractor):
16 IE_NAME = 'pandora.tv'
17 IE_DESC = '판도라TV'
18 _VALID_URL = r'''(?x)
19 https?://
20 (?:
21 (?:www\.)?pandora\.tv/view/(?P<user_id>[^/]+)/(?P<id>\d+)| # new format
22 (?:.+?\.)?channel\.pandora\.tv/channel/video\.ptv\?| # old format
23 m\.pandora\.tv/?\? # mobile
24 )
25 '''
26 _TESTS = [{
27 'url': 'http://jp.channel.pandora.tv/channel/video.ptv?c1=&prgid=53294230&ch_userid=mikakim&ref=main&lot=cate_01_2',
28 'info_dict': {
29 'id': '53294230',
30 'ext': 'flv',
31 'title': '頭を撫でてくれる?',
32 'description': '頭を撫でてくれる?',
33 'thumbnail': r're:^https?://.*\.jpg$',
34 'duration': 39,
35 'upload_date': '20151218',
36 'uploader': 'カワイイ動物まとめ',
37 'uploader_id': 'mikakim',
38 'view_count': int,
39 'like_count': int,
40 }
41 }, {
42 'url': 'http://channel.pandora.tv/channel/video.ptv?ch_userid=gogoucc&prgid=54721744',
43 'info_dict': {
44 'id': '54721744',
45 'ext': 'flv',
46 'title': '[HD] JAPAN COUNTDOWN 170423',
47 'description': '[HD] JAPAN COUNTDOWN 170423',
48 'thumbnail': r're:^https?://.*\.jpg$',
49 'duration': 1704.9,
50 'upload_date': '20170423',
51 'uploader': 'GOGO_UCC',
52 'uploader_id': 'gogoucc',
53 'view_count': int,
54 'like_count': int,
55 },
56 'params': {
57 # Test metadata only
58 'skip_download': True,
59 },
60 }, {
61 'url': 'http://www.pandora.tv/view/mikakim/53294230#36797454_new',
62 'only_matching': True,
63 }, {
64 'url': 'http://m.pandora.tv/?c=view&ch_userid=mikakim&prgid=54600346',
65 'only_matching': True,
66 }]
67
68 def _real_extract(self, url):
69 mobj = self._match_valid_url(url)
70 user_id = mobj.group('user_id')
71 video_id = mobj.group('id')
72
73 if not user_id or not video_id:
74 qs = parse_qs(url)
75 video_id = qs.get('prgid', [None])[0]
76 user_id = qs.get('ch_userid', [None])[0]
77 if any(not f for f in (video_id, user_id,)):
78 raise ExtractorError('Invalid URL', expected=True)
79
80 data = self._download_json(
81 'http://m.pandora.tv/?c=view&m=viewJsonApi&ch_userid=%s&prgid=%s'
82 % (user_id, video_id), video_id)
83
84 info = data['data']['rows']['vod_play_info']['result']
85
86 formats = []
87 for format_id, format_url in info.items():
88 if not format_url:
89 continue
90 height = self._search_regex(
91 r'^v(\d+)[Uu]rl$', format_id, 'height', default=None)
92 if not height:
93 continue
94
95 play_url = self._download_json(
96 'http://m.pandora.tv/?c=api&m=play_url', video_id,
97 data=urlencode_postdata({
98 'prgid': video_id,
99 'runtime': info.get('runtime'),
100 'vod_url': format_url,
101 }),
102 headers={
103 'Origin': url,
104 'Content-Type': 'application/x-www-form-urlencoded',
105 })
106 format_url = play_url.get('url')
107 if not format_url:
108 continue
109
110 formats.append({
111 'format_id': '%sp' % height,
112 'url': format_url,
113 'height': int(height),
114 })
115
116 return {
117 'id': video_id,
118 'title': info['subject'],
119 'description': info.get('body'),
120 'thumbnail': info.get('thumbnail') or info.get('poster'),
121 'duration': float_or_none(info.get('runtime'), 1000) or parse_duration(info.get('time')),
122 'upload_date': info['fid'].split('/')[-1][:8] if isinstance(info.get('fid'), compat_str) else None,
123 'uploader': info.get('nickname'),
124 'uploader_id': info.get('upload_userid'),
125 'view_count': str_to_int(info.get('hit')),
126 'like_count': str_to_int(info.get('likecnt')),
127 'formats': formats,
128 }