[pandoratv] Fix extraction (closes #11023)
[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     compat_urllib_request,
9 )
10 from ..utils import (
11     ExtractorError,
12     float_or_none,
13     parse_duration,
14     str_to_int,
15     urlencode_postdata,
16 )
17
18
19 class PandoraTVIE(InfoExtractor):
20     IE_NAME = 'pandora.tv'
21     IE_DESC = '판도라TV'
22     _VALID_URL = r'https?://(?:.+?\.)?channel\.pandora\.tv/channel/video\.ptv\?'
23     _TEST = {
24         'url': 'http://jp.channel.pandora.tv/channel/video.ptv?c1=&prgid=53294230&ch_userid=mikakim&ref=main&lot=cate_01_2',
25         'info_dict': {
26             'id': '53294230',
27             'ext': 'flv',
28             'title': '頭を撫でてくれる?',
29             'description': '頭を撫でてくれる?',
30             'thumbnail': 're:^https?://.*\.jpg$',
31             'duration': 39,
32             'upload_date': '20151218',
33             'uploader': 'カワイイ動物まとめ',
34             'uploader_id': 'mikakim',
35             'view_count': int,
36             'like_count': int,
37         }
38     }
39
40     def _real_extract(self, url):
41         qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
42         video_id = qs.get('prgid', [None])[0]
43         user_id = qs.get('ch_userid', [None])[0]
44         if any(not f for f in (video_id, user_id,)):
45             raise ExtractorError('Invalid URL', expected=True)
46
47         data = self._download_json(
48             'http://m.pandora.tv/?c=view&m=viewJsonApi&ch_userid=%s&prgid=%s'
49             % (user_id, video_id), video_id)
50
51         info = data['data']['rows']['vod_play_info']['result']
52
53         formats = []
54         for format_id, format_url in info.items():
55             if not format_url:
56                 continue
57             height = self._search_regex(
58                 r'^v(\d+)[Uu]rl$', format_id, 'height', default=None)
59             if not height:
60                 continue
61
62             post_data = {'prgid': video_id, 'runtime': info.get('runtime'), 'vod_url': format_url}
63             play_url = self._download_json('http://m.pandora.tv/?c=api&m=play_url', video_id, 
64                 data=urlencode_postdata(post_data), 
65                 headers={
66                     'Origin': url,
67                     'Content-Type': 'application/x-www-form-urlencoded'
68             })
69             format_url = play_url.get('url')
70             if not format_url:
71                 continue
72
73             formats.append({
74                 'format_id': '%sp' % height,
75                 'url': format_url,
76                 'height': int(height),
77             })
78         self._sort_formats(formats)
79
80         return {
81             'id': video_id,
82             'title': info['subject'],
83             'description': info.get('body'),
84             'thumbnail': info.get('thumbnail') or info.get('poster'),
85             'duration': float_or_none(info.get('runtime'), 1000) or parse_duration(info.get('time')),
86             'upload_date': info['fid'][:8] if isinstance(info.get('fid'), compat_str) else None,
87             'uploader': info.get('nickname'),
88             'uploader_id': info.get('upload_userid'),
89             'view_count': str_to_int(info.get('hit')),
90             'like_count': str_to_int(info.get('likecnt')),
91             'formats': formats,
92         }