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.

117 lines
4.1 KiB

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