submission_archive_entry.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. # coding=utf-8
  3. import logging
  4. import praw.models
  5. from bdfr.archive_entry.base_archive_entry import BaseArchiveEntry
  6. logger = logging.getLogger(__name__)
  7. class SubmissionArchiveEntry(BaseArchiveEntry):
  8. def __init__(self, submission: praw.models.Submission):
  9. super(SubmissionArchiveEntry, self).__init__(submission)
  10. def compile(self) -> dict:
  11. comments = self._get_comments()
  12. self._get_post_details()
  13. out = self.post_details
  14. out['comments'] = comments
  15. return out
  16. def _get_post_details(self):
  17. self.post_details = {
  18. 'title': self.source.title,
  19. 'name': self.source.name,
  20. 'url': self.source.url,
  21. 'selftext': self.source.selftext,
  22. 'score': self.source.score,
  23. 'upvote_ratio': self.source.upvote_ratio,
  24. 'permalink': self.source.permalink,
  25. 'id': self.source.id,
  26. 'author': self.source.author.name if self.source.author else 'DELETED',
  27. 'link_flair_text': self.source.link_flair_text,
  28. 'num_comments': self.source.num_comments,
  29. 'over_18': self.source.over_18,
  30. 'spoiler': self.source.spoiler,
  31. 'pinned': self.source.pinned,
  32. 'locked': self.source.locked,
  33. 'distinguished': self.source.distinguished,
  34. 'created_utc': self.source.created_utc,
  35. }
  36. def _get_comments(self) -> list[dict]:
  37. logger.debug(f'Retrieving full comment tree for submission {self.source.id}')
  38. comments = []
  39. self.source.comments.replace_more(0)
  40. for top_level_comment in self.source.comments:
  41. comments.append(self._convert_comment_to_dict(top_level_comment))
  42. return comments