Spaces:
Running
Running
Upload 2 files
Browse files- config.py +7 -8
- remote_storage.py +72 -0
config.py
CHANGED
|
@@ -16,6 +16,13 @@ GIST_TOKEN = os.getenv("GIST_TOKEN", "") # GitHub Pe
|
|
| 16 |
_admin_ids_raw = os.getenv("ADMIN_USER_IDS", "123456789")
|
| 17 |
ADMIN_USER_IDS = [int(uid.strip()) for uid in _admin_ids_raw.split(",") if uid.strip().isdigit()]
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
# DYNAMIC CONFIGURATION (IMPORT INSTANCE DIRECTLY)
|
| 21 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -38,14 +45,6 @@ SUBJECT_ROTATION = {
|
|
| 38 |
"Sunday": ["physics", "maths", "biology"],
|
| 39 |
}
|
| 40 |
|
| 41 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 42 |
-
# REPEAT PREVENTION
|
| 43 |
-
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 44 |
-
|
| 45 |
-
HISTORY_DEPTH = 60 # How many past topics to remember before reuse is allowed
|
| 46 |
-
HISTORY_FILE = "data/content_history.json"
|
| 47 |
-
HISTORY_FILE_NAME = "content_history.json" # Filename inside the Gist
|
| 48 |
-
|
| 49 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
# GROQ SETTINGS
|
| 51 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 16 |
_admin_ids_raw = os.getenv("ADMIN_USER_IDS", "123456789")
|
| 17 |
ADMIN_USER_IDS = [int(uid.strip()) for uid in _admin_ids_raw.split(",") if uid.strip().isdigit()]
|
| 18 |
|
| 19 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 20 |
+
# REPEAT PREVENTION
|
| 21 |
+
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 22 |
+
HISTORY_DEPTH = 60
|
| 23 |
+
HISTORY_FILE = "data/content_history.json"
|
| 24 |
+
HISTORY_FILE_NAME = "content_history.json"
|
| 25 |
+
|
| 26 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 27 |
# DYNAMIC CONFIGURATION (IMPORT INSTANCE DIRECTLY)
|
| 28 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 45 |
"Sunday": ["physics", "maths", "biology"],
|
| 46 |
}
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
# GROQ SETTINGS
|
| 50 |
# βββββββββββββββββββββββββββββββββββββββββββββ
|
remote_storage.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import logging
|
| 3 |
+
import requests
|
| 4 |
+
logger = logging.getLogger(__name__)
|
| 5 |
+
|
| 6 |
+
def pull_from_gist(filename: str) -> dict:
|
| 7 |
+
"""Pull a specific JSON file from the GitHub Gist."""
|
| 8 |
+
from config import GIST_ID, GIST_TOKEN
|
| 9 |
+
if not GIST_ID or not GIST_TOKEN:
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
url = f"https://api.github.com/gists/{GIST_ID}"
|
| 14 |
+
headers = {"Authorization": f"token {GIST_TOKEN}"}
|
| 15 |
+
resp = requests.get(url, headers=headers, timeout=10)
|
| 16 |
+
|
| 17 |
+
if resp.status_code == 200:
|
| 18 |
+
gist_data = resp.json()
|
| 19 |
+
files = gist_data.get("files", {})
|
| 20 |
+
if filename in files:
|
| 21 |
+
content = files[filename].get("content", "{}")
|
| 22 |
+
logger.info(f"Successfully pulled {filename} from Gist")
|
| 23 |
+
return json.loads(content)
|
| 24 |
+
elif resp.status_code == 401:
|
| 25 |
+
logger.error(f"Gist Pull Failed ({filename}): [401 Unauthorized] Check your GIST_TOKEN.")
|
| 26 |
+
elif resp.status_code != 404:
|
| 27 |
+
logger.error(f"Failed to pull {filename} from Gist (Status {resp.status_code})")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
logger.error(f"Error pulling {filename} from Gist: {e}")
|
| 30 |
+
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def push_to_gist(filename: str, data_dict: dict):
|
| 35 |
+
"""Push a dictionary to a specific file in the GitHub Gist."""
|
| 36 |
+
from config import GIST_ID, GIST_TOKEN
|
| 37 |
+
if not GIST_ID or not GIST_TOKEN:
|
| 38 |
+
return
|
| 39 |
+
|
| 40 |
+
try:
|
| 41 |
+
url = f"https://api.github.com/gists/{GIST_ID}"
|
| 42 |
+
headers = {"Authorization": f"token {GIST_TOKEN}"}
|
| 43 |
+
|
| 44 |
+
content = json.dumps(data_dict, indent=2)
|
| 45 |
+
payload = {
|
| 46 |
+
"files": {
|
| 47 |
+
filename: {
|
| 48 |
+
"content": content
|
| 49 |
+
}
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
resp = requests.patch(url, headers=headers, json=payload, timeout=10)
|
| 54 |
+
|
| 55 |
+
if resp.status_code == 200:
|
| 56 |
+
logger.info(f"Successfully pushed {filename} to Gist")
|
| 57 |
+
elif resp.status_code == 401:
|
| 58 |
+
logger.error(f"Gist Push Failed ({filename}): [401 Unauthorized] Check your GIST_TOKEN.")
|
| 59 |
+
else:
|
| 60 |
+
logger.error(f"Failed to push {filename} to Gist (Status {resp.status_code})")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
logger.error(f"Error pushing {filename} to Gist: {e}")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# ββ Legacy Wrappers (to avoid breaking other files) βββββββββββββββββββββββββββ
|
| 66 |
+
def pull_history_from_gist() -> dict:
|
| 67 |
+
from config import HISTORY_FILE_NAME
|
| 68 |
+
return pull_from_gist(HISTORY_FILE_NAME)
|
| 69 |
+
|
| 70 |
+
def push_history_to_gist(history_dict: dict):
|
| 71 |
+
from config import HISTORY_FILE_NAME
|
| 72 |
+
push_to_gist(HISTORY_FILE_NAME, history_dict)
|