self_post.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #!/usr/bin/env python3
  2. import logging
  3. from typing import Optional
  4. from praw.models import Submission
  5. from bdfr.resource import Resource
  6. from bdfr.site_authenticator import SiteAuthenticator
  7. from bdfr.site_downloaders.base_downloader import BaseDownloader
  8. logger = logging.getLogger(__name__)
  9. class SelfPost(BaseDownloader):
  10. def __init__(self, post: Submission):
  11. super().__init__(post)
  12. def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
  13. out = Resource(self.post, self.post.url, lambda: None, '.txt')
  14. out.content = self.export_to_string().encode('utf-8')
  15. out.create_hash()
  16. return [out]
  17. def export_to_string(self) -> str:
  18. """Self posts are formatted here"""
  19. content = ("## ["
  20. + self.post.fullname
  21. + "]("
  22. + self.post.url
  23. + ")\n"
  24. + self.post.selftext
  25. + "\n\n---\n\n"
  26. + "submitted to [r/"
  27. + self.post.subreddit.title
  28. + "](https://www.reddit.com/r/"
  29. + self.post.subreddit.title
  30. + ") by [u/"
  31. + (self.post.author.name if self.post.author else "DELETED")
  32. + "](https://www.reddit.com/user/"
  33. + (self.post.author.name if self.post.author else "DELETED")
  34. + ")")
  35. return content