[pandoratv] Improve extraction (Closes #7921)
[youtube-dl] / youtube_dl / extractor / pandoratv.py
1 # encoding: 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 )
15
16
17 class PandoraTVIE(InfoExtractor):
18     _VALID_URL = r'https?://(?:.+?\.)?channel\.pandora\.tv/channel/video\.ptv\?'
19     _TEST = {
20         'url': 'http://jp.channel.pandora.tv/channel/video.ptv?c1=&prgid=53294230&ch_userid=mikakim&ref=main&lot=cate_01_2',
21         'info_dict': {
22             'id': '53294230',
23             'ext': 'flv',
24             'title': '頭を撫でてくれる?',
25             'description': '頭を撫でてくれる?',
26             'thumbnail': 're:^https?://.*\.jpg$',
27             'duration': 39,
28             'upload_date': '20151218',
29             'uploader': 'カワイイ動物まとめ',
30             'uploader_id': 'mikakim',
31             'view_count': int,
32             'like_count': int,
33         }
34     }
35
36     def _real_extract(self, url):
37         qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
38         video_id = qs.get('prgid', [None])[0]
39         user_id = qs.get('ch_userid', [None])[0]
40         if any(not f for f in (video_id, user_id,)):
41             raise ExtractorError('Invalid URL', expected=True)
42
43         data = self._download_json(
44             'http://m.pandora.tv/?c=view&m=viewJsonApi&ch_userid=%s&prgid=%s'
45             % (user_id, video_id), video_id)
46
47         info = data['data']['rows']['vod_play_info']['result']
48
49         formats = []
50         for format_id, format_url in info.items():
51             if not format_url:
52                 continue
53             height = self._search_regex(
54                 r'^v(\d+)[Uu]rl$', format_id, 'height', default=None)
55             if not height:
56                 continue
57             formats.append({
58                 'format_id': '%sp' % height,
59                 'url': format_url,
60                 'height': int(height),
61             })
62         self._sort_formats(formats)
63
64         return {
65             'id': video_id,
66             'title': info['subject'],
67             'description': info.get('body'),
68             'thumbnail': info.get('thumbnail') or info.get('poster'),
69             'duration': float_or_none(info.get('runtime'), 1000) or parse_duration(info.get('time')),
70             'upload_date': info['fid'][:8] if isinstance(info.get('fid'), compat_str) else None,
71             'uploader': info.get('nickname'),
72             'uploader_id': info.get('upload_userid'),
73             'view_count': str_to_int(info.get('hit')),
74             'like_count': str_to_int(info.get('likecnt')),
75             'formats': formats,
76         }