]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/bambuser.py
[prosiebensat1] extract all formats
[yt-dlp.git] / youtube_dl / extractor / bambuser.py
CommitLineData
3798eadc
PH
1from __future__ import unicode_literals
2
72a5b4f7 3import re
165e3bb6 4import itertools
72a5b4f7
JMF
5
6from .common import InfoExtractor
6e6bc8da 7from ..compat import compat_str
edf42161
S
8from ..utils import (
9 ExtractorError,
edf42161 10 float_or_none,
6e6bc8da 11 int_or_none,
5c2266df 12 sanitized_Request,
6e6bc8da 13 urlencode_postdata,
edf42161 14)
72a5b4f7
JMF
15
16
17class BambuserIE(InfoExtractor):
3798eadc 18 IE_NAME = 'bambuser'
72a5b4f7
JMF
19 _VALID_URL = r'https?://bambuser\.com/v/(?P<id>\d+)'
20 _API_KEY = '005f64509e19a868399060af746a00aa'
006ce15a
S
21 _LOGIN_URL = 'https://bambuser.com/user'
22 _NETRC_MACHINE = 'bambuser'
72a5b4f7
JMF
23
24 _TEST = {
3798eadc 25 'url': 'http://bambuser.com/v/4050584',
ce152341 26 # MD5 seems to be flaky, see https://travis-ci.org/rg3/youtube-dl/jobs/14051016#L388
dcddc10a 27 # 'md5': 'fba8f7693e48fd4e8641b3fd5539a641',
3798eadc
PH
28 'info_dict': {
29 'id': '4050584',
30 'ext': 'flv',
31 'title': 'Education engineering days - lightning talks',
32 'duration': 3741,
33 'uploader': 'pixelversity',
34 'uploader_id': '344706',
edf42161
S
35 'timestamp': 1382976692,
36 'upload_date': '20131028',
37 'view_count': int,
72a5b4f7 38 },
3798eadc 39 'params': {
1a62c18f
JMF
40 # It doesn't respect the 'Range' header, it would download the whole video
41 # caused the travis builds to fail: https://travis-ci.org/rg3/youtube-dl/jobs/14493845#L59
3798eadc 42 'skip_download': True,
1a62c18f 43 },
72a5b4f7
JMF
44 }
45
006ce15a
S
46 def _login(self):
47 (username, password) = self._get_login_info()
48 if username is None:
49 return
50
51 login_form = {
52 'form_id': 'user_login',
53 'op': 'Log in',
54 'name': username,
55 'pass': password,
56 }
57
5c2266df 58 request = sanitized_Request(
6e6bc8da 59 self._LOGIN_URL, urlencode_postdata(login_form))
006ce15a
S
60 request.add_header('Referer', self._LOGIN_URL)
61 response = self._download_webpage(
62 request, None, 'Logging in as %s' % username)
63
64 login_error = self._html_search_regex(
65 r'(?s)<div class="messages error">(.+?)</div>',
66 response, 'login error', default=None)
67 if login_error:
68 raise ExtractorError(
69 'Unable to login: %s' % login_error, expected=True)
70
71 def _real_initialize(self):
72 self._login()
73
72a5b4f7 74 def _real_extract(self, url):
bda44f31
S
75 video_id = self._match_id(url)
76
77 info = self._download_json(
78 'http://player-c.api.bambuser.com/getVideo.json?api_key=%s&vid=%s'
ae895340
S
79 % (self._API_KEY, video_id), video_id)
80
81 error = info.get('error')
82 if error:
83 raise ExtractorError(
84 '%s returned error: %s' % (self.IE_NAME, error), expected=True)
85
86 result = info['result']
72a5b4f7
JMF
87
88 return {
89 'id': video_id,
ae895340
S
90 'title': result['title'],
91 'url': result['url'],
92 'thumbnail': result.get('preview'),
edf42161
S
93 'duration': int_or_none(result.get('length')),
94 'uploader': result.get('username'),
95 'uploader_id': compat_str(result.get('owner', {}).get('uid')),
96 'timestamp': int_or_none(result.get('created')),
97 'fps': float_or_none(result.get('framerate')),
98 'view_count': int_or_none(result.get('views_total')),
99 'comment_count': int_or_none(result.get('comment_count')),
72a5b4f7
JMF
100 }
101
165e3bb6
JMF
102
103class BambuserChannelIE(InfoExtractor):
3798eadc 104 IE_NAME = 'bambuser:channel'
c0ade33e 105 _VALID_URL = r'https?://bambuser\.com/channel/(?P<user>.*?)(?:/|#|\?|$)'
165e3bb6
JMF
106 # The maximum number we can get with each request
107 _STEP = 50
22a6f150
PH
108 _TEST = {
109 'url': 'http://bambuser.com/channel/pixelversity',
110 'info_dict': {
111 'title': 'pixelversity',
112 },
113 'playlist_mincount': 60,
114 }
165e3bb6
JMF
115
116 def _real_extract(self, url):
117 mobj = re.match(self._VALID_URL, url)
118 user = mobj.group('user')
119 urls = []
120 last_id = ''
121 for i in itertools.count(1):
b74e86f4
PH
122 req_url = (
123 'http://bambuser.com/xhr-api/index.php?username={user}'
124 '&sort=created&access_mode=0%2C1%2C2&limit={count}'
125 '&method=broadcast&format=json&vid_older_than={last}'
126 ).format(user=user, count=self._STEP, last=last_id)
5c2266df 127 req = sanitized_Request(req_url)
165e3bb6
JMF
128 # Without setting this header, we wouldn't get any result
129 req.add_header('Referer', 'http://bambuser.com/channel/%s' % user)
22a6f150
PH
130 data = self._download_json(
131 req, user, 'Downloading page %d' % i)
132 results = data['result']
133 if not results:
165e3bb6
JMF
134 break
135 last_id = results[-1]['vid']
136 urls.extend(self.url_result(v['page'], 'Bambuser') for v in results)
137
138 return {
139 '_type': 'playlist',
140 'title': user,
141 'entries': urls,
142 }