Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit e4e94be

Browse files
committed
Show spotify liked songs
Signed-off-by: Sahil Gupte <[email protected]>
1 parent b3ddd82 commit e4e94be

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

src/types/declarations/spotifyResponses.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
declare namespace SpotifyResponses {
1212
enum ApiResources {
1313
USER_DETAILS = 'me',
14+
LIKED_SONGS = 'me/tracks',
1415
PLAYLISTS = 'me/playlists',
1516
PLAYLIST = 'playlists/{playlist_id}',
1617
PLAYLIST_ITEMS = 'playlists/{playlist_id}/tracks',
@@ -49,6 +50,13 @@ declare namespace SpotifyResponses {
4950
}
5051
}
5152

53+
type LikedSongsRequest = {
54+
params: {
55+
limit?: number
56+
offset?: number
57+
}
58+
}
59+
5260
type PlaylistItemsRequest = {
5361
params: {
5462
playlist_id: string
@@ -125,6 +133,8 @@ declare namespace SpotifyResponses {
125133
? ArtistAlbumsRequest
126134
: T extends ApiResources.ALBUM_SONGS
127135
? AlbumTracksRequest
136+
: T extends ApiResources.LIKED_SONGS
137+
? LikedSongsRequest
128138
: undefined
129139

130140
interface Image {
@@ -438,5 +448,7 @@ declare namespace SpotifyResponses {
438448
? ArtistAlbumsResponse
439449
: T extends ApiResources.ALBUM_SONGS
440450
? AlbumTracksResponse
451+
: T extends ApiResources.LIKED_SONGS
452+
? PlaylistItems.PlaylistItems
441453
: undefined
442454
}

src/utils/ui/providers/spotify.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const BASE_URL = 'https://api.spotify.com/v1/'
2828

2929
enum ApiResources {
3030
USER_DETAILS = 'me',
31+
LIKED_SONGS = 'me/tracks',
3132
PLAYLISTS = 'me/playlists',
3233
PLAYLIST = 'playlists/{playlist_id}',
3334
PLAYLIST_ITEMS = 'playlists/{playlist_id}/tracks',
@@ -59,7 +60,7 @@ export class SpotifyProvider extends GenericAuth implements GenericProvider, Gen
5960
clientId: id,
6061
clientSecret: secret,
6162
redirectUri: 'https://moosync.app/spotify',
62-
scope: 'playlist-read-private user-top-read',
63+
scope: 'playlist-read-private user-top-read user-library-read',
6364
keytarService: 'MoosyncSpotifyRefreshToken',
6465
oAuthChannel: oauthChannel
6566
}
@@ -194,6 +195,12 @@ export class SpotifyProvider extends GenericAuth implements GenericProvider, Gen
194195
const playlists: Playlist[] = []
195196

196197
if (this.auth?.loggedIn() || validRefreshToken) {
198+
playlists.push({
199+
playlist_id: 'spotify-playlist:saved-tracks',
200+
playlist_name: 'Liked Songs',
201+
playlist_coverPath: 'https://t.scdn.co/images/3099b3803ad9496896c43f22fe9be8c4.png',
202+
isRemote: true
203+
})
197204
while (hasNext) {
198205
const resp = await this.populateRequest(
199206
ApiResources.PLAYLISTS,
@@ -282,20 +289,35 @@ export class SpotifyProvider extends GenericAuth implements GenericProvider, Gen
282289
if (this.auth?.loggedIn() || validRefreshToken) {
283290
let nextOffset = 0
284291
let isNext = false
285-
const limit = 100
292+
const limit = id === 'saved-tracks' ? 50 : 100
286293
const parsed: Song[] = []
287294
do {
288-
const resp = await this.populateRequest(
289-
ApiResources.PLAYLIST_ITEMS,
290-
{
291-
params: {
292-
playlist_id: id,
293-
limit,
294-
offset: nextOffset
295-
}
296-
},
297-
invalidateCache
298-
)
295+
let resp: SpotifyResponses.PlaylistItems.PlaylistItems
296+
297+
if (id === 'saved-tracks') {
298+
resp = await this.populateRequest(
299+
ApiResources.LIKED_SONGS,
300+
{
301+
params: {
302+
limit,
303+
offset: nextOffset
304+
}
305+
},
306+
invalidateCache
307+
)
308+
} else {
309+
resp = await this.populateRequest(
310+
ApiResources.PLAYLIST_ITEMS,
311+
{
312+
params: {
313+
playlist_id: id,
314+
limit,
315+
offset: nextOffset
316+
}
317+
},
318+
invalidateCache
319+
)
320+
}
299321
const items = await this.parsePlaylistItems(resp.items)
300322
parsed.push(...items)
301323
isNext = !!resp.next

0 commit comments

Comments
 (0)