Skip to content

Commit

Permalink
feat: support spotify playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrickert committed May 23, 2022
1 parent 32cb323 commit 744fabd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/engines/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { stat } from "fs/promises";
import { PlayerEngine, TrackSource } from "../types/engines";
import { fileEngine } from "./file";
import { spotifyEngine } from "./spotify";
Expand All @@ -12,8 +13,17 @@ export const playerEngines: Record<TrackSource, PlayerEngine> = {
/**
* Automatically detect the query type
*/
export function detectTrackSource(query: string): TrackSource {
if (!query.startsWith("https://")) return "file";
export async function detectTrackSource(query: string): Promise<TrackSource> {
if (query.startsWith("https://open.spotify.com/")) return "spotify";
if (query.startsWith("https://www.youtube.com/")) return "youtube";

// check if query is file path
try {
const stats = await stat(query);
if (stats.isFile()) return "file";
} catch (e) {
// noop
}

return "youtube";
}
29 changes: 26 additions & 3 deletions src/engines/spotify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTracks, Tracks } from "spotify-url-info";
import { PlayerEngine, Track } from "../types/engines";
import { getData, getTracks, Tracks } from "spotify-url-info";
import { PlayerEngine, SearchResult, Track } from "../types/engines";
import { youtubeEngine } from "./youtube";

/**
Expand All @@ -8,7 +8,9 @@ import { youtubeEngine } from "./youtube";
*/
export const spotifyEngine: PlayerEngine = {
search: async (query, options) => {
// TODO: check how to get playlist info
const isPlaylist = query.startsWith("https://open.spotify.com/playlist");
if (isPlaylist) return await searchPlaylist(query);

const tracks = await getTracks(query);

return [
Expand All @@ -31,6 +33,27 @@ export const spotifyEngine: PlayerEngine = {
},
};

async function searchPlaylist(query: string): Promise<SearchResult[]> {
const data = await getData(query);
if (data?.type !== "playlist") return [];

console.log(data.tracks.items);

return [
{
tracks: data.tracks.items.map((i: { track: Tracks }) =>
mapSpotifyTrack(i.track)
),
playlist: {
title: data.name,
url: data.external_urls.spotify,
thumbnailUrl: data.images.length ? data.images[0].url : undefined,
},
source: "spotify",
},
];
}

function mapSpotifyTrack(track: Tracks): Track {
return {
title: track.name,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { detectTrackSource } from "./engines";
export * from "./player";
export * from "./player-manager";
export * from "./types/engines";
Expand Down
2 changes: 1 addition & 1 deletion src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class Player extends TypedEmitter<PlayerEvents> {
if (customResult) return customResult;
}

const trackSource = detectTrackSource(query);
const trackSource = await detectTrackSource(query);
const playerEngine = playerEngines[trackSource];
return await playerEngine.search(query, options);
}
Expand Down

0 comments on commit 744fabd

Please sign in to comment.