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