You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.7 KiB

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