From cdcd795b26a4927265e9e0ca307103f97430e3e2 Mon Sep 17 00:00:00 2001 From: ny Date: Thu, 25 Dec 2025 04:05:03 -0500 Subject: [PATCH] feat: init --- .gitignore | 1 + main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ schema.sql | 17 +++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 schema.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..98e6ef6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.db diff --git a/main.py b/main.py new file mode 100644 index 0000000..f9d2e08 --- /dev/null +++ b/main.py @@ -0,0 +1,47 @@ +import httpx +from nyutils import pp +from nyutils.sqlutils import db_to_py, sqlite3 + +def fetch_servers(l): + # Get each server's name / address from a List + return [Server(0, l.lid, s['name'], s['address']) for s in httpx.get(l.url).json()['servers']] # type: ignore + +def down_servers(l): + # Get the servers from a List that are down / return errors + servers = fetch_servers(l) + scodes = {s.name: httpx.get(s.url, headers={"User-Agent": "SideStore"}).status_code for s in servers} + return {s: scodes[s] for s in scodes if scodes[s] != 200} + +if __name__ == '__main__': + # Open / Create db + ani = sqlite3.connect('ani.db') + + # Run creation schema script + with open("schema.sql", "r") as f: + ani.executescript(f.read()) + + # Convert db tables into Python types + db_to_py(ani, globals()) + # Dynamically generated List + classmethod + l = List.select(ani) # type: ignore + + if len(l) == 0: + # Insert default if none exist + List(0, "https://servers.sidestore.io/servers.json").insert(ani) # type: ignore + l = List.select(ani)[0] # type: ignore + else: + # Select first list + l = l[0] + + # Fetch servers from list and attempt to insert them into db + for s in fetch_servers(l): + s.insert(ani) + + # Commit table creations / insertions + ani.commit() + + # print info + pp(List.select(ani)) # type: ignore + pp(Server.select(ani)) # type: ignore + pp(down_servers(l)) + diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..28e298f --- /dev/null +++ b/schema.sql @@ -0,0 +1,17 @@ + +-- Anisette list +CREATE TABLE IF NOT EXISTS list ( + lid INTEGER PRIMARY KEY, + url TEXT UNIQUE NOT NULL +); + +-- Anisette server entry in list +CREATE TABLE IF NOT EXISTS server ( + sid INTEGER PRIMARY KEY, + list INTEGER NOT NULL, + name TEXT NOT NULL, + url TEXT NOT NULL, + UNIQUE (name, url), + FOREIGN KEY (list) REFERENCES list(lid) +); +