-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.py
executable file
·168 lines (136 loc) · 4.65 KB
/
app.py
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
#!/usr/bin/env python3
"""
Flask app that walks through a given directory to index
ROM ZIPs and renders web pages using templates.
"""
import json
import os
from datetime import datetime
import arrow
from flask import Flask, jsonify, render_template
# pylint: disable=missing-docstring,invalid-name
app = Flask(__name__)
DEVICE_JSON = "devices.json"
BUILDS_JSON = "builds.json"
ALLOWED_BUILDTYPES = ["official", "gapps"]
ALLOWED_VERSIONS = ("9.0", "10", "11")
DOWNLOAD_BASE_URL = os.environ.get("DOWNLOAD_BASE_URL", "https://get.aosip.dev")
def get_devices() -> dict:
"""
Returns a dictionary with the list of codenames and actual
device names
"""
with open(DEVICE_JSON, "r") as f:
data = json.loads(f.read())
return {d["codename"]: d["device"] for d in data}
def get_zips() -> list:
"""
Returns list of available builds after reading the builds.json
"""
with open(BUILDS_JSON, "r") as f:
builds = json.loads(f.read())
return [build["filename"] for device in builds for build in builds[device]]
def get_latest(device: str, romtype: str) -> dict:
if device not in get_devices().keys():
return {}
with open("builds.json", "r") as builds:
builds = json.loads(builds.read()).get(device, [])
for build in builds:
if build["type"] == romtype:
return build
return {}
##########################
# API
##########################
@app.route("/")
def show_files():
"""
Render the template with ZIP info
"""
zips = get_zips()
devices = get_devices()
build_dates = {}
for zip_file in zips:
zip_file = os.path.splitext(zip_file)[0]
device = zip_file.split("-")[3]
if device not in devices:
devices[device] = device
build_date = zip_file.split("-")[4]
if device not in build_dates or build_date > build_dates[device]:
build_dates[device] = build_date
for device in build_dates:
build_dates[device] = datetime.strftime(
datetime.strptime(build_dates[device], "%Y%m%d"), "%A, %d %B - %Y"
)
devices = {
device: device_name
for device, device_name in devices.items()
if device in build_dates
}
return render_template("latest.html", devices=devices, build_dates=build_dates)
@app.route("/<string:target_device>")
def latest_device(target_device: str) -> str:
"""
Show the latest release for the current device
"""
available_files = {}
with open(BUILDS_JSON, "r") as f:
json_data = json.loads(f.read())
if target_device not in json_data:
return f"There isn't any build for {target_device} available here!"
for build in json_data[target_device]:
buildtype = build.get("type")
if buildtype in ALLOWED_BUILDTYPES:
available_files[buildtype] = build.get("filename")
if build.get("fastboot_images"):
available_files[buildtype + "-img"] = build.get("filename").replace(
".zip", "-img.zip"
)
if build.get("boot_image"):
available_files[buildtype + "-boot"] = build.get("filename").replace(
".zip", "-boot.img"
)
with open(DEVICE_JSON, "r") as f:
json_data = json.loads(f.read())
for device in json_data:
if device["codename"] == target_device:
xda_url = device.get("xda")
model = device.get("device")
maintainers = device.get("maintainer")
break
else:
model = target_device
xda_url = None
maintainers = None
if available_files:
return render_template(
"device.html",
available_files=available_files,
device=target_device,
model=model,
xda=xda_url,
maintainer=maintainers,
)
@app.route("/<string:device>/<string:romtype>")
def ota(device: str, romtype: str):
if rom := get_latest(device, romtype):
return jsonify(
{
"response": [
{
"id": rom["sha256"],
"url": "{}{}{}".format(
DOWNLOAD_BASE_URL, rom["filepath"], rom["filename"]
),
"romtype": rom["type"],
"datetime": int(arrow.get(rom["date"]).timestamp()),
"version": rom["version"],
"filename": rom["filename"],
"size": rom["size"],
}
]
}
)
return jsonify([])
if __name__ == "__main__":
app.run()