Skip to content

Commit af0ca76

Browse files
committed
Refactor
1 parent eb84d04 commit af0ca76

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

root/app/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Increment when updating schema or forcing an update on start
44
IMAGES_SCHEMA_VERSION = 3
5+
SCARF_SCHEMA_VERSION = 1
56

67

78
class Tag(BaseModel):

root/app/updater.py

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from keyvaluestore import KeyValueStore, set_db_schema
33
from models import Architecture, Changelog, Tag, EnvVar, Volume, Port, Config
44
from models import Custom, SecurityOpt, Device, Cap, Hostname, MacAddress, Image
5-
from models import Repository, ImagesData, ImagesResponse, IMAGES_SCHEMA_VERSION
5+
from models import Repository, ImagesData, ImagesResponse, IMAGES_SCHEMA_VERSION, SCARF_SCHEMA_VERSION
66

77
import datetime
8+
import json
89
import os
910
import requests
1011
import time
@@ -144,23 +145,7 @@ def get_mac_address(readme_vars):
144145
hostname = readme_vars.get("param_mac_address", False)
145146
return MacAddress(mac_address=hostname, desc=readme_vars.get("param_mac_address_desc", ""), optional=optional)
146147

147-
def get_monthly_pulls():
148-
pulls_map = {}
149-
try:
150-
response = requests.get("https://api.scarf.sh/v2/packages/linuxserver-ci/overview?per_page=1000", headers={"Authorization": f"Bearer {SCARF_TOKEN}"})
151-
results = response.json()["results"]
152-
for result in results:
153-
name = result["package"]["name"].replace("linuxserver/", "")
154-
if "total_installs" not in result:
155-
continue
156-
monthly_pulls = result["total_installs"]
157-
pulls_map[name] = monthly_pulls
158-
except Exception:
159-
print(traceback.format_exc())
160-
raise
161-
return pulls_map
162-
163-
def get_image(repo, pulls_map):
148+
def get_image(repo, scarf_data):
164149
print(f"Processing {repo.name}")
165150
if not repo.name.startswith("docker-") or repo.name.startswith("docker-baseimage-"):
166151
return None
@@ -207,7 +192,7 @@ def get_image(repo, pulls_map):
207192
stable=stable,
208193
deprecated=deprecated,
209194
stars=repo.stargazers_count,
210-
monthly_pulls=pulls_map.get(project_name, None),
195+
monthly_pulls=scarf_data.get(project_name, None),
211196
tags=tags,
212197
architectures=get_architectures(readme_vars),
213198
changelog=changelog,
@@ -218,14 +203,14 @@ def update_images():
218203
with KeyValueStore(invalidate_hours=INVALIDATE_HOURS, readonly=False) as kv:
219204
is_current_schema = kv.is_current_schema("images", IMAGES_SCHEMA_VERSION)
220205
if ("images" in kv and is_current_schema) or CI == "1":
221-
print(f"{datetime.datetime.now()} - skipped - already updated")
206+
print(f"{datetime.datetime.now()} - images skipped - already updated")
222207
return
223208
print(f"{datetime.datetime.now()} - updating images")
224-
pulls_map = get_monthly_pulls()
225209
images = []
210+
scarf_data = json.loads(kv["scarf"])
226211
repos = gh.get_repos()
227212
for repo in sorted(repos, key=lambda repo: repo.name):
228-
image = get_image(repo, pulls_map)
213+
image = get_image(repo, scarf_data)
229214
if not image:
230215
continue
231216
images.append(image)
@@ -237,10 +222,41 @@ def update_images():
237222
kv.set_value("images", new_state, IMAGES_SCHEMA_VERSION)
238223
print(f"{datetime.datetime.now()} - updated images")
239224

225+
def get_monthly_pulls():
226+
pulls_map = {}
227+
try:
228+
response = requests.get("https://api.scarf.sh/v2/packages/linuxserver-ci/overview?per_page=1000", headers={"Authorization": f"Bearer {SCARF_TOKEN}"})
229+
results = response.json()["results"]
230+
for result in results:
231+
name = result["package"]["name"].replace("linuxserver/", "")
232+
if "total_installs" not in result:
233+
continue
234+
monthly_pulls = result["total_installs"]
235+
pulls_map[name] = monthly_pulls
236+
except Exception:
237+
print(traceback.format_exc())
238+
return pulls_map
239+
240+
def update_scarf():
241+
with KeyValueStore(invalidate_hours=INVALIDATE_HOURS, readonly=False) as kv:
242+
is_current_schema = kv.is_current_schema("scarf", SCARF_SCHEMA_VERSION)
243+
if ("scarf" in kv and is_current_schema) or CI == "1":
244+
print(f"{datetime.datetime.now()} - scarf skipped - already updated")
245+
return
246+
print(f"{datetime.datetime.now()} - updating scarf")
247+
pulls_map = get_monthly_pulls()
248+
if not pulls_map:
249+
return
250+
new_state = json.dumps(pulls_map)
251+
kv.set_value("scarf", new_state, SCARF_SCHEMA_VERSION)
252+
print(f"{datetime.datetime.now()} - updated scarf")
253+
254+
240255
def main():
241256
set_db_schema()
242257
while True:
243258
gh.print_rate_limit()
259+
update_scarf()
244260
update_images()
245261
gh.print_rate_limit()
246262
time.sleep(INVALIDATE_HOURS*60*60)

0 commit comments

Comments
 (0)