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