generated from SteamDeckHomebrew/Plugin-Template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only fetch translations when connected to Steam
- Loading branch information
1 parent
e21965d
commit 698719e
Showing
4 changed files
with
88 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os, asyncio, aiohttp | ||
from css_loader import get_loader_instance | ||
from css_utils import Log, store_read, is_steam_beta_active, get_theme_path | ||
from css_inject import initialize_class_mappings | ||
from css_browserhook import ON_WEBSOCKET_CONNECT | ||
|
||
STARTED_FETCHING_TRANSLATIONS = False | ||
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") | ||
|
||
if ((len(setting.strip()) <= 0 or setting == "-1" or setting == "auto") and is_steam_beta_active()) or (setting == "1" or setting == "true"): | ||
css_translations_url = "https://api.deckthemes.com/beta.json" | ||
else: | ||
css_translations_url = "https://api.deckthemes.com/stable.json" | ||
|
||
Log(f"Fetching CSS mappings from {css_translations_url}") | ||
|
||
try: | ||
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False, use_dns_cache=False), timeout=aiohttp.ClientTimeout(total=30)) as session: | ||
async with session.get(css_translations_url) as response: | ||
if response.status == 200: | ||
text = await response.text() | ||
|
||
if len(text.strip()) <= 0: | ||
raise Exception("Empty response") | ||
|
||
with open(css_translations_path, "w", encoding="utf-8") as fp: | ||
fp.write(text) | ||
|
||
SUCCESSFUL_FETCH_THIS_RUN = True | ||
Log(f"Fetched css translations from server") | ||
initialize_class_mappings() | ||
asyncio.get_running_loop().create_task(get_loader_instance().reset(silent=True)) | ||
|
||
except Exception as ex: | ||
Log(f"Failed to fetch css translations from server [{type(ex).__name__}]: {str(ex)}") | ||
|
||
async def __every(__seconds: float, func, *args, **kwargs): | ||
global SUCCESSFUL_FETCH_THIS_RUN | ||
|
||
await ON_WEBSOCKET_CONNECT.wait() | ||
|
||
while not SUCCESSFUL_FETCH_THIS_RUN: | ||
await func(*args, **kwargs) | ||
await asyncio.sleep(__seconds) | ||
|
||
async def force_fetch_translations(): | ||
global SUCCESSFUL_FETCH_THIS_RUN | ||
|
||
SUCCESSFUL_FETCH_THIS_RUN = False | ||
css_translations_path = os.path.join(get_theme_path(), "css_translations.json") | ||
await __fetch_class_mappings(css_translations_path) | ||
|
||
def start_fetch_translations(): | ||
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters