Skip to content

Commit 6e8839e

Browse files
committed
Use database to fetch playlist progress
1 parent 310d57c commit 6e8839e

File tree

6 files changed

+28
-51
lines changed

6 files changed

+28
-51
lines changed

backend/src/controllers/music_data.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,23 @@ def get_playback_info():
133133
if playback_info is None:
134134
return ("", 204)
135135
if playback_info.playlist_id is not None:
136+
playlist_name = get_playlist_by_id_or_none(playback_info.playlist_id).name
136137
playlist_duration = get_playlist_duration(playback_info.playlist_id)
137138
playlist_progress = (
138139
get_playlist_duration_up_to_track(
139140
playback_info.playlist_id, playback_info.track_id
140141
)
141142
+ playback_info.track_progress
142143
)
143-
return PlaylistProgression.model_validate(
144-
{
145-
"playlist_id": playback_info.playlist_id,
146-
"playlist_title": "test",
147-
"playlist_progress": playlist_progress,
148-
"playlist_duration": playlist_duration,
144+
145+
playback_info_with_playlist = playback_info.model_dump()
146+
playback_info_with_playlist["playlist"] = {
147+
"id": playback_info.playlist_id,
148+
"title": playlist_name,
149+
"progress": playlist_progress,
150+
"duration": playlist_duration,
149151
}
150-
)
152+
return playback_info_with_playlist
151153

152154
return playback_info.model_dump_json()
153155

backend/src/database/crud/playlist.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ def get_playlist_duration(playlist_id):
300300

301301

302302
def get_playlist_duration_up_to_track(playlist_id, track_id):
303-
print(playlist_id)
304303
# Query to get all tracks in the albums of the playlist, with proper joins and ordering
305304
tracks_in_playlist = (
306305
DbTrack.select(DbTrack.id, DbTrack.duration_ms)
@@ -327,5 +326,4 @@ def get_playlist_duration_up_to_track(playlist_id, track_id):
327326
total_duration += track.duration_ms
328327
if track.id == track_id:
329328
break
330-
print(total_duration)
331329
return total_duration

frontend/src/api/index.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Album } from "../interfaces/Album";
2-
import { PlaybackInfo, PlaylistProgress } from "../interfaces/PlaybackInfo";
2+
import { PlaybackInfo } from "../interfaces/PlaybackInfo";
33
import { Playlist } from "../interfaces/Playlist";
44
import { User } from "../interfaces/User";
55
import { backendUrl, request } from "./jsonRequest";
@@ -100,17 +100,7 @@ export const getPlaylistAlbums = async (
100100
};
101101

102102
export const getPlaybackInfo = async (): Promise<PlaybackInfo> => {
103-
return jsonRequest(`spotify/playback`, RequestMethod.GET, undefined, false);
104-
};
105-
106-
export const getPlaylistProgress = async (
107-
playbackInfo: PlaybackInfo,
108-
): Promise<PlaylistProgress> => {
109-
return jsonRequest(
110-
`spotify/playlist_progress`,
111-
RequestMethod.POST,
112-
playbackInfo,
113-
);
103+
return jsonRequest(`music/playback`, RequestMethod.GET, undefined, false);
114104
};
115105

116106
export const findAssociatedPlaylists = async (

frontend/src/context/PlaybackContext.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import React, {
55
useEffect,
66
useState,
77
} from "react";
8-
import { PlaybackInfo, PlaylistProgress } from "../interfaces/PlaybackInfo";
8+
import { PlaybackInfo } from "../interfaces/PlaybackInfo";
99
import { useQuery } from "@tanstack/react-query";
10-
import { getPlaybackInfo, getPlaylistProgress } from "../api";
10+
import { getPlaybackInfo } from "../api";
1111

1212
interface PlaybackContext {
1313
playbackInfo?: PlaybackInfo;
14-
playlistProgress?: PlaylistProgress;
1514
}
1615

1716
export const PlaybackContext = createContext<PlaybackContext>({});
@@ -36,21 +35,9 @@ export const PlaybackContextProvider: FC<PlaybackContextProviderProps> = ({
3635
useEffect(() => {
3736
setPlaybackRefetchInterval(playbackInfo ? 10000 : 20000);
3837
}, [playbackInfo]);
39-
const { data: playlistProgress } = useQuery<PlaylistProgress | undefined>({
40-
queryKey: ["playlistProgress"],
41-
queryFn: () => {
42-
if (playbackInfo?.playlist_id) {
43-
return getPlaylistProgress(playbackInfo);
44-
}
45-
},
46-
retryDelay: playbackRefetchInterval,
47-
refetchInterval: 60000,
48-
refetchIntervalInBackground: false,
49-
enabled: !!playbackInfo?.playlist_id,
50-
});
5138

5239
return (
53-
<PlaybackContext.Provider value={{ playbackInfo, playlistProgress }}>
40+
<PlaybackContext.Provider value={{ playbackInfo }}>
5441
{children}
5542
</PlaybackContext.Provider>
5643
);

frontend/src/interfaces/PlaybackInfo.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ export interface PlaybackInfo {
33
track_id: string;
44
album_title: string;
55
album_id: string;
6-
playlist_id?: string;
76
track_artists: string[];
87
album_artists: string[];
98
artwork_url: string;
@@ -12,11 +11,12 @@ export interface PlaybackInfo {
1211
album_progress: number;
1312
album_duration: number;
1413
is_playing: boolean;
14+
playlist?: PlaylistProgress
1515
}
1616

1717
export interface PlaylistProgress {
18-
playlist_id: string;
19-
playlist_title: string;
20-
playlist_progress: number;
21-
playlist_duration: number;
18+
id: string;
19+
title: string;
20+
progress: number;
21+
duration: number;
2222
}

frontend/src/presentational/PlaybackFooter.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { usePlaybackContext } from "../hooks/usePlaybackContext";
88
import { Link } from "react-router-dom";
99

1010
const PlaybackFooter: FC = () => {
11-
const { playbackInfo, playlistProgress } = usePlaybackContext();
11+
const { playbackInfo } = usePlaybackContext();
1212

1313
if (!playbackInfo) return null;
1414

@@ -63,29 +63,29 @@ const PlaybackFooter: FC = () => {
6363
</div>
6464
</div>
6565

66-
{playbackInfo?.playlist_id && (
66+
{playbackInfo.playlist?.id && (
6767
<div
6868
className={`flex flex-row justify-between ${
69-
playlistProgress ? "" : "opacity-0"
69+
playbackInfo.playlist ? "" : "opacity-0"
7070
}`}
7171
>
7272
<div className="flex flex-row space-x-2">
7373
<PlaylistIcon className="my-auto w-8 h-8 fill-primary-darker" />
7474
<div className="my-auto text-balance">
75-
{playlistProgress && (
76-
<Link to={`edit/${playlistProgress?.playlist_id}`}>
77-
{playlistProgress?.playlist_title}
75+
{playbackInfo.playlist && (
76+
<Link to={`edit/${playbackInfo.playlist?.id}`}>
77+
{playbackInfo.playlist?.title}
7878
</Link>
7979
)}
8080
</div>
8181
</div>
8282
<div className="w-12 h-12 sm:w-16 sm:h-16 my-auto">
8383
<ProgressCircle
8484
percentage={
85-
playlistProgress
85+
playbackInfo.playlist
8686
? Math.round(
87-
(playlistProgress.playlist_progress /
88-
playlistProgress.playlist_duration) *
87+
(playbackInfo.playlist.progress /
88+
playbackInfo.playlist.duration) *
8989
100
9090
)
9191
: 0

0 commit comments

Comments
 (0)