the right way to check fod updates ,down from 18 seconds to ~3 seconds (40 items)

This commit is contained in:
faroukbmiled 2023-09-29 16:30:11 +01:00
parent f3d412bcb3
commit 840ac74ece
2 changed files with 38 additions and 27 deletions

View File

@ -351,11 +351,9 @@ def check_item_date(down_date, date_updated):
except ValueError: except ValueError:
download_datetime = datetime.strptime(down_date + f", {current_year}", date_format_with_added_year) download_datetime = datetime.strptime(down_date + f", {current_year}", date_format_with_added_year)
date_updated_datetime = datetime.fromtimestamp(date_updated) if date_updated >= download_datetime:
if date_updated_datetime >= download_datetime:
return True return True
elif date_updated_datetime < download_datetime: elif date_updated < download_datetime:
return False return False
except: except:
return False return False
@ -395,11 +393,25 @@ def item_steam_api(id):
print(e) print(e)
return False return False
def get_item_date(data): def get_item_dates(ids):
try: try:
time_updated = data["response"]["publishedfiledetails"][0]["time_updated"] data = {
return time_updated "itemcount": len(ids),
except: }
return None for i, id in enumerate(ids):
data[f"publishedfileids[{i}]"] = int(id)
info = requests.post(ITEM_INFO_API, data=data)
response_data = info.json()
if "response" in response_data:
item_details = response_data["response"]["publishedfiledetails"]
return {item["publishedfileid"]: item["time_updated"] for item in item_details}
return {}
except Exception as e:
print(e)
return {}
# End helper functions # End helper functions

View File

@ -849,41 +849,40 @@ class LibraryTab(ctk.CTkScrollableFrame):
# Needed to refresh item that needs updates # Needed to refresh item that needs updates
self.to_update.clear() self.to_update.clear()
def if_id_needs_update(item_id, item_date, text): def if_ids_need_update(item_ids, item_dates, texts):
try: try:
item_data = item_steam_api(item_id) item_data = get_item_dates(item_ids)
date_updated = get_item_date(item_data)
if not date_updated: for item_id, date_updated in item_data.items():
return False item_date = item_dates[item_id]
date_updated = datetime.fromtimestamp(date_updated)
if check_item_date(item_date, date_updated): if check_item_date(item_date, date_updated):
self.to_update.add(text + f" | Updated: {date_updated}") date_updated = date_updated.strftime("%d %b @ %I:%M%p, %Y")
return True self.to_update.add(texts[item_id] + f" | Updated: {date_updated}")
else:
return False
except Exception as e: except Exception as e:
show_message("Error", f"Error occured\n{e}", icon="cancel") show_message("Error", f"Error occurred\n{e}", icon="cancel")
return
def check_for_update(): def check_for_update():
try: try:
lib_data = None lib_data = None
if not os.path.exists(os.path.join(application_path, LIBRARY_FILE)): if not os.path.exists(os.path.join(application_path, LIBRARY_FILE)):
show_message("Error checking for item updates! -> Setting is on", "Please visit library tab at least once with the correct boiii path!, you also need to have at lease 1 item!") show_message("Error checking for item updates! -> Setting is on", "Please visit library tab at least once with the correct boiii path!, you also need to have at least 1 item!")
return return
with open(LIBRARY_FILE, 'r') as file: with open(LIBRARY_FILE, 'r') as file:
lib_data = json.load(file) lib_data = json.load(file)
for item in lib_data: item_ids = [item["id"] for item in lib_data]
item_id = item["id"] item_dates = {item["id"]: item["date"] for item in lib_data}
item_date = item["date"] texts = {item["id"]: item["text"] for item in lib_data}
if_id_needs_update(item_id, item_date, item["text"])
if_ids_need_update(item_ids, item_dates, texts)
except: except:
show_message("Error checking for item updates!", "Please visit library tab at least once with the correct boiii path!, you also need to have at lease 1 item!") show_message("Error checking for item updates!", "Please visit the library tab at least once with the correct boiii path!, you also need to have at least 1 item!")
return return
check_for_update() check_for_update()