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