1
0
Fork 0
mirror of synced 2024-06-03 10:54:38 +12:00
bulk-downloader-for-reddit/bdfr/site_downloaders/self_post.py

44 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import logging
2021-02-25 23:40:08 +13:00
from typing import Optional
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
logger = logging.getLogger(__name__)
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-26 21:57:05 +13:00
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
2021-07-27 15:39:49 +12:00
out = Resource(self.post, self.post.url, lambda: None, '.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-11 12:10:40 +13:00
def export_to_string(self) -> str:
"""Self posts are formatted here"""
content = ("## ["
2021-02-11 12:10:40 +13:00
+ self.post.fullname
+ "]("
2021-02-11 12:10:40 +13:00
+ self.post.url
+ ")\n"
2021-02-11 12:10:40 +13:00
+ self.post.selftext
+ "\n\n---\n\n"
+ "submitted to [r/"
2021-02-11 12:10:40 +13:00
+ self.post.subreddit.title
+ "](https://www.reddit.com/r/"
2021-02-11 12:10:40 +13:00
+ self.post.subreddit.title
+ ") by [u/"
+ (self.post.author.name if self.post.author else "DELETED")
+ "](https://www.reddit.com/user/"
+ (self.post.author.name if self.post.author else "DELETED")
+ ")")
2021-02-11 12:10:40 +13:00
return content