Move to different program structure

This commit is contained in:
Serene-Arc
2021-02-11 09:10:40 +10:00
committed by Ali Parlakci
parent a72abd6603
commit a7f1db14e5
24 changed files with 504 additions and 2133 deletions

View File

@@ -1,64 +1,39 @@
#!/usr/bin/env python3
import io
import logging
import pathlib
from pathlib import Path
from praw.models import Submission
from bulkredditdownloader.resource import Resource
from bulkredditdownloader.site_downloaders.base_downloader import BaseDownloader
from bulkredditdownloader.errors import FileAlreadyExistsError, TypeInSkip
from bulkredditdownloader.utils import GLOBAL
logger = logging.getLogger(__name__)
class SelfPost(BaseDownloader):
def __init__(self, directory: pathlib.Path, post: dict):
def __init__(self, directory: pathlib.Path, post: Submission):
super().__init__(directory, post)
self.download()
def download(self):
if "self" in GLOBAL.arguments.skip:
raise TypeInSkip
return Resource(self.post, self.post.url, bytes(self.export_to_string()))
self.directory.mkdir(exist_ok=True)
filename = GLOBAL.config['filename'].format(**self.post)
file_dir = self.directory / (filename + ".md")
logger.info(file_dir)
logger.info(filename + ".md")
if Path.is_file(file_dir):
raise FileAlreadyExistsError
try:
self._write_to_file(file_dir, self.post)
except FileNotFoundError:
file_dir = self.post['POSTID'] + ".md"
file_dir = self.directory / file_dir
self._write_to_file(file_dir, self.post)
@staticmethod
def _write_to_file(directory: pathlib.Path, post: dict):
def export_to_string(self) -> str:
"""Self posts are formatted here"""
content = ("## ["
+ post["TITLE"]
+ self.post.fullname
+ "]("
+ post["CONTENTURL"]
+ self.post.url
+ ")\n"
+ post["CONTENT"]
+ self.post.selftext
+ "\n\n---\n\n"
+ "submitted to [r/"
+ post["SUBREDDIT"]
+ self.post.subreddit.title
+ "](https://www.reddit.com/r/"
+ post["SUBREDDIT"]
+ self.post.subreddit.title
+ ") by [u/"
+ post["REDDITOR"]
+ self.post.author.name
+ "](https://www.reddit.com/user/"
+ post["REDDITOR"]
+ self.post.author.name
+ ")")
with io.open(directory, "w", encoding="utf-8") as FILE:
print(content, file=FILE)
logger.info("Downloaded")
return content