2021-02-07 17:46:20 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
2021-02-25 23:40:08 +13:00
|
|
|
from typing import Optional
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-11 12:10:40 +13:00
|
|
|
from praw.models import Submission
|
|
|
|
|
2021-04-12 19:58:32 +12:00
|
|
|
from bdfr.resource import Resource
|
|
|
|
from bdfr.site_authenticator import SiteAuthenticator
|
|
|
|
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 14:33:19 +13:00
|
|
|
class SelfPost(BaseDownloader):
|
2021-02-15 18:12:27 +13:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
2021-02-07 17:46:20 +13:00
|
|
|
|
2021-02-26 21:57:05 +13:00
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2021-03-11 14:38:21 +13:00
|
|
|
out = Resource(self.post, self.post.url, '.txt')
|
2021-02-28 12:40:42 +13:00
|
|
|
out.content = self.export_to_string().encode('utf-8')
|
|
|
|
out.create_hash()
|
|
|
|
return [out]
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-11 12:10:40 +13:00
|
|
|
def export_to_string(self) -> str:
|
2020-05-29 06:42:11 +12:00
|
|
|
"""Self posts are formatted here"""
|
|
|
|
content = ("## ["
|
2021-02-11 12:10:40 +13:00
|
|
|
+ self.post.fullname
|
2020-05-29 06:42:11 +12:00
|
|
|
+ "]("
|
2021-02-11 12:10:40 +13:00
|
|
|
+ self.post.url
|
2020-05-29 06:42:11 +12:00
|
|
|
+ ")\n"
|
2021-02-11 12:10:40 +13:00
|
|
|
+ self.post.selftext
|
2020-05-29 06:42:11 +12:00
|
|
|
+ "\n\n---\n\n"
|
|
|
|
+ "submitted to [r/"
|
2021-02-11 12:10:40 +13:00
|
|
|
+ self.post.subreddit.title
|
2020-05-29 06:42:11 +12:00
|
|
|
+ "](https://www.reddit.com/r/"
|
2021-02-11 12:10:40 +13:00
|
|
|
+ self.post.subreddit.title
|
2020-05-29 06:42:11 +12:00
|
|
|
+ ") by [u/"
|
2021-03-29 21:21:47 +13:00
|
|
|
+ (self.post.author.name if self.post.author else "DELETED")
|
2020-05-29 06:42:11 +12:00
|
|
|
+ "](https://www.reddit.com/user/"
|
2021-03-29 21:21:47 +13:00
|
|
|
+ (self.post.author.name if self.post.author else "DELETED")
|
2020-05-29 06:42:11 +12:00
|
|
|
+ ")")
|
2021-02-11 12:10:40 +13:00
|
|
|
return content
|