diff --git a/app/routes/jellyfin_routes.py b/app/routes/jellyfin_routes.py index a74cd5c..c1e0d57 100644 --- a/app/routes/jellyfin_routes.py +++ b/app/routes/jellyfin_routes.py @@ -170,6 +170,37 @@ def delete_playlist(playlist_id): except Exception as e: flash(f'Failed to remove item: {str(e)}') +@app.route('/refresh_playlist/', methods=['GET']) +@functions.jellyfin_admin_required +def refresh_playlist(playlist_id): + # get the playlist from the database using the playlist_id + playlist = Playlist.query.filter_by(jellyfin_id=playlist_id).first() + # if the playlist has a jellyfin_id, then fetch the playlist from Jellyfin + if playlist.jellyfin_id: + try: + app.logger.debug(f"removing all tracks from playlist {playlist.jellyfin_id}") + jellyfin_playlist = jellyfin.get_music_playlist(session_token=functions._get_api_token(), playlist_id=playlist.jellyfin_id) + jellyfin.remove_songs_from_playlist(session_token=functions._get_token_from_sessioncookie(), playlist_id=playlist.jellyfin_id, song_ids=[track for track in jellyfin_playlist['ItemIds']]) + ordered_tracks = db.session.execute( + db.select(Track, playlist_tracks.c.track_order) + .join(playlist_tracks, playlist_tracks.c.track_id == Track.id) + .where(playlist_tracks.c.playlist_id == playlist.id) + .order_by(playlist_tracks.c.track_order) + ).all() + + tracks = [track.jellyfin_id for track, idx in ordered_tracks if track.jellyfin_id is not None] + #jellyfin.remove_songs_from_playlist(session_token=jellyfin_admin_token, playlist_id=playlist.jellyfin_id, song_ids=tracks) + jellyfin.add_songs_to_playlist(session_token=functions._get_api_token(), user_id=functions._get_admin_id(), playlist_id=playlist.jellyfin_id, song_ids=tracks) + # if the playlist is found, then update the playlist metadata + provider_playlist = MusicProviderRegistry.get_provider(playlist.provider_id).get_playlist(playlist.provider_playlist_id) + functions.update_playlist_metadata(playlist, provider_playlist) + flash('Playlist refreshed') + return jsonify({'success': True}) + + except Exception as e: + flash(f'Failed to refresh playlist: {str(e)}') + return jsonify({'success': False}) + @app.route('/wipe_playlist/', methods=['DELETE']) @functions.jellyfin_admin_required diff --git a/jellyfin/client.py b/jellyfin/client.py index 0b6749b..a6cca1f 100644 --- a/jellyfin/client.py +++ b/jellyfin/client.py @@ -118,6 +118,23 @@ class JellyfinClient: return {"status": "success", "message": "Playlist updated successfully"} else: raise Exception(f"Failed to update playlist: {response.content}") + + def get_music_playlist(self, session_token : str, playlist_id: str): + """ + Get a music playlist by its ID. + :param playlist_id: The ID of the playlist to fetch. + :return: The playlist object + """ + url = f'{self.base_url}/Playlists/{playlist_id}' + self.logger.debug(f"Url={url}") + + response = requests.get(url, headers=self._get_headers(session_token=session_token), timeout = self.timeout) + self.logger.debug(f"Response = {response.status_code}") + + if response.status_code == 200: + return response.json() + else: + raise Exception(f"Failed to get playlist: {response.content}") def get_playlist_metadata(self, session_token: str, user_id: str,playlist_id: str) -> PlaylistMetadata: url = f'{self.base_url}/Items/{playlist_id}' diff --git a/templates/partials/_playlist_info.html b/templates/partials/_playlist_info.html index 656ae86..35dfffb 100644 --- a/templates/partials/_playlist_info.html +++ b/templates/partials/_playlist_info.html @@ -9,6 +9,29 @@

{{ item.track_count }} songs, {{ total_duration }}

Last Updated: {{ item.last_updated}} | Last Change: {{ item.last_changed}}

{% include 'partials/_add_remove_button.html' %} + {% if session['is_admin'] and item.jellyfin_id %} +

+ + +

+ {% endif %} + \ No newline at end of file