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