Clean up cache paths.

This commit is contained in:
Brian Lee 2024-10-16 07:16:08 -07:00
parent 4f58daa29f
commit 283853d2c4
4 changed files with 36 additions and 29 deletions

View File

@ -31,7 +31,8 @@ class BeatSaverAPI:
def _determine_cache_dir(self) -> str:
home_cache = os.path.expanduser("~/.cache")
beatsaver_cache = os.path.join(home_cache, "beatsaver")
saberlist_cache = os.path.join(home_cache, "saberlist")
beatsaver_cache = os.path.join(saberlist_cache, "beatsaver")
if os.path.exists(home_cache):
if not os.path.exists(beatsaver_cache):
@ -59,7 +60,7 @@ class BeatSaverAPI:
self,
use_cache: bool = True,
page_size: int = 20,
sort: GetMapsLatestSort = GetMapsLatestSort.UPDATED,
sort: GetMapsLatestSort = GetMapsLatestSort.FIRST_PUBLISHED,
verified: bool = True,
month: Optional[int] = None,
year: Optional[int] = None,
@ -86,7 +87,7 @@ class BeatSaverAPI:
start_of_month = datetime(year, month, 1, tzinfo=timezone.utc)
end_of_month = start_of_month + relativedelta(months=1)
cache_file = os.path.join(self.CACHE_DIR, f"maps_{year}_{month:02d}.json")
cache_file = os.path.join(self.CACHE_DIR, f"maps_{year}_{month:02d}_{sort.value}.json")
cached_maps = []
if use_cache and self._is_cache_valid(cache_file):
@ -124,8 +125,8 @@ class BeatSaverAPI:
f"Fetched map: '{map.name}' by {map.metadata.song_author_name} [{map.uploader.name}, "
# f"created at {map.created_at.strftime('%Y-%m-%dT%H:%M:%S')}, "
# f"last published at {map.last_published_at.strftime('%Y-%m-%dT%H:%M:%S')}, "
f"updated at {map.updated_at.strftime('%m-%d %H:%M')}, "
# f"uploaded on {map.uploaded.strftime('%Y-%m-%dT%H:%M:%S')}"
# f"updated at {map.updated_at.strftime('%m-%d %H:%M')}, "
f"uploaded on {map.uploaded.strftime('%Y-%m-%dT%H:%M:%S')}"
)
new_maps = [map for map in maps.docs if map not in all_maps]

View File

@ -27,7 +27,8 @@ class ScoreSaberAPI:
def _determine_cache_dir(self) -> str:
home_cache = os.path.expanduser("~/.cache")
scoresaber_cache = os.path.join(home_cache, "scoresaber")
saberlist_cache = os.path.join(home_cache, "saberlist")
scoresaber_cache = os.path.join(saberlist_cache, "scoresaber")
if os.path.exists(home_cache):
if not os.path.exists(scoresaber_cache):

View File

@ -27,6 +27,7 @@ class SimpleBeatLeaderAPI:
def _determine_cache_dir(self):
home_cache = os.path.expanduser("~/.cache")
saberlist_cache = os.path.join(home_cache, "saberlist")
beatleader_cache = os.path.join(saberlist_cache, "beatleader")
if os.path.exists(home_cache):
if not os.path.exists(saberlist_cache):
@ -36,7 +37,7 @@ class SimpleBeatLeaderAPI:
except OSError as e:
logging.warning(f"Failed to create {saberlist_cache}: {e}")
return os.path.join(os.getcwd(), ".cache")
return saberlist_cache
return beatleader_cache
else:
logging.info("~/.cache doesn't exist, using local .cache directory")
return os.path.join(os.getcwd(), ".cache")

View File

@ -116,19 +116,26 @@ def infer_beatleader_leaderboard_id(song_id: str, difficulty: str) -> str:
def playlist_strategy_scoresaber_oldscores(
api: ScoreSaberAPI,
song_count: int = 20 # Total number of songs to select
song_count: int = 20
) -> List[Dict[str, Any]]:
"""Build and format a list of songs based on old scores from ScoreSaber, avoiding reusing the same song+difficulty."""
player_id = prompt_for_player_id()
history = load_history()
history.setdefault('scoresaber_oldscores', {})
history.setdefault('playlist_counts', {})
# Get the current count for ScoreSaber old scores and increment it
count_key = 'scoresaber_oldscores'
current_count = history['playlist_counts'].get(count_key, 0)
new_count = current_count + 1
history['playlist_counts'][count_key] = new_count
scores_data = api.get_player_scores(player_id, use_cache=True)
all_scores = scores_data.get('playerScores', [])
if not all_scores:
logging.warning(f"No scores found for player ID {player_id}.")
return []
return [], ""
logging.debug(f"Found {len(all_scores)} scores for player ID {player_id}.")
# Sort scores by timeSet in ascending order (oldest first)
@ -208,28 +215,28 @@ def playlist_strategy_scoresaber_oldscores(
history['scoresaber_oldscores'].setdefault(song_id, []).append(difficulty_name)
save_history(history)
return playlist_data
return playlist_data, f"scoresaber_oldscores-{new_count:02d}"
def playlist_strategy_beatleader_oldscores(
api: BeatLeaderAPI,
song_count: int = 20
) -> List[Dict[str, Any]]:
"""
Build and format a list of songs based on old scores from BeatLeader,
avoiding reusing the same song+difficulty.
The playlist will consist of song hashes and their corresponding difficulties.
"""
player_id = prompt_for_player_id()
history = load_history()
history.setdefault('beatleader_oldscores', {})
history.setdefault('playlist_counts', {})
# Get the current count for BeatLeader old scores and increment it
count_key = 'beatleader_oldscores'
current_count = history['playlist_counts'].get(count_key, 0)
new_count = current_count + 1
history['playlist_counts'][count_key] = new_count
scores_data = api.get_player_scores(player_id)
all_scores = scores_data.get('playerScores', [])
if not all_scores:
logging.warning(f"No scores found for player ID {player_id} on BeatLeader.")
return []
return [], ""
logging.debug(f"Found {len(all_scores)} scores for player ID {player_id} on BeatLeader.")
# Sort scores by epochTime in ascending order (oldest first)
@ -300,7 +307,7 @@ def playlist_strategy_beatleader_oldscores(
save_history(history)
return playlist_data
return playlist_data, f"beatleader_oldscores-{new_count:02d}"
def map_leaders_by_month(month: int = 9, year: int = 2024, game_modes: List[str] = ['Standard']) -> List[Dict]:
"""
@ -408,8 +415,8 @@ def playlist_strategy_highest_accuracy(
except ValueError:
print("Invalid input. Please enter numbers only.")
# Get the current count for this year/month and increment it
count_key = f"{year}-{month:02d}"
# Get the current count for highest accuracy and increment it
count_key = f"highest_accuracy-{year}-{month:02d}"
current_count = history['playlist_counts'].get(count_key, 0)
new_count = current_count + 1
history['playlist_counts'][count_key] = new_count
@ -418,7 +425,7 @@ def playlist_strategy_highest_accuracy(
if not leaderboard_data:
logging.error(f"No map+difficulty data available for {calendar.month_name[month]} {year}.")
return []
return [], ""
# Sort the data by average_accuracy in descending order
sorted_data = sorted(
@ -470,13 +477,10 @@ def saberlist() -> None:
"""
strategy = get_strategy()
timestamp = datetime.now().strftime("%y%m%d_%H%M%S")
playlist_title = f"{strategy}-{timestamp}"
if strategy == 'scoresaber_oldscores':
playlist_data = playlist_strategy_scoresaber_oldscores(ScoreSaberAPI(cache_expiry_days=CACHE_EXPIRY_DAYS))
playlist_data, playlist_title = playlist_strategy_scoresaber_oldscores(ScoreSaberAPI(cache_expiry_days=CACHE_EXPIRY_DAYS))
elif strategy == 'beatleader_oldscores':
playlist_data = playlist_strategy_beatleader_oldscores(BeatLeaderAPI(cache_expiry_days=CACHE_EXPIRY_DAYS))
playlist_data, playlist_title = playlist_strategy_beatleader_oldscores(BeatLeaderAPI(cache_expiry_days=CACHE_EXPIRY_DAYS))
elif strategy == 'highest_accuracy':
playlist_data, playlist_title = playlist_strategy_highest_accuracy()
else: