Skip to content

Commit

Permalink
Does this work?
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 10, 2024
1 parent 31685e4 commit cead164
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions css_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@
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

async def initialize_class_mappings():
css_translations_path = os.path.join(get_theme_path(), "css_translations.json")
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"

timeout = 20
while timeout > 0:
try:
async with aiohttp.ClientSession(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)
try:
async with aiohttp.ClientSession(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)

break
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():
css_translations_path = os.path.join(get_theme_path(), "css_translations.json")

await asyncio.sleep(0.5)
except Exception as ex:
Log(f"Failed to fetch css translations from server: {str(ex)}")
await asyncio.sleep(3)
finally:
timeout -= 1
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 server or from local file")
Log("Failed to get css translations from local file")
return

with open(css_translations_path, "r", encoding="utf-8") as fp:
Expand Down

0 comments on commit cead164

Please sign in to comment.