22from keyvaluestore import KeyValueStore , set_db_schema
33from models import Architecture , Changelog , Tag , EnvVar , Volume , Port , Config
44from 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
77import datetime
8+ import json
89import os
910import requests
1011import 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+
240255def 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