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