Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(popupLyrics): add lrclib provider #3133

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Extensions/popupLyrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,62 @@ function PopupLyrics() {

return { lyrics };
},

async fetchLrclib(info) {
const baseURL = "https://lrclib.net/api/get";
const durr = info.duration / 1000;
const params = {
track_name: info.title,
artist_name: info.artist,
album_name: info.album,
duration: durr,
};

const finalURL = `${baseURL}?${Object.keys(params)
.map((key) => `${key}=${encodeURIComponent(params[key])}`)
.join("&")}`;

const body = await fetch(finalURL, {
headers: {
"x-user-agent": `spicetify v${Spicetify.Config.version} (https://github.com/spicetify/cli)`,
},
});

if (body.status !== 200) {
return { error: "Request error: Track wasn't found" };
}

const meta = await body.json();
if (meta?.instrumental) {
return { error: "Instrumental" };
}
if (!meta?.syncedLyrics) {
return { error: "No synced lyrics" };
}

// Preprocess lyrics by removing [tags] and empty lines
const lines = meta?.syncedLyrics
.replaceAll(/\[[a-zA-Z]+:.+\]/g, "")
.trim()
.split("\n");

const syncedTimestamp = /\[([0-9:.]+)\]/;
const isSynced = lines[0].match(syncedTimestamp);

const lyrics = lines.map((line) => {
const time = line.match(syncedTimestamp)?.[1];
const lyricContent = line.replace(syncedTimestamp, "").trim();
const lyric = lyricContent.replaceAll(/\<([0-9:.]+)\>/g, "").trim();
const [min, sec] = time.replace(/\[\]\<\>/, "").split(":");

if (line.trim() !== "" && isSynced && time) {
return { text: lyric || "♪", startTime: Number(min) * 60 + Number(sec) };
rxri marked this conversation as resolved.
Show resolved Hide resolved
}
return;
});

return { lyrics };
},
};

const userConfigs = {
Expand Down Expand Up @@ -276,6 +332,11 @@ function PopupLyrics() {
call: LyricProviders.fetchSpotify,
desc: "Lyrics sourced from official Spotify API.",
},
lrclib: {
on: boolLocalStorage("popup-lyrics:services:lrclib:on"),
call: LyricProviders.fetchLrclib,
desc: "Lyrics sourced from lrclib.net. Supports both synced and unsynced lyrics. LRCLIB is a free and open-source lyrics provider.",
},
},
servicesOrder: [],
};
Expand Down