from os import sep from os.path import exists from shutil import copyfile from typing import List, Optional from ci import client, sqlite from json import load from defs.format_time import now_time from defs.utils import Module new_modules: List[Module] = [] new_modules_index: dict = {} old_modules: List[Module] = [] old_modules_index: dict = {} if exists(f"data{sep}modules.json"): with open(f"data{sep}modules.json", "r", encoding="utf-8") as file: temp_data = load(file) for temp in temp_data: new_modules.append(Module(temp)) if exists(f"data{sep}old_modules.json"): with open(f"data{sep}old_modules.json", "r", encoding="utf-8") as file: temp_data = load(file) for temp in temp_data: old_modules.append(Module(temp)) new_modules_index = {i.name: i for i in new_modules} old_modules_index = {i.name: i.latestRelease for i in old_modules} async def update_data() -> None: global new_modules, old_modules, new_modules_index if exists(f"data{sep}modules.json"): copyfile(f"data{sep}modules.json", f"data{sep}old_modules.json") data = await client.get("https://modules.lsposed.org/modules.json") with open(f"data{sep}modules.json", "w", encoding="utf-8") as f: f.write(data.text) data = data.json() old_modules = new_modules new_modules = [] for i in data: new_modules.append(Module(i)) new_modules_index = {i.name: i for i in new_modules} sqlite["update_time"] = now_time() def compare() -> List[Module]: global old_modules_index data = [] old_modules_index = {i.name: i.latestRelease for i in old_modules} for i in new_modules: if i.latestRelease != old_modules_index.get(i.name, ""): data.append(i) return data async def download(url: str, name: str, pack_name: str) -> (str, str): content = await client.get(url) if content.status_code == 404: content = (await client.get(f"https://modules.lsposed.org/module/{pack_name}.json")).json() data = Module(content) url = None if data.releases: if data.releases[0].releaseAssets: url = data.releases[0].releaseAssets[0].url mime = data.releases[0].releaseAssets[0].name.split(".")[-1:][0] name = data.name.replace('.', '_') + "-" + data.latestRelease + "." + mime if url: content = await client.get(url) if content.status_code == 404: raise FileNotFoundError else: raise FileNotFoundError content = content.content with open(f"data{sep}{name}", 'wb') as f: f.write(content) return f"data{sep}{name}", url def from_name_to_module(name: str) -> Optional[Module]: return new_modules_index.get(name, None) def from_list_to_name(data: List) -> str: data_ = "" for i in data: name = new_modules_index.get(i, None) if isinstance(name, Module): data_ += f"\n{name.name}({name.description})" return data_ def from_keyword_to_module(keyword: str) -> Optional[Module]: for value in new_modules: data = value.name + value.description + value.url + value.homepageUrl + value.summary + \ value.sourceUrl if value.scope: for i in value.scope: data += i if keyword in data: return value return None