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

47 lines
1.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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]:
2022-12-03 18:11:17 +13:00
out = Resource(self.post, self.post.url, lambda: None, ".txt")
out.content = self.export_to_string().encode("utf-8")
2021-02-28 12:40:42 +13:00
out.create_hash()
return [out]
2021-02-11 12:10:40 +13:00
def export_to_string(self) -> str:
"""Self posts are formatted here"""
2022-12-03 18:11:17 +13:00
content = (
"## ["
+ self.post.fullname
+ "]("
+ self.post.url
+ ")\n"
+ self.post.selftext
+ "\n\n---\n\n"
+ "submitted to [r/"
+ self.post.subreddit.title
+ "](https://www.reddit.com/r/"
+ 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