Merge branch 'master' of github.com:rg3/youtube-dl
[youtube-dl] / youtube_dl / extractor / bambuser.py
1 import re
2 import json
3 import itertools
4
5 from .common import InfoExtractor
6 from ..utils import (
7     compat_urllib_request,
8 )
9
10
11 class BambuserIE(InfoExtractor):
12     IE_NAME = u'bambuser'
13     _VALID_URL = r'https?://bambuser\.com/v/(?P<id>\d+)'
14     _API_KEY = '005f64509e19a868399060af746a00aa'
15
16     _TEST = {
17         u'url': u'http://bambuser.com/v/4050584',
18         # MD5 seems to be flaky, see https://travis-ci.org/rg3/youtube-dl/jobs/14051016#L388
19         #u'md5': u'fba8f7693e48fd4e8641b3fd5539a641',
20         u'info_dict': {
21             u'id': u'4050584',
22             u'ext': u'flv',
23             u'title': u'Education engineering days - lightning talks',
24             u'duration': 3741,
25             u'uploader': u'pixelversity',
26             u'uploader_id': u'344706',
27         },
28         u'params': {
29             # It doesn't respect the 'Range' header, it would download the whole video
30             # caused the travis builds to fail: https://travis-ci.org/rg3/youtube-dl/jobs/14493845#L59
31             u'skip_download': True,
32         },
33     }
34
35     def _real_extract(self, url):
36         mobj = re.match(self._VALID_URL, url)
37         video_id = mobj.group('id')
38         info_url = ('http://player-c.api.bambuser.com/getVideo.json?'
39             '&api_key=%s&vid=%s' % (self._API_KEY, video_id))
40         info_json = self._download_webpage(info_url, video_id)
41         info = json.loads(info_json)['result']
42
43         return {
44             'id': video_id,
45             'title': info['title'],
46             'url': info['url'],
47             'thumbnail': info.get('preview'),
48             'duration': int(info['length']),
49             'view_count': int(info['views_total']),
50             'uploader': info['username'],
51             'uploader_id': info['uid'],
52         }
53
54
55 class BambuserChannelIE(InfoExtractor):
56     IE_NAME = u'bambuser:channel'
57     _VALID_URL = r'http://bambuser.com/channel/(?P<user>.*?)(?:/|#|\?|$)'
58     # The maximum number we can get with each request
59     _STEP = 50
60
61     def _real_extract(self, url):
62         mobj = re.match(self._VALID_URL, url)
63         user = mobj.group('user')
64         urls = []
65         last_id = ''
66         for i in itertools.count(1):
67             req_url = ('http://bambuser.com/xhr-api/index.php?username={user}'
68                 '&sort=created&access_mode=0%2C1%2C2&limit={count}'
69                 '&method=broadcast&format=json&vid_older_than={last}'
70                 ).format(user=user, count=self._STEP, last=last_id)
71             req = compat_urllib_request.Request(req_url)
72             # Without setting this header, we wouldn't get any result
73             req.add_header('Referer', 'http://bambuser.com/channel/%s' % user)
74             info_json = self._download_webpage(req, user,
75                 u'Downloading page %d' % i)
76             results = json.loads(info_json)['result']
77             if len(results) == 0:
78                 break
79             last_id = results[-1]['vid']
80             urls.extend(self.url_result(v['page'], 'Bambuser') for v in results)
81
82         return {
83             '_type': 'playlist',
84             'title': user,
85             'entries': urls,
86         }