patreon.py 4.0 KB

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