[pandoratv] Add new extractor (closes #6884)
[youtube-dl] / youtube_dl / extractor / pandoratv.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 from ..compat import (
7     compat_urlparse,
8 )
9 from ..utils import (
10     ExtractorError,
11 )
12
13
14 class PandoraTVIE(InfoExtractor):
15     _VALID_URL = r'http://(?:.+?\.)?channel.pandora.tv/channel/video.ptv\?'
16     _TESTS = [{
17         'url': 'http://jp.channel.pandora.tv/channel/video.ptv?c1=&prgid=53294230&ch_userid=mikakim&ref=main&lot=cate_01_2',
18         'info_dict': {
19             'description': '\u982d\u3092\u64ab\u3067\u3066\u304f\u308c\u308b\uff1f',
20             'ext': 'mp4',
21             'id': '53294230',
22             'title': '\u982d\u3092\u64ab\u3067\u3066\u304f\u308c\u308b\uff1f',
23             'upload_date': '20151218',
24         }
25     }]
26
27
28     def _real_extract(self, url):
29         qs = compat_urlparse.parse_qs(compat_urlparse.urlparse(url).query)
30         video_id = qs.get('prgid', [None])[0]
31         user_id = qs.get('ch_userid', [None])[0]
32         if any(not f for f in (video_id, user_id,)):
33             raise ExtractorError('Invalid URL', expected=True)
34
35         data_url ='http://m.pandora.tv/?c=view&m=viewJsonApi&ch_userid={userid}&prgid={prgid}'.format(userid=user_id,prgid=video_id)
36         data = self._download_json(data_url, video_id)
37         info = data['data']['rows']['vod_play_info']['result']
38
39         formats = []
40         for format_id in sorted([k for k in info if k.startswith('v') and k.endswith('Url') and info[k]]):
41             formats.append({
42                 'format_id': format_id,
43                 'url': info[format_id],
44                 'ext': 'mp4',
45                 'height': int(format_id[1:-3]),
46             })
47
48         return {
49             'description': info['body'],
50             'thumbnail': info['thumbnail'],
51             'formats': formats,
52             'id': video_id,
53             'title': info['subject'],
54             'upload_date': info['fid'][:8],
55             'view_count': info['hit'],
56         }