2018-07-10 07:58:11 +12:00
|
|
|
|
import io
|
|
|
|
|
import sys
|
2021-02-25 22:32:06 +13:00
|
|
|
|
from os import makedirs, path
|
2018-07-10 07:58:11 +12:00
|
|
|
|
from pathlib import Path
|
2021-02-07 01:29:13 +13:00
|
|
|
|
from typing import Optional
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
2021-02-07 14:09:31 +13:00
|
|
|
|
from bulkredditdownloader.json_helper import JsonFile
|
2021-02-25 22:32:06 +13:00
|
|
|
|
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
|
|
|
|
class GLOBAL:
|
|
|
|
|
"""Declare global variables"""
|
2020-06-02 00:05:02 +12:00
|
|
|
|
RUN_TIME = ""
|
2021-02-25 22:32:06 +13:00
|
|
|
|
config = {'imgur_client_id': None, 'imgur_client_secret': None}
|
2018-07-10 07:58:11 +12:00
|
|
|
|
arguments = None
|
|
|
|
|
directory = None
|
2019-02-24 22:28:40 +13:00
|
|
|
|
defaultConfigDirectory = Path.home() / "Bulk Downloader for Reddit"
|
|
|
|
|
configDirectory = ""
|
2020-06-02 00:05:02 +12:00
|
|
|
|
reddit_client_id = "U-6gk4ZCh3IeNQ"
|
|
|
|
|
reddit_client_secret = "7CZHY6AmKweZME5s50SfDGylaPg"
|
2018-07-10 07:58:11 +12:00
|
|
|
|
printVanilla = print
|
2021-02-25 22:32:06 +13:00
|
|
|
|
log_stream = None
|
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
@staticmethod
|
2021-02-07 01:29:13 +13:00
|
|
|
|
def downloadedPosts() -> list:
|
2021-02-06 21:35:50 +13:00
|
|
|
|
return []
|
|
|
|
|
|
2020-06-04 03:10:25 +12:00
|
|
|
|
|
2021-02-07 01:29:13 +13:00
|
|
|
|
def createLogFile(title: str) -> JsonFile:
|
2018-07-10 07:58:11 +12:00
|
|
|
|
"""Create a log file with given name
|
|
|
|
|
inside a folder time stampt in its name and
|
|
|
|
|
put given arguments inside \"HEADER\" key
|
|
|
|
|
"""
|
2021-02-06 21:35:50 +13:00
|
|
|
|
folder_directory = GLOBAL.directory / "LOG_FILES" / GLOBAL.RUN_TIME
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
log_filename = title.upper() + '.json'
|
2020-06-02 00:05:02 +12:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
if not path.exists(folder_directory):
|
|
|
|
|
makedirs(folder_directory)
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
file = JsonFile(folder_directory / Path(log_filename))
|
|
|
|
|
header = " ".join(sys.argv)
|
|
|
|
|
file.add({"HEADER": header})
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
return file
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
def printToFile(*args, no_print=False, **kwargs):
|
2021-02-25 22:32:06 +13:00
|
|
|
|
"""Print to both CONSOLE and
|
2018-07-10 07:58:11 +12:00
|
|
|
|
CONSOLE LOG file in a folder time stampt in the name
|
|
|
|
|
"""
|
2021-02-06 21:35:50 +13:00
|
|
|
|
folder_directory = GLOBAL.directory / Path("LOG_FILES") / Path(GLOBAL.RUN_TIME)
|
2021-02-25 22:32:06 +13:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
if not no_print or GLOBAL.arguments.verbose or "file" in kwargs:
|
2021-02-25 22:32:06 +13:00
|
|
|
|
print(*args, **kwargs)
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
if not path.exists(folder_directory):
|
|
|
|
|
makedirs(folder_directory)
|
2021-02-25 22:32:06 +13:00
|
|
|
|
|
|
|
|
|
if "file" not in kwargs:
|
2021-02-06 21:35:50 +13:00
|
|
|
|
with io.open(folder_directory / "CONSOLE_LOG.txt", "a", encoding="utf-8") as FILE:
|
2021-02-25 22:32:06 +13:00
|
|
|
|
print(*args, file=FILE, **kwargs)
|
|
|
|
|
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
2021-02-07 01:29:13 +13:00
|
|
|
|
def nameCorrector(string: str, reference: Optional[str] = None) -> str:
|
2021-02-25 22:32:06 +13:00
|
|
|
|
"""Swap strange characters from given string
|
2018-07-10 07:58:11 +12:00
|
|
|
|
with underscore (_) and shorten it.
|
|
|
|
|
Return the string
|
|
|
|
|
"""
|
2021-02-06 21:35:50 +13:00
|
|
|
|
limit = 247
|
|
|
|
|
string_length = len(string)
|
2020-06-04 03:10:25 +12:00
|
|
|
|
|
|
|
|
|
if reference:
|
2021-02-06 21:35:50 +13:00
|
|
|
|
reference_length = len(reference)
|
|
|
|
|
total_lenght = reference_length
|
2020-06-04 03:10:25 +12:00
|
|
|
|
else:
|
2021-02-06 21:35:50 +13:00
|
|
|
|
total_lenght = string_length
|
2020-06-04 03:10:25 +12:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
if total_lenght > limit:
|
|
|
|
|
limit -= reference_length
|
2021-02-25 22:32:06 +13:00
|
|
|
|
string = string[:limit - 1]
|
2020-06-04 03:10:25 +12:00
|
|
|
|
|
|
|
|
|
string = string.replace(" ", "_")
|
2021-02-25 22:32:06 +13:00
|
|
|
|
|
2018-07-10 07:58:11 +12:00
|
|
|
|
if len(string.split('\n')) > 1:
|
|
|
|
|
string = "".join(string.split('\n'))
|
2021-02-25 22:32:06 +13:00
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
|
bad_chars = ['\\', '/', ':', '*', '?', '"', '<', '>', '|', '#', '.', '@', '“', '’', '\'', '!']
|
|
|
|
|
string = "".join([i if i not in bad_chars else "_" for i in string])
|
2018-07-10 07:58:11 +12:00
|
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
|
return string
|