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.

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