cbb1968d37032e0adf64d9612470e10ac8823acd
[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     _TEST = {
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': '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
39     def _real_extract(self, url):
40         qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
41         video_id = qs.get('prgid', [None])[0]
42         user_id = qs.get('ch_userid', [None])[0]
43         if any(not f for f in (video_id, user_id,)):
44             raise ExtractorError('Invalid URL', expected=True)
45
46         data = self._download_json(
47             'http://m.pandora.tv/?c=view&m=viewJsonApi&ch_userid=%s&prgid=%s'
48             % (user_id, video_id), video_id)
49
50         info = data['data']['rows']['vod_play_info']['result']
51
52         formats = []
53         for format_id, format_url in info.items():
54             if not format_url:
55                 continue
56             height = self._search_regex(
57                 r'^v(\d+)[Uu]rl$', format_id, 'height', default=None)
58             if not height:
59                 continue
60
61             play_url = self._download_json(
62                 'http://m.pandora.tv/?c=api&m=play_url', video_id,
63                 data=urlencode_postdata({
64                     'prgid': video_id,
65                     'runtime': info.get('runtime'),
66                     'vod_url': format_url,
67                 }),
68                 headers={
69                     'Origin': url,
70                     'Content-Type': 'application/x-www-form-urlencoded',
71                 })
72             format_url = play_url.get('url')
73             if not format_url:
74                 continue
75
76             formats.append({
77                 'format_id': '%sp' % height,
78                 'url': format_url,
79                 'height': int(height),
80             })
81         self._sort_formats(formats)
82
83         return {
84             'id': video_id,
85             'title': info['subject'],
86             'description': info.get('body'),
87             'thumbnail': info.get('thumbnail') or info.get('poster'),
88             'duration': float_or_none(info.get('runtime'), 1000) or parse_duration(info.get('time')),
89             'upload_date': info['fid'][:8] if isinstance(info.get('fid'), compat_str) else None,
90             'uploader': info.get('nickname'),
91             'uploader_id': info.get('upload_userid'),
92             'view_count': str_to_int(info.get('hit')),
93             'like_count': str_to_int(info.get('likecnt')),
94             'formats': formats,
95         }