aboutsummaryrefslogtreecommitdiffstats
path: root/fileutil.py
diff options
context:
space:
mode:
authorPinapelz <donaldshan1@outlook.com>2023-06-20 02:14:34 -0700
committerPinapelz <donaldshan1@outlook.com>2023-06-20 02:14:34 -0700
commit70237b5a5d82e8425eb5870a975bde497a6def08 (patch)
tree6930d7ae771fb188fc6a3a0f7f1f81bad5bf84f2 /fileutil.py
parentc929c11f9006db67e10ddd7fa599124a6edeadeb (diff)
Refactored entire codebase
- Replaced most code with HTML templates - Fixed some janky SQL queries - Replaced config.py with ini and json
Diffstat (limited to 'fileutil.py')
-rw-r--r--fileutil.py81
1 files changed, 47 insertions, 34 deletions
diff --git a/fileutil.py b/fileutil.py
index 6cc1351..b8f6015 100644
--- a/fileutil.py
+++ b/fileutil.py
@@ -2,6 +2,7 @@ import os.path
import urllib.request
import json
import time
+import configparser
def _read_file(path: str, lines=True) -> list:
@@ -12,29 +13,11 @@ def _read_file(path: str, lines=True) -> list:
return file.read().splitlines()
-def get_login_data(path="config.json"):
- # gets login data from config.json
- data = json.loads(_read_file(os.path.join(path), lines=False))
- try:
- return data["address"], data["user"], data["password"]
- except KeyError:
- raise Exception("Login data not found")
-
-
-def get_api_key(api: str, path="config.json"):
- # gets api key from config.json
- data = json.loads(_read_file(os.path.join(path), lines=False))
- try:
- return data[api]
- except KeyError:
- raise Exception(f"API key for {api} not found")
-
-
def get_excluded_channels():
# gets excluded channels from exclude_channel.txt
- if not os.path.exists(os.path.join("data","exclude_channel.txt")):
- open(os.path.join("data","exclude_channel.txt"), "w").close()
- excluded_channels = _read_file(os.path.join("data","exclude_channel.txt"))
+ if not os.path.exists(os.path.join("data", "exclude_channel.txt")):
+ open(os.path.join("data", "exclude_channel.txt"), "w").close()
+ excluded_channels = _read_file(os.path.join("data", "exclude_channel.txt"))
return excluded_channels
@@ -49,10 +32,11 @@ def save_local_channels(data: list, path: str = "data"):
open(path, "w").close()
with open(path, "w", encoding="utf-8") as file:
for channel in data:
- if channel['id'] in excluded_channels:
+ if channel["id"] in excluded_channels:
continue
file.write(f"{channel['id']},{channel['english_name']}\n")
+
def get_local_channels(path: str = "data"):
"""
Get the channel names and ids locally for when the API is down
@@ -61,28 +45,57 @@ def get_local_channels(path: str = "data"):
if not os.path.exists(path):
raise Exception("Local channel data not found")
with open(path, "r", encoding="utf-8") as file:
- rows = file.read().splitlines()
+ rows = file.read().splitlines()
return [tuple(row.split(",")) for row in rows]
+
def check_diff_refresh():
- if not os.path.exists(os.path.join("data","last_refresh.txt")):
- with open(os.path.join("data","last_refresh.txt"), "w", encoding="utf-8") as file:
+ if not os.path.exists(os.path.join("data", "last_refresh.txt")):
+ with open(
+ os.path.join("data", "last_refresh.txt"), "w", encoding="utf-8"
+ ) as file:
file.write(time.strftime("%Y-%m-%d"))
return True
- with open(os.path.join("data","last_refresh.txt"), "r", encoding="utf-8") as file:
+ with open(os.path.join("data", "last_refresh.txt"), "r", encoding="utf-8") as file:
last_refresh = file.read()
if last_refresh != time.strftime("%Y-%m-%d"):
- with open(os.path.join("data","last_refresh.txt"), "w", encoding="utf-8") as file:
+ with open(
+ os.path.join("data", "last_refresh.txt"), "w", encoding="utf-8"
+ ) as file:
file.write(time.strftime("%Y-%m-%d"))
return True
+
def update_data_files(url: str) -> None:
# Updates the local txt channel data stored in data folder
- if not os.path.exists(os.path.join("data","channels.txt")):
- open(os.path.join("data","channels.txt"), "w").close()
- urllib.request.urlretrieve(url+"channels.txt", os.path.join("data","channels.txt"))
+ if not os.path.exists(os.path.join("data", "channels.txt")):
+ open(os.path.join("data", "channels.txt"), "w").close()
+ urllib.request.urlretrieve(
+ url + "channels.txt", os.path.join("data", "channels.txt")
+ )
# downloaded txt file from url and write to channels.txt
-
- if not os.path.exists(os.path.join("data","exclude_channel.txt")):
- open(os.path.join("data","exclude_channel.txt"), "w").close()
- urllib.request.urlretrieve(url+"exclude_channel.txt", os.path.join("data","exclude_channel.txt"))
+
+ if not os.path.exists(os.path.join("data", "exclude_channel.txt")):
+ open(os.path.join("data", "exclude_channel.txt"), "w").close()
+ urllib.request.urlretrieve(
+ url + "exclude_channel.txt", os.path.join("data", "exclude_channel.txt")
+ )
+
+
+def load_config(ini_filepath: str) -> dict:
+ config_object = configparser.ConfigParser()
+ file = open(ini_filepath, "r")
+ config_object.read_file(file)
+ output_dict = {}
+ sections = config_object.sections()
+ for section in sections:
+ output_dict[section] = {}
+ for key in config_object[section]:
+ output_dict[section][key] = config_object[section][key]
+ return output_dict
+
+def load_json_file(json_file_path: str) -> dict:
+ with open(json_file_path, "r", encoding="utf-8") as file:
+ return json.load(file)
+
+
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage