Skip to content

Commit

Permalink
Download then reinject latest mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 11, 2024
1 parent 417e666 commit 4929b26
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 37 deletions.
46 changes: 11 additions & 35 deletions css_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,31 @@
from css_browserhook import BrowserTabHook as CssTab, inject, remove

CLASS_MAPPINGS = {}
SUCCESSFUL_FETCH_THIS_RUN = False

async def fetch_class_mappings(css_translations_path : str):
global SUCCESSFUL_FETCH_THIS_RUN

if SUCCESSFUL_FETCH_THIS_RUN:
return

setting = store_read("beta_translations")
css_translations_url = "https://api.deckthemes.com/beta.json" if (setting == "1" or setting == "true") else "https://api.deckthemes.com/stable.json"

try:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False), timeout=aiohttp.ClientTimeout(total=2)) as session:
async with session.get(css_translations_url) as response:
if response.status == 200:
with open(css_translations_path, "w", encoding="utf-8") as fp:
json.dump(await response.json(), fp)

SUCCESSFUL_FETCH_THIS_RUN = True
Log(f"Fetched css translations from server")

except Exception as ex:
Log(f"Failed to fetch css translations from server: {str(ex)}")

async def every(__seconds: float, func, *args, **kwargs):
while True:
await func(*args, **kwargs)
await asyncio.sleep(__seconds)

async def initialize_class_mappings():
def initialize_class_mappings():
css_translations_path = os.path.join(get_theme_path(), "css_translations.json")

await fetch_class_mappings(css_translations_path)

asyncio.get_event_loop().create_task(every(60, fetch_class_mappings, css_translations_path))

if not os.path.exists(css_translations_path):
Log("Failed to get css translations from local file")
return

with open(css_translations_path, "r", encoding="utf-8") as fp:
data : dict = json.load(fp)
try:
with open(css_translations_path, "r", encoding="utf-8") as fp:
data : dict = json.load(fp)
except Exception as e:
Log(f"Failed to load css translations from local file: {str(e)}")
return

CLASS_MAPPINGS.clear()

# Data is in the format of { "uid": ["ver1", "ver2", "ver3"]}
for uid in data:
latest_value = data[uid][-1]
for y in data[uid][:-1]:
CLASS_MAPPINGS[y] = latest_value

Log("Loaded css translations from local file")

ALL_INJECTS = []

def helper_get_tab_from_list(tab_list : List[str], cssTab : CssTab) -> str|None:
Expand Down
38 changes: 36 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os, asyncio, sys, time
import os, asyncio, sys, time, aiohttp, json

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
Expand All @@ -25,6 +25,38 @@

Initialized = False

SUCCESSFUL_FETCH_THIS_RUN = False

async def fetch_class_mappings(css_translations_path : str, loader : Loader):
global SUCCESSFUL_FETCH_THIS_RUN

if SUCCESSFUL_FETCH_THIS_RUN:
return

setting = util_store_read("beta_translations")
css_translations_url = "https://api.deckthemes.com/beta.json" if (setting == "1" or setting == "true") else "https://api.deckthemes.com/stable.json"

try:
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False), timeout=aiohttp.ClientTimeout(total=2)) as session:
async with session.get(css_translations_url) as response:
if response.status == 200:
with open(css_translations_path, "w", encoding="utf-8") as fp:
fp.write(await response.text())

SUCCESSFUL_FETCH_THIS_RUN = True
Log(f"Fetched css translations from server")
initialize_class_mappings()
asyncio.get_running_loop().create_task(loader.reset(silent=True))

except Exception as ex:
Log(f"Failed to fetch css translations from server: {str(ex)}")


async def every(__seconds: float, func, *args, **kwargs):
while True:
await func(*args, **kwargs)
await asyncio.sleep(__seconds)

class FileChangeHandler(FileSystemEventHandler):
def __init__(self, loader : Loader, loop):
self.loader = loader
Expand Down Expand Up @@ -158,7 +190,7 @@ async def _main(self):
self.server_loaded = False

Log("Initializing css loader...")
await initialize_class_mappings()
initialize_class_mappings()
Log(f"Max supported manifest version: {CSS_LOADER_VER}")

create_steam_symlink()
Expand All @@ -176,6 +208,8 @@ async def _main(self):
if (ALWAYS_RUN_SERVER or store_or_file_config("server")):
await self.enable_server(self)

css_translations_path = os.path.join(get_theme_path(), "css_translations.json")
asyncio.get_event_loop().create_task(every(60, fetch_class_mappings, css_translations_path, self.loader))
await initialize()

if __name__ == '__main__':
Expand Down

0 comments on commit 4929b26

Please sign in to comment.