Merge remote-tracking branch 'jaimeMF/yt-playlists'
[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     }
29
30     def _real_extract(self, url):
31         mobj = re.match(self._VALID_URL, url)
32         video_id = mobj.group('id')
33         info_url = ('http://player-c.api.bambuser.com/getVideo.json?'
34             '&api_key=%s&vid=%s' % (self._API_KEY, video_id))
35         info_json = self._download_webpage(info_url, video_id)
36         info = json.loads(info_json)['result']
37
38         return {
39             'id': video_id,
40             'title': info['title'],
41             'url': info['url'],
42             'thumbnail': info.get('preview'),
43             'duration': int(info['length']),
44             'view_count': int(info['views_total']),
45             'uploader': info['username'],
46             'uploader_id': info['uid'],
47         }
48
49
50 class BambuserChannelIE(InfoExtractor):
51     IE_NAME = u'bambuser:channel'
52     _VALID_URL = r'http://bambuser.com/channel/(?P<user>.*?)(?:/|#|\?|$)'
53     # The maximum number we can get with each request
54     _STEP = 50
55
56     def _real_extract(self, url):
57         mobj = re.match(self._VALID_URL, url)
58         user = mobj.group('user')
59         urls = []
60         last_id = ''
61         for i in itertools.count(1):
62             req_url = ('http://bambuser.com/xhr-api/index.php?username={user}'
63                 '&sort=created&access_mode=0%2C1%2C2&limit={count}'
64                 '&method=broadcast&format=json&vid_older_than={last}'
65                 ).format(user=user, count=self._STEP, last=last_id)
66             req = compat_urllib_request.Request(req_url)
67             # Without setting this header, we wouldn't get any result
68             req.add_header('Referer', 'http://bambuser.com/channel/%s' % user)
69             info_json = self._download_webpage(req, user,
70                 u'Downloading page %d' % i)
71             results = json.loads(info_json)['result']
72             if len(results) == 0:
73                 break
74             last_id = results[-1]['vid']
75             urls.extend(self.url_result(v['page'], 'Bambuser') for v in results)
76
77         return {
78             '_type': 'playlist',
79             'title': user,
80             'entries': urls,
81         }