Lsposed_Modules_Updates_Tra.../defs/source.py
2022-08-08 14:38:00 +08:00

97 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
new_modules.extend(Module(temp) for temp in temp_data)
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)
old_modules.extend(Module(temp) for temp in temp_data)
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 = [Module(i) for i in data]
new_modules_index.clear()
for i in new_modules:
new_modules_index[i.name] = i
sqlite["update_time"] = now_time()
def compare() -> List[Module]:
global old_modules_index
old_modules_index = {i.name: i.latestRelease for i in old_modules}
data = [i for i in new_modules if i.latestRelease != old_modules_index.get(i.name, "")]
if len(data) > 10:
await update_data()
data = []
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 and 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 not url:
raise FileNotFoundError
content = await client.get(url)
if content.status_code == 404:
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