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