1
0
Fork 0
mirror of synced 2024-10-03 02:27:32 +13:00
bulk-downloader-for-reddit/bulkredditdownloader/utils.py

91 lines
2.5 KiB
Python
Raw Normal View History

2018-07-10 07:58:11 +12:00
import io
import sys
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
2018-07-10 07:58:11 +12:00
class GLOBAL:
"""Declare global variables"""
RUN_TIME = ""
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 = ""
reddit_client_id = "U-6gk4ZCh3IeNQ"
reddit_client_secret = "7CZHY6AmKweZME5s50SfDGylaPg"
2018-07-10 07:58:11 +12:00
printVanilla = print
log_stream = None
@staticmethod
2021-02-07 01:29:13 +13:00
def downloadedPosts() -> list:
return []
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
"""
folder_directory = GLOBAL.directory / "LOG_FILES" / GLOBAL.RUN_TIME
2018-07-10 07:58:11 +12:00
log_filename = title.upper() + '.json'
if not path.exists(folder_directory):
makedirs(folder_directory)
2018-07-10 07:58:11 +12:00
file = JsonFile(folder_directory / Path(log_filename))
header = " ".join(sys.argv)
file.add({"HEADER": header})
2018-07-10 07:58:11 +12:00
return file
2018-07-10 07:58:11 +12:00
def printToFile(*args, no_print=False, **kwargs):
"""Print to both CONSOLE and
2018-07-10 07:58:11 +12:00
CONSOLE LOG file in a folder time stampt in the name
"""
folder_directory = GLOBAL.directory / Path("LOG_FILES") / Path(GLOBAL.RUN_TIME)
if not no_print or GLOBAL.arguments.verbose or "file" in kwargs:
print(*args, **kwargs)
2018-07-10 07:58:11 +12:00
if not path.exists(folder_directory):
makedirs(folder_directory)
if "file" not in kwargs:
with io.open(folder_directory / "CONSOLE_LOG.txt", "a", encoding="utf-8") as FILE:
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:
"""Swap strange characters from given string
2018-07-10 07:58:11 +12:00
with underscore (_) and shorten it.
Return the string
"""
limit = 247
string_length = len(string)
if reference:
reference_length = len(reference)
total_lenght = reference_length
else:
total_lenght = string_length
if total_lenght > limit:
limit -= reference_length
string = string[:limit - 1]
string = string.replace(" ", "_")
2018-07-10 07:58:11 +12:00
if len(string.split('\n')) > 1:
string = "".join(string.split('\n'))
bad_chars = ['\\', '/', ':', '*', '?', '"', '<', '>', '|', '#', '.', '@', '', '', '\'', '!']
string = "".join([i if i not in bad_chars else "_" for i in string])
2018-07-10 07:58:11 +12:00
return string