-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
202 lines (162 loc) · 6.6 KB
/
server.py
File metadata and controls
202 lines (162 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import httpx
import yaml
import asyncio
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from contextlib import asynccontextmanager
from typing import List, Dict, Any
DEVICES_URL = "https://raw.githubusercontent.com/XiaomiFirmwareUpdater/xiaomi_devices/master/devices.json"
FIRMWARE_CODENAMES_URL = "https://raw.githubusercontent.com/xiaomifirmwareupdater/xiaomifirmwareupdater.github.io/master/data/firmware_codenames.yml"
MIUI_CODENAMES_URL = "https://raw.githubusercontent.com/xiaomifirmwareupdater/xiaomifirmwareupdater.github.io/master/data/miui_codenames.yml"
VENDOR_CODENAMES_URL = "https://raw.githubusercontent.com/xiaomifirmwareupdater/xiaomifirmwareupdater.github.io/master/data/vendor_codenames.yml"
FIRMWARE_URL = "https://raw.githubusercontent.com/xiaomifirmwareupdater/xiaomifirmwareupdater.github.io/master/data/devices/latest.yml"
MIUI_ROMS_URL = "https://raw.githubusercontent.com/xiaomifirmwareupdater/miui-updates-tracker/master/data/latest.yml"
app_cache = {
"device_list": [],
"codename_to_name": {},
"firmware_codenames": [],
"miui_codenames": [],
"vendor_codenames": [],
"firmware_data": {},
"miui_data": {}
}
async def load_devices_data(client: httpx.AsyncClient):
try:
response = await client.get(DEVICES_URL)
response.raise_for_status()
devices_data = response.json()
device_list = []
codename_map = {}
for codename, details in devices_data.items():
if "display_name_en" in details:
name = details["display_name_en"]
device_list.append({"name": name, "codename": codename})
codename_map[codename] = name
elif "display_name" in details:
name = details["display_name"]
device_list.append({"name": name, "codename": codename})
codename_map[codename] = name
app_cache["device_list"] = device_list
app_cache["codename_to_name"] = codename_map
print(f"Loaded {len(device_list)} devices.")
except Exception as e:
print(f"ERROR fetching devices: {e}")
async def load_yaml_list_data(client: httpx.AsyncClient, url: str, cache_key: str, name: str):
try:
response = await client.get(url)
response.raise_for_status()
data = yaml.safe_load(response.text)
app_cache[cache_key] = data
print(f"Loaded {len(data)} {name}.")
except Exception as e:
print(f"ERROR fetching {name}: {e}")
async def load_firmware_data(client: httpx.AsyncClient):
try:
response = await client.get(FIRMWARE_URL)
response.raise_for_status()
data = yaml.safe_load(response.text)
latest = {}
for item in data:
try:
codename = item['downloads']['github'].split('/')[4].split('_')[-1]
version = item['versions']['miui']
if latest.get(codename):
latest[codename].append(version)
else:
latest[codename] = [version]
except (KeyError, IndexError, TypeError):
continue
app_cache["firmware_data"] = latest
print(f"Loaded firmware data for {len(latest)} devices.")
except Exception as e:
print(f"ERROR fetching firmware: {e}")
async def load_miui_roms_data(client: httpx.AsyncClient):
try:
response = await client.get(MIUI_ROMS_URL)
response.raise_for_status()
roms = yaml.safe_load(response.text)
latest = {}
for item in roms:
try:
codename = item['codename'].split('_')[0]
if latest.get(codename):
latest[codename].append(item)
else:
latest[codename] = [item]
except (KeyError, IndexError, TypeError):
continue
app_cache["miui_data"] = latest
print(f"Loaded MIUI ROMs data for {len(latest)} devices.")
except Exception as e:
print(f"ERROR fetching MIUI ROMs: {e}")
@asynccontextmanager
async def lifespan(app: FastAPI):
print("Server starting up... Fetching all data...")
async with httpx.AsyncClient() as client:
await asyncio.gather(
load_devices_data(client),
load_yaml_list_data(client, FIRMWARE_CODENAMES_URL, "firmware_codenames", "firmware codenames"),
load_yaml_list_data(client, MIUI_CODENAMES_URL, "miui_codenames", "MIUI codenames"),
load_yaml_list_data(client, VENDOR_CODENAMES_URL, "vendor_codenames", "vendor codenames"),
load_firmware_data(client),
load_miui_roms_data(client)
)
print("Server is ready and all data is cached.")
yield
print("Server shutting down...")
app = FastAPI(
title="Xiaomi Software API",
description="API for accessing Xiaomi device firmware and MIUI ROM information",
version="1.0.0",
lifespan=lifespan
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
@app.get("/")
async def root():
return {
"message": "Welcome to the Xiaomi Software API.",
"endpoints": [
"/devices",
"/devices/{codename}/software",
"/codenames"
]
}
@app.get("/devices")
async def get_all_devices() -> List[Dict[str, str]]:
return app_cache["device_list"]
@app.get("/devices/{codename}/software")
async def get_device_software(codename: str) -> Dict[str, Any]:
device_name = app_cache["codename_to_name"].get(codename)
if not device_name:
raise HTTPException(status_code=404, detail="Device codename not found.")
firmware_versions = app_cache["firmware_data"].get(codename, [])
miui_roms = app_cache["miui_data"].get(codename, [])
return {
"name": device_name,
"codename": codename,
"firmware_versions": firmware_versions,
"miui_roms": miui_roms
}
@app.get("/codenames")
async def get_all_codenames() -> Dict[str, List[str]]:
return {
"firmware_codenames": app_cache["firmware_codenames"],
"miui_codenames": app_cache["miui_codenames"],
"vendor_codenames": app_cache["vendor_codenames"],
}
if __name__ == "__main__":
import os
import uvicorn
from dotenv import load_dotenv
load_dotenv()
host = os.getenv("WEB_HOST", "0.0.0.0")
port = int(os.getenv("WEB_PORT", "9837"))
print(f"Starting FastAPI server on {host}:{port}")
uvicorn.run(app, host=host, port=port, log_level="info")