From 770bd7ce56bc7d64880e320dce02a4dff29710a1 Mon Sep 17 00:00:00 2001 From: Brian Lee Date: Tue, 17 Sep 2024 15:37:36 -0700 Subject: [PATCH] Fixed minor issues and added convenience rsync for litten in shell script. --- convert-comyfui-outupt.sh | 4 +++ src/saberlist/beatleader.py | 25 +++++++++++++++--- src/saberlist/beatleaderAPI.py | 46 ++++++++++++++++++---------------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/convert-comyfui-outupt.sh b/convert-comyfui-outupt.sh index ffe5117..e07745e 100755 --- a/convert-comyfui-outupt.sh +++ b/convert-comyfui-outupt.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +set -e # =============================================================== # PNG to JPEG Converter for ComfyUI Output # =============================================================== @@ -25,6 +26,9 @@ # # =============================================================== +if [ $(hostname) = "litten" ]; then + rsync -tap roar:/opt/comfyui/output/dev-dancing comfyui-output/ +fi # Create the output directory if it doesn't exist mkdir -p ./covers diff --git a/src/saberlist/beatleader.py b/src/saberlist/beatleader.py index 1d42a50..c29394f 100644 --- a/src/saberlist/beatleader.py +++ b/src/saberlist/beatleader.py @@ -84,8 +84,21 @@ def build_difficulty_based_playlist(api, player_id, history, playlist_name): history[song_id] = [] history[song_id].append(difficulty_name) + # Prepare the custom playlist + custom_playlist = [] + for score in playlist_scores: + custom_playlist.append({ + 'song': score['leaderboard']['song'], + 'difficulty': score['leaderboard']['difficulty'] + }) + playlist_file, used_cover = api.create_player_playlist_with_random_cover( - player_id, playlist_name, "SaberList Tool", len(playlist_scores), True, set(history['used_covers']) + player_id, + playlist_name, + "SaberList Tool", + custom_playlist=playlist_scores, # Pass the playlist_scores directly + use_cache=True, + used_covers=set(history['used_covers']) ) if used_cover: @@ -100,7 +113,7 @@ def saberlist_replay_bl(): if player_info: logging.info(f"Fetching score history for player: {player_info.get('name', 'N/A')}") - + history = load_history() # Generate a unique playlist name @@ -110,7 +123,13 @@ def saberlist_replay_bl(): playlist_file, playlist_scores, used_cover = build_difficulty_based_playlist(api, player_id, history, playlist_name) save_history(history) - print(f"Playlist created: {playlist_file}") + # Create the full path for the cover image + cover_image_path = os.path.join("covers", used_cover) if used_cover else None + + # Create the bplist file using create_bplist() + bplist_file = api.create_bplist(playlist_scores, playlist_name, "SaberList Tool", cover_image_path) + + print(f"Playlist created: {bplist_file}") if used_cover: print(f"Cover image used: {used_cover}") print("Playlist contents:") diff --git a/src/saberlist/beatleaderAPI.py b/src/saberlist/beatleaderAPI.py index aa97f6a..ff6ba19 100644 --- a/src/saberlist/beatleaderAPI.py +++ b/src/saberlist/beatleaderAPI.py @@ -142,15 +142,14 @@ class BeatLeaderAPI: return self.CACHE_DIR - def create_bplist(self, scores, playlist_title="playlist", playlist_author="SaberList Tool", song_limit=0, cover_image=None): + def create_bplist(self, playlist_data, playlist_title="playlist", playlist_author="SaberList Tool", cover_path=None): """ - Create a bplist (JSON) file in the current directory from the given scores data. + Create a bplist (JSON) file in the current directory from the given playlist data. - :param scores: List of score data from get_player_scores + :param playlist_data: List of score data or custom playlist data :param playlist_title: Title of the playlist (default: "playlist") :param playlist_author: Author of the playlist (default: "SaberList Tool") - :param song_limit: Maximum number of songs to include (0 for no limit) - :param cover_image: Path to the cover image file (optional) + :param cover_path: Path to the cover image file (optional) :return: Path to the created bplist file """ playlist = { @@ -159,17 +158,18 @@ class BeatLeaderAPI: "songs": [] } - if cover_image: - with open(cover_image, "rb") as image_file: + if cover_path: + with open(cover_path, "rb") as image_file: encoded_image = base64.b64encode(image_file.read()).decode('utf-8') playlist["image"] = f"data:image/jpeg;base64,{encoded_image}" - # Determine the number of songs to include - num_songs = len(scores) if song_limit == 0 else min(song_limit, len(scores)) - - for score in scores[:num_songs]: - song = score['leaderboard']['song'] - difficulty = score['leaderboard']['difficulty'] + for item in playlist_data: + if 'leaderboard' in item: # It's a score object + song = item['leaderboard']['song'] + difficulty = item['leaderboard']['difficulty'] + else: # It's a custom playlist item + song = item['song'] + difficulty = item['difficulty'] song_entry = { "hash": song['hash'], @@ -192,7 +192,7 @@ class BeatLeaderAPI: with open(filename, 'w') as file: json.dump(playlist, file, indent=4) - logging.info(f"Playlist written to {filename} with {num_songs} songs") + logging.info(f"Playlist written to {filename} with {len(playlist['songs'])} songs") return os.path.abspath(filename) @@ -210,20 +210,23 @@ class BeatLeaderAPI: scores_data = self.get_player_scores(player_id, use_cache=use_cache) return self.create_bplist(scores_data['data'], playlist_title, playlist_author, song_limit) - - def create_player_playlist_with_random_cover(self, player_id, playlist_title="playlist", playlist_author="SaberList Tool", song_limit=0, use_cache=True, used_covers=None): + def create_player_playlist_with_random_cover(self, player_id, playlist_title="playlist", playlist_author="SaberList Tool", custom_playlist=None, use_cache=True, used_covers=None): """ - Create a bplist (JSON) file for a player's scores with a random cover image. + Create a bplist (JSON) file for a player's scores or a custom playlist with a random cover image. :param player_id: ID of the player :param playlist_title: Title of the playlist (default: "playlist") :param playlist_author: Author of the playlist (default: "SaberList Tool") - :param song_limit: Maximum number of songs to include (0 for no limit) + :param custom_playlist: Custom playlist data (optional) :param use_cache: Whether to use cached scores data (default: True) :param used_covers: Set of already used cover image filenames :return: Path to the created bplist file, and the filename of the used cover """ - scores_data = self.get_player_scores(player_id, use_cache=use_cache) + if custom_playlist is None: + scores_data = self.get_player_scores(player_id, use_cache=use_cache) + playlist_data = scores_data['data'] + else: + playlist_data = custom_playlist covers_dir = "./covers" @@ -236,12 +239,13 @@ class BeatLeaderAPI: if not available_covers: logging.warning("No unused cover images available. Using no cover.") - return self.create_bplist(scores_data['data'], playlist_title, playlist_author, song_limit), None + return self.create_bplist(playlist_data, playlist_title, playlist_author), None selected_cover = random.choice(available_covers) cover_path = os.path.join(covers_dir, selected_cover) - playlist_file = self.create_bplist(scores_data['data'], playlist_title, playlist_author, song_limit, cover_path) + playlist_file = self.create_bplist(playlist_data, playlist_title, playlist_author, cover_path=cover_path) + return playlist_file, selected_cover def get_player_info(self, player_id):