k-mktr commited on
Commit
2a37e4a
·
verified ·
1 Parent(s): 2c850f8

Update leaderboard.py

Browse files
Files changed (1) hide show
  1. leaderboard.py +27 -15
leaderboard.py CHANGED
@@ -11,23 +11,31 @@ elo_ratings = {}
11
 
12
  def load_leaderboard() -> Dict[str, Any]:
13
  try:
14
- with config.get_nextcloud_client_instance() as nc: # Use as context manager
15
- file_content = nc.files.download(config.NEXTCLOUD_LEADERBOARD_PATH)
16
- if file_content: # Check if content is not empty
17
- return json.loads(file_content.decode('utf-8'))
18
- else:
19
- print(f"Error loading leaderboard: Received empty content from Nextcloud at {config.NEXTCLOUD_LEADERBOARD_PATH}")
20
- return {}
 
 
 
 
21
  except Exception as e:
22
  print(f"Error loading leaderboard: {str(e)}")
23
  return {}
24
 
25
  def save_leaderboard(leaderboard_data: Dict[str, Any]) -> bool:
26
  try:
27
- with config.get_nextcloud_client_instance() as nc: # Use as context manager
28
- json_data = json.dumps(leaderboard_data, indent=2)
29
- nc.files.upload(config.NEXTCLOUD_LEADERBOARD_PATH, json_data.encode('utf-8'))
30
- return True
 
 
 
 
31
  except Exception as e:
32
  print(f"Error saving leaderboard: {str(e)}")
33
  return False
@@ -264,10 +272,14 @@ def create_backup():
264
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
265
  backup_file_name = f"leaderboard_backup_{timestamp}.json"
266
  backup_path = f"{config.NEXTCLOUD_BACKUP_FOLDER}/{backup_file_name}"
267
- with config.get_nextcloud_client_instance() as nc: # Use as context manager
268
- json_data = json.dumps(leaderboard_data, indent=2)
269
- nc.files.upload(backup_path, json_data.encode('utf-8'))
270
- print(f"Backup created on Nextcloud: {backup_path}")
 
 
 
 
271
  except Exception as e:
272
  print(f"Error creating backup: {e}")
273
  time.sleep(43200) # Sleep for 12 HOURS
 
11
 
12
  def load_leaderboard() -> Dict[str, Any]:
13
  try:
14
+ nc = Nextcloud(
15
+ nextcloud_url=config.NEXTCLOUD_URL,
16
+ nc_auth_user=config.NEXTCLOUD_USERNAME,
17
+ nc_auth_pass=config.NEXTCLOUD_PASSWORD
18
+ )
19
+ file_content = nc.files.download(config.NEXTCLOUD_LEADERBOARD_PATH)
20
+ if file_content: # Check if content is not empty
21
+ return json.loads(file_content.decode('utf-8'))
22
+ else:
23
+ print(f"Error loading leaderboard: Received empty content from Nextcloud at {config.NEXTCLOUD_LEADERBOARD_PATH}")
24
+ return {}
25
  except Exception as e:
26
  print(f"Error loading leaderboard: {str(e)}")
27
  return {}
28
 
29
  def save_leaderboard(leaderboard_data: Dict[str, Any]) -> bool:
30
  try:
31
+ nc = Nextcloud(
32
+ nextcloud_url=config.NEXTCLOUD_URL,
33
+ nc_auth_user=config.NEXTCLOUD_USERNAME,
34
+ nc_auth_pass=config.NEXTCLOUD_PASSWORD
35
+ )
36
+ json_data = json.dumps(leaderboard_data, indent=2)
37
+ nc.files.upload(config.NEXTCLOUD_LEADERBOARD_PATH, json_data.encode('utf-8'))
38
+ return True
39
  except Exception as e:
40
  print(f"Error saving leaderboard: {str(e)}")
41
  return False
 
272
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
273
  backup_file_name = f"leaderboard_backup_{timestamp}.json"
274
  backup_path = f"{config.NEXTCLOUD_BACKUP_FOLDER}/{backup_file_name}"
275
+ nc = Nextcloud(
276
+ nextcloud_url=config.NEXTCLOUD_URL,
277
+ nc_auth_user=config.NEXTCLOUD_USERNAME,
278
+ nc_auth_pass=config.NEXTCLOUD_PASSWORD
279
+ )
280
+ json_data = json.dumps(leaderboard_data, indent=2)
281
+ nc.files.upload(backup_path, json_data.encode('utf-8'))
282
+ print(f"Backup created on Nextcloud: {backup_path}")
283
  except Exception as e:
284
  print(f"Error creating backup: {e}")
285
  time.sleep(43200) # Sleep for 12 HOURS