2021-02-25 22:32:06 +13:00
|
|
|
from src.utils import printToFile as print
|
2020-05-29 06:42:11 +12:00
|
|
|
import io
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
|
2020-06-04 03:10:25 +12:00
|
|
|
from src.errors import FileAlreadyExistsError, TypeInSkip
|
2020-06-02 00:05:02 +12:00
|
|
|
from src.utils import GLOBAL
|
2021-02-06 21:35:50 +13:00
|
|
|
from src.utils import printToFile as print
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
VanillaPrint = print
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
class SelfPost:
|
2021-02-25 22:32:06 +13:00
|
|
|
def __init__(self, directory, post):
|
|
|
|
if "self" in GLOBAL.arguments.skip:
|
|
|
|
raise TypeInSkip
|
2020-06-04 03:10:25 +12:00
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2020-06-02 00:05:02 +12:00
|
|
|
filename = GLOBAL.config['filename'].format(**post)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
file_dir = directory / (filename + ".md")
|
|
|
|
print(file_dir)
|
2021-02-25 22:32:06 +13:00
|
|
|
print(filename + ".md")
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if Path.is_file(file_dir):
|
2020-05-29 06:42:11 +12:00
|
|
|
raise FileAlreadyExistsError
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
try:
|
2021-02-06 21:35:50 +13:00
|
|
|
self.writeToFile(file_dir, post)
|
2020-05-29 06:42:11 +12:00
|
|
|
except FileNotFoundError:
|
2021-02-06 21:35:50 +13:00
|
|
|
file_dir = post['POSTID'] + ".md"
|
|
|
|
file_dir = directory / file_dir
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
self.writeToFile(file_dir, post)
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
@staticmethod
|
2021-02-25 22:32:06 +13:00
|
|
|
def writeToFile(directory, post):
|
2020-05-29 06:42:11 +12:00
|
|
|
"""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
|
|
|
+ ")")
|
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
with io.open(directory, "w", encoding="utf-8") as FILE:
|
|
|
|
VanillaPrint(content, file=FILE)
|
2020-05-29 06:42:11 +12:00
|
|
|
print("Downloaded")
|