patreon.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. clean_html,
  6. determine_ext,
  7. int_or_none,
  8. parse_iso8601,
  9. )
  10. class PatreonIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?patreon\.com/(?:creation\?hid=|posts/(?:[\w-]+-)?)(?P<id>\d+)'
  12. _TESTS = [{
  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. 'description': 'md5:713b08b772cd6271b9f3906683cfacdf',
  20. 'uploader': 'Cognitive Dissonance Podcast',
  21. 'thumbnail': 're:^https?://.*$',
  22. 'timestamp': 1406473987,
  23. 'upload_date': '20140727',
  24. },
  25. }, {
  26. 'url': 'http://www.patreon.com/creation?hid=754133',
  27. 'md5': '3eb09345bf44bf60451b8b0b81759d0a',
  28. 'info_dict': {
  29. 'id': '754133',
  30. 'ext': 'mp3',
  31. 'title': 'CD 167 Extra',
  32. 'uploader': 'Cognitive Dissonance Podcast',
  33. 'thumbnail': 're:^https?://.*$',
  34. },
  35. 'skip': 'Patron-only content',
  36. }, {
  37. 'url': 'https://www.patreon.com/creation?hid=1682498',
  38. 'info_dict': {
  39. 'id': 'SU4fj_aEMVw',
  40. 'ext': 'mp4',
  41. 'title': 'I\'m on Patreon!',
  42. 'uploader': 'TraciJHines',
  43. 'thumbnail': 're:^https?://.*$',
  44. 'upload_date': '20150211',
  45. 'description': 'md5:c5a706b1f687817a3de09db1eb93acd4',
  46. 'uploader_id': 'TraciJHines',
  47. },
  48. 'params': {
  49. 'noplaylist': True,
  50. 'skip_download': True,
  51. }
  52. }, {
  53. 'url': 'https://www.patreon.com/posts/episode-166-of-743933',
  54. 'only_matching': True,
  55. }, {
  56. 'url': 'https://www.patreon.com/posts/743933',
  57. 'only_matching': True,
  58. }]
  59. # Currently Patreon exposes download URL via hidden CSS, so login is not
  60. # needed. Keeping this commented for when this inevitably changes.
  61. '''
  62. def _login(self):
  63. username, password = self._get_login_info()
  64. if username is None:
  65. return
  66. login_form = {
  67. 'redirectUrl': 'http://www.patreon.com/',
  68. 'email': username,
  69. 'password': password,
  70. }
  71. request = sanitized_Request(
  72. 'https://www.patreon.com/processLogin',
  73. compat_urllib_parse_urlencode(login_form).encode('utf-8')
  74. )
  75. login_page = self._download_webpage(request, None, note='Logging in')
  76. if re.search(r'onLoginFailed', login_page):
  77. raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
  78. def _real_initialize(self):
  79. self._login()
  80. '''
  81. def _real_extract(self, url):
  82. video_id = self._match_id(url)
  83. post = self._download_json(
  84. 'https://www.patreon.com/api/posts/' + video_id, video_id)
  85. attributes = post['data']['attributes']
  86. title = attributes['title'].strip()
  87. image = attributes.get('image') or {}
  88. info = {
  89. 'id': video_id,
  90. 'title': title,
  91. 'description': clean_html(attributes.get('content')),
  92. 'thumbnail': image.get('large_url') or image.get('url'),
  93. 'timestamp': parse_iso8601(attributes.get('published_at')),
  94. 'like_count': int_or_none(attributes.get('like_count')),
  95. 'comment_count': int_or_none(attributes.get('comment_count')),
  96. }
  97. for i in post.get('included', []):
  98. i_type = i.get('type')
  99. if i_type == 'attachment':
  100. attachment_attributes = i.get('attributes') or {}
  101. attachment_url = attachment_attributes.get('url')
  102. if attachment_url:
  103. info.update({
  104. 'url': attachment_url,
  105. 'ext': determine_ext(attachment_attributes.get('name'), 'mp3'),
  106. })
  107. elif i_type == 'user':
  108. user_attributes = i.get('attributes')
  109. if user_attributes:
  110. info.update({
  111. 'uploader': user_attributes.get('full_name'),
  112. 'uploader_url': user_attributes.get('url'),
  113. })
  114. if not info.get('url'):
  115. info.update({
  116. '_type': 'url',
  117. 'url': attributes['embed']['url'],
  118. })
  119. return info