logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

patreon.py (5646B)


  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