1
0
Fork 0
mirror of synced 2024-07-03 05:21:02 +12:00
bulk-downloader-for-reddit/src/downloaders/selfPost.py

62 lines
1.8 KiB
Python
Raw Normal View History

from src.utils import printToFile as print
import io
import os
2021-02-07 01:29:13 +13:00
import pathlib
from pathlib import Path
from src.errors import FileAlreadyExistsError, TypeInSkip
from src.utils import GLOBAL
from src.utils import printToFile as print
VanillaPrint = print
class SelfPost:
2021-02-07 01:29:13 +13:00
def __init__(self, directory: pathlib.Path, post: dict):
if "self" in GLOBAL.arguments.skip:
raise TypeInSkip
if not os.path.exists(directory):
os.makedirs(directory)
filename = GLOBAL.config['filename'].format(**post)
file_dir = directory / (filename + ".md")
print(file_dir)
print(filename + ".md")
if Path.is_file(file_dir):
raise FileAlreadyExistsError
try:
self.writeToFile(file_dir, post)
except FileNotFoundError:
file_dir = post['POSTID'] + ".md"
file_dir = directory / file_dir
self.writeToFile(file_dir, post)
@staticmethod
2021-02-07 01:29:13 +13:00
def writeToFile(directory: pathlib.Path, post: dict):
"""Self posts are formatted here"""
content = ("## ["
+ post["TITLE"]
+ "]("
+ post["CONTENTURL"]
+ ")\n"
+ post["CONTENT"]
+ "\n\n---\n\n"
+ "submitted to [r/"
+ post["SUBREDDIT"]
+ "](https://www.reddit.com/r/"
+ post["SUBREDDIT"]
+ ") by [u/"
+ post["REDDITOR"]
+ "](https://www.reddit.com/user/"
+ post["REDDITOR"]
+ ")")
with io.open(directory, "w", encoding="utf-8") as FILE:
VanillaPrint(content, file=FILE)
print("Downloaded")