Lsposed_Modules_Updates_Tra.../defs/source.py

100 lines
3.7 KiB
Python
Raw Normal View History

2022-03-19 17:44:35 +00:00
from os import sep
from os.path import exists
from shutil import copyfile
2022-03-20 05:02:17 +00:00
from typing import List, Optional
2022-03-19 17:44:35 +00:00
2022-08-15 02:50:53 +00:00
from httpx import AsyncClient
2022-08-15 03:08:16 +00:00
from ci import sqlite, headers, max_update_file
2022-03-19 17:44:35 +00:00
from json import load
from defs.format_time import now_time
from defs.utils import Module
new_modules: List[Module] = []
2022-03-20 05:02:17 +00:00
new_modules_index: dict = {}
2022-03-19 17:44:35 +00:00
old_modules: List[Module] = []
2022-03-20 05:02:17 +00:00
old_modules_index: dict = {}
2022-03-19 17:44:35 +00:00
if exists(f"data{sep}modules.json"):
with open(f"data{sep}modules.json", "r", encoding="utf-8") as file:
2022-03-20 05:02:17 +00:00
temp_data = load(file)
2022-08-08 06:38:00 +00:00
new_modules.extend(Module(temp) for temp in temp_data)
2022-03-19 17:44:35 +00:00
if exists(f"data{sep}old_modules.json"):
with open(f"data{sep}old_modules.json", "r", encoding="utf-8") as file:
2022-03-20 05:02:17 +00:00
temp_data = load(file)
2022-08-08 06:38:00 +00:00
old_modules.extend(Module(temp) for temp in temp_data)
2022-03-20 05:02:17 +00:00
new_modules_index = {i.name: i for i in new_modules}
old_modules_index = {i.name: i.latestRelease for i in old_modules}
2022-03-19 17:44:35 +00:00
async def update_data() -> None:
2022-03-20 05:02:17 +00:00
global new_modules, old_modules, new_modules_index
2022-03-19 17:44:35 +00:00
if exists(f"data{sep}modules.json"):
copyfile(f"data{sep}modules.json", f"data{sep}old_modules.json")
2022-08-15 02:50:53 +00:00
async with AsyncClient(timeout=10.0, headers=headers, follow_redirects=True) as client:
data = await client.get("https://raw.githubusercontent.com/Xposed-Modules-Repo/modules/gh-pages/modules.json")
2022-03-19 17:44:35 +00:00
with open(f"data{sep}modules.json", "w", encoding="utf-8") as f:
f.write(data.text)
data = data.json()
old_modules = new_modules
2022-08-08 06:38:00 +00:00
new_modules = [Module(i) for i in data]
new_modules_index.clear()
for i in new_modules:
new_modules_index[i.name] = i
2022-03-19 17:44:35 +00:00
sqlite["update_time"] = now_time()
def compare() -> List[Module]:
2022-03-20 05:02:17 +00:00
global old_modules_index
2022-08-15 03:08:16 +00:00
old_modules_index.clear()
for i in old_modules:
old_modules_index[i.name] = i.latestRelease
2022-08-08 06:38:00 +00:00
data = [i for i in new_modules if i.latestRelease != old_modules_index.get(i.name, "")]
2022-08-15 03:08:16 +00:00
return [] if len(data) > max_update_file else data
2022-03-19 17:44:35 +00:00
2022-03-21 01:45:54 +00:00
async def download(url: str, name: str, pack_name: str) -> (str, str):
2022-08-15 02:50:53 +00:00
async with AsyncClient(timeout=10.0, headers=headers, follow_redirects=True) as client:
2022-08-08 06:38:00 +00:00
content = await client.get(url)
2022-08-15 02:50:53 +00:00
if content.status_code == 404:
content = (await client.get(f"https://raw.githubusercontent.com/Xposed-Modules-Repo/modules/gh-pages/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)
2022-08-08 06:38:00 +00:00
if content.status_code == 404:
raise FileNotFoundError
2022-03-19 17:44:35 +00:00
content = content.content
with open(f"data{sep}{name}", 'wb') as f:
f.write(content)
2022-03-21 01:45:54 +00:00
return f"data{sep}{name}", url
2022-03-20 05:02:17 +00:00
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