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.

156 lines
5.5 KiB

Switch codebase to use sanitized_Request instead of compat_urllib_request.Request [downloader/dash] Use sanitized_Request [downloader/http] Use sanitized_Request [atresplayer] Use sanitized_Request [bambuser] Use sanitized_Request [bliptv] Use sanitized_Request [brightcove] Use sanitized_Request [cbs] Use sanitized_Request [ceskatelevize] Use sanitized_Request [collegerama] Use sanitized_Request [extractor/common] Use sanitized_Request [crunchyroll] Use sanitized_Request [dailymotion] Use sanitized_Request [dcn] Use sanitized_Request [dramafever] Use sanitized_Request [dumpert] Use sanitized_Request [eitb] Use sanitized_Request [escapist] Use sanitized_Request [everyonesmixtape] Use sanitized_Request [extremetube] Use sanitized_Request [facebook] Use sanitized_Request [fc2] Use sanitized_Request [flickr] Use sanitized_Request [4tube] Use sanitized_Request [gdcvault] Use sanitized_Request [extractor/generic] Use sanitized_Request [hearthisat] Use sanitized_Request [hotnewhiphop] Use sanitized_Request [hypem] Use sanitized_Request [iprima] Use sanitized_Request [ivi] Use sanitized_Request [keezmovies] Use sanitized_Request [letv] Use sanitized_Request [lynda] Use sanitized_Request [metacafe] Use sanitized_Request [minhateca] Use sanitized_Request [miomio] Use sanitized_Request [meovideo] Use sanitized_Request [mofosex] Use sanitized_Request [moniker] Use sanitized_Request [mooshare] Use sanitized_Request [movieclips] Use sanitized_Request [mtv] Use sanitized_Request [myvideo] Use sanitized_Request [neteasemusic] Use sanitized_Request [nfb] Use sanitized_Request [niconico] Use sanitized_Request [noco] Use sanitized_Request [nosvideo] Use sanitized_Request [novamov] Use sanitized_Request [nowness] Use sanitized_Request [nuvid] Use sanitized_Request [played] Use sanitized_Request [pluralsight] Use sanitized_Request [pornhub] Use sanitized_Request [pornotube] Use sanitized_Request [primesharetv] Use sanitized_Request [promptfile] Use sanitized_Request [qqmusic] Use sanitized_Request [rtve] Use sanitized_Request [safari] Use sanitized_Request [sandia] Use sanitized_Request [shared] Use sanitized_Request [sharesix] Use sanitized_Request [sina] Use sanitized_Request [smotri] Use sanitized_Request [sohu] Use sanitized_Request [spankwire] Use sanitized_Request [sportdeutschland] Use sanitized_Request [streamcloud] Use sanitized_Request [streamcz] Use sanitized_Request [tapely] Use sanitized_Request [tube8] Use sanitized_Request [tubitv] Use sanitized_Request [twitch] Use sanitized_Request [twitter] Use sanitized_Request [udemy] Use sanitized_Request [vbox7] Use sanitized_Request [veoh] Use sanitized_Request [vessel] Use sanitized_Request [vevo] Use sanitized_Request [viddler] Use sanitized_Request [videomega] Use sanitized_Request [viewvster] Use sanitized_Request [viki] Use sanitized_Request [vk] Use sanitized_Request [vodlocker] Use sanitized_Request [voicerepublic] Use sanitized_Request [wistia] Use sanitized_Request [xfileshare] Use sanitized_Request [xtube] Use sanitized_Request [xvideos] Use sanitized_Request [yandexmusic] Use sanitized_Request [youku] Use sanitized_Request [youporn] Use sanitized_Request [youtube] Use sanitized_Request [patreon] Use sanitized_Request [extractor/common] Remove unused import [nfb] PEP 8
9 years ago
  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. KNOWN_EXTENSIONS,
  9. mimetype2ext,
  10. parse_iso8601,
  11. str_or_none,
  12. try_get,
  13. )
  14. class PatreonIE(InfoExtractor):
  15. _VALID_URL = r'https?://(?:www\.)?patreon\.com/(?:creation\?hid=|posts/(?:[\w-]+-)?)(?P<id>\d+)'
  16. _TESTS = [{
  17. 'url': 'http://www.patreon.com/creation?hid=743933',
  18. 'md5': 'e25505eec1053a6e6813b8ed369875cc',
  19. 'info_dict': {
  20. 'id': '743933',
  21. 'ext': 'mp3',
  22. 'title': 'Episode 166: David Smalley of Dogma Debate',
  23. 'description': 'md5:713b08b772cd6271b9f3906683cfacdf',
  24. 'uploader': 'Cognitive Dissonance Podcast',
  25. 'thumbnail': 're:^https?://.*$',
  26. 'timestamp': 1406473987,
  27. 'upload_date': '20140727',
  28. 'uploader_id': '87145',
  29. },
  30. }, {
  31. 'url': 'http://www.patreon.com/creation?hid=754133',
  32. 'md5': '3eb09345bf44bf60451b8b0b81759d0a',
  33. 'info_dict': {
  34. 'id': '754133',
  35. 'ext': 'mp3',
  36. 'title': 'CD 167 Extra',
  37. 'uploader': 'Cognitive Dissonance Podcast',
  38. 'thumbnail': 're:^https?://.*$',
  39. },
  40. 'skip': 'Patron-only content',
  41. }, {
  42. 'url': 'https://www.patreon.com/creation?hid=1682498',
  43. 'info_dict': {
  44. 'id': 'SU4fj_aEMVw',
  45. 'ext': 'mp4',
  46. 'title': 'I\'m on Patreon!',
  47. 'uploader': 'TraciJHines',
  48. 'thumbnail': 're:^https?://.*$',
  49. 'upload_date': '20150211',
  50. 'description': 'md5:c5a706b1f687817a3de09db1eb93acd4',
  51. 'uploader_id': 'TraciJHines',
  52. },
  53. 'params': {
  54. 'noplaylist': True,
  55. 'skip_download': True,
  56. }
  57. }, {
  58. 'url': 'https://www.patreon.com/posts/episode-166-of-743933',
  59. 'only_matching': True,
  60. }, {
  61. 'url': 'https://www.patreon.com/posts/743933',
  62. 'only_matching': True,
  63. }]
  64. # Currently Patreon exposes download URL via hidden CSS, so login is not
  65. # needed. Keeping this commented for when this inevitably changes.
  66. '''
  67. def _login(self):
  68. username, password = self._get_login_info()
  69. if username is None:
  70. return
  71. login_form = {
  72. 'redirectUrl': 'http://www.patreon.com/',
  73. 'email': username,
  74. 'password': password,
  75. }
  76. request = sanitized_Request(
  77. 'https://www.patreon.com/processLogin',
  78. compat_urllib_parse_urlencode(login_form).encode('utf-8')
  79. )
  80. login_page = self._download_webpage(request, None, note='Logging in')
  81. if re.search(r'onLoginFailed', login_page):
  82. raise ExtractorError('Unable to login, incorrect username and/or password', expected=True)
  83. def _real_initialize(self):
  84. self._login()
  85. '''
  86. def _real_extract(self, url):
  87. video_id = self._match_id(url)
  88. post = self._download_json(
  89. 'https://www.patreon.com/api/posts/' + video_id, video_id, query={
  90. 'fields[media]': 'download_url,mimetype,size_bytes',
  91. 'fields[post]': 'comment_count,content,embed,image,like_count,post_file,published_at,title',
  92. 'fields[user]': 'full_name,url',
  93. 'json-api-use-default-includes': 'false',
  94. 'include': 'media,user',
  95. })
  96. attributes = post['data']['attributes']
  97. title = attributes['title'].strip()
  98. image = attributes.get('image') or {}
  99. info = {
  100. 'id': video_id,
  101. 'title': title,
  102. 'description': clean_html(attributes.get('content')),
  103. 'thumbnail': image.get('large_url') or image.get('url'),
  104. 'timestamp': parse_iso8601(attributes.get('published_at')),
  105. 'like_count': int_or_none(attributes.get('like_count')),
  106. 'comment_count': int_or_none(attributes.get('comment_count')),
  107. }
  108. for i in post.get('included', []):
  109. i_type = i.get('type')
  110. if i_type == 'media':
  111. media_attributes = i.get('attributes') or {}
  112. download_url = media_attributes.get('download_url')
  113. ext = mimetype2ext(media_attributes.get('mimetype'))
  114. if download_url and ext in KNOWN_EXTENSIONS:
  115. info.update({
  116. 'ext': ext,
  117. 'filesize': int_or_none(media_attributes.get('size_bytes')),
  118. 'url': download_url,
  119. })
  120. elif i_type == 'user':
  121. user_attributes = i.get('attributes')
  122. if user_attributes:
  123. info.update({
  124. 'uploader': user_attributes.get('full_name'),
  125. 'uploader_id': str_or_none(i.get('id')),
  126. 'uploader_url': user_attributes.get('url'),
  127. })
  128. if not info.get('url'):
  129. embed_url = try_get(attributes, lambda x: x['embed']['url'])
  130. if embed_url:
  131. info.update({
  132. '_type': 'url',
  133. 'url': embed_url,
  134. })
  135. if not info.get('url'):
  136. post_file = attributes['post_file']
  137. ext = determine_ext(post_file.get('name'))
  138. if ext in KNOWN_EXTENSIONS:
  139. info.update({
  140. 'ext': ext,
  141. 'url': post_file['url'],
  142. })
  143. return info