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.

101 lines
3.3 KiB

  1. # encoding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. compat_urlparse,
  8. js_to_json,
  9. )
  10. class PatreonIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?patreon\.com/creation\?hid=(.+)'
  12. _TESTS = [
  13. {
  14. 'url': 'http://www.patreon.com/creation?hid=743933',
  15. 'md5': 'e25505eec1053a6e6813b8ed369875cc',
  16. 'info_dict': {
  17. 'id': '743933',
  18. 'ext': 'mp3',
  19. 'title': 'Episode 166: David Smalley of Dogma Debate',
  20. 'uploader': 'Cognitive Dissonance Podcast',
  21. 'thumbnail': 're:^https?://.*$',
  22. },
  23. },
  24. {
  25. 'url': 'http://www.patreon.com/creation?hid=754133',
  26. 'md5': '3eb09345bf44bf60451b8b0b81759d0a',
  27. 'info_dict': {
  28. 'id': '754133',
  29. 'ext': 'mp3',
  30. 'title': 'CD 167 Extra',
  31. 'uploader': 'Cognitive Dissonance Podcast',
  32. 'thumbnail': 're:^https?://.*$',
  33. },
  34. },
  35. ]
  36. # Currently Patreon exposes download URL via hidden CSS, so login is not
  37. # needed. Keeping this commented for when this inevitably changes.
  38. '''
  39. def _login(self):
  40. (username, password) = self._get_login_info()
  41. if username is None:
  42. return
  43. login_form = {
  44. 'redirectUrl': 'http://www.patreon.com/',
  45. 'email': username,
  46. 'password': password,
  47. }
  48. request = compat_urllib_request.Request(
  49. 'https://www.patreon.com/processLogin',
  50. compat_urllib_parse.urlencode(login_form).encode('utf-8')
  51. )
  52. login_page = self._download_webpage(request, None, note='Logging in as %s' % username)
  53. if re.search(r'onLoginFailed', login_page):
  54. raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
  55. def _real_initialize(self):
  56. self._login()
  57. '''
  58. def _real_extract(self, url):
  59. mobj = re.match(self._VALID_URL, url)
  60. video_id = mobj.group(1)
  61. webpage = self._download_webpage(url, video_id)
  62. title = self._og_search_title(webpage).strip()
  63. attach_fn = self._html_search_regex(
  64. r'<div class="attach"><a target="_blank" href="([^"]+)">',
  65. webpage, 'attachment URL', default=None)
  66. if attach_fn is not None:
  67. video_url = 'http://www.patreon.com' + attach_fn
  68. thumbnail = self._og_search_thumbnail(webpage)
  69. uploader = self._html_search_regex(
  70. r'<strong>(.*?)</strong> is creating', webpage, 'uploader')
  71. else:
  72. playlist_js = self._search_regex(
  73. r'(?s)new\s+jPlayerPlaylist\(\s*\{\s*[^}]*},\s*(\[.*?,?\s*\])',
  74. webpage, 'playlist JSON')
  75. playlist_json = js_to_json(playlist_js)
  76. playlist = json.loads(playlist_json)
  77. data = playlist[0]
  78. video_url = self._proto_relative_url(data['mp3'])
  79. thumbnail = self._proto_relative_url(data.get('cover'))
  80. uploader = data.get('artist')
  81. return {
  82. 'id': video_id,
  83. 'url': video_url,
  84. 'ext': 'mp3',
  85. 'title': title,
  86. 'uploader': uploader,
  87. 'thumbnail': thumbnail,
  88. }