Skip to content

Commit

Permalink
Use aiohttp instead of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 10, 2024
1 parent d0e151b commit e45675c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions css_inject.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import json
import re
import os
import requests
import aiohttp
import time
from typing import List
from css_utils import Result, Log, store_read, get_theme_path
from css_browserhook import BrowserTabHook as CssTab, inject, remove

CLASS_MAPPINGS = {}

def initialize_class_mappings():
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 = 8
while timeout > 0:
try:
response = requests.get(css_translations_url, timeout=2)
if response.status_code == 200:
with open(css_translations_path, "w", encoding="utf-8") as fp:
json.dump(response.json(), fp)
break
except:
pass
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

time.sleep(0.3)
except Exception as ex:
Log(f"Failed to fetch css translations from server: {str(ex)}")
finally:
timeout -= 1

Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def _main(self):
self.server_loaded = False

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

create_steam_symlink()
Expand Down

0 comments on commit e45675c

Please sign in to comment.