adding playlist... lastfm to play

This commit is contained in:
NoLogicAlan committed 2026-05-10 00:25:34 +03:00
1 parent 4bff159fb3
commit e2e39b52ad
3 files changed
+172 -39

No files matched your search

+6 -4
View File
@@ -41,7 +41,7 @@ function notLinked(prefix) {
} }
// Valid categories that can be played (without a sub-number) // Valid categories that can be played (without a sub-number)
const SIMPLE_CATEGORIES = ["loved", "top", "recent"]; const SIMPLE_CATEGORIES = ["loved", "top", "recent", "albums"];
/** /**
* Resolve a Last.fm category (loved/top/recent/playlist) into playable tracks. * Resolve a Last.fm category (loved/top/recent/playlist) into playable tracks.
@@ -68,7 +68,7 @@ export async function playLastFmCategory(ctx, msg, userId, category, options = {
if (!validCategories.includes(category)) { if (!validCategories.includes(category)) {
return msg.reply({ return msg.reply({
embeds: [new EmbedBuilder().setColor("#ff0000").setDescription( embeds: [new EmbedBuilder().setColor("#ff0000").setDescription(
`❌ Unknown category \`${category}\`. Use \`loved\`, \`top\`, \`recent\`, or \`playlist\`.` `❌ Unknown category \`${category}\`. Use \`loved\`, \`top\`, \`recent\`, \`albums\`, or \`playlist\`.`
)] )]
}); });
} }
@@ -91,8 +91,8 @@ export async function playLastFmCategory(ctx, msg, userId, category, options = {
if (!p) return; if (!p) return;
// Fetch tracks from Last.fm // Fetch tracks from Last.fm
const categoryEmoji = { loved: "❤️", top: "📊", recent: "🕐", playlist: "📋" }[category]; const categoryEmoji = { loved: "❤️", top: "📊", recent: "🕐", playlist: "📋", albums: "💿" }[category];
const categoryLabel = { loved: "Loved", top: "Top", recent: "Recent", playlist: "Playlist" }[category]; const categoryLabel = { loved: "Loved", top: "Top", recent: "Recent", playlist: "Playlist", albums: "Top Albums" }[category];
let statusMsg; let statusMsg;
try { try {
@@ -466,6 +466,7 @@ export async function run(msg, data) {
`\`loved\` — Play your loved tracks`, `\`loved\` — Play your loved tracks`,
`\`top\` — Play your top tracks`, `\`top\` — Play your top tracks`,
`\`recent\` — Play your recent tracks`, `\`recent\` — Play your recent tracks`,
`\`albums\` — Play your top albums`,
`\`playlist <number>\` — Play a playlist (use \`${prefix}lastfm playlists\` to list)`, `\`playlist <number>\` — Play a playlist (use \`${prefix}lastfm playlists\` to list)`,
``, ``,
`**Examples:**`, `**Examples:**`,
@@ -570,6 +571,7 @@ export async function run(msg, data) {
`\`${prefix}lastfm play loved\` — Play your loved tracks`, `\`${prefix}lastfm play loved\` — Play your loved tracks`,
`\`${prefix}lastfm play top\` — Play your top tracks`, `\`${prefix}lastfm play top\` — Play your top tracks`,
`\`${prefix}lastfm play recent\` — Play your recent tracks`, `\`${prefix}lastfm play recent\` — Play your recent tracks`,
`\`${prefix}lastfm play albums\` — Play your top albums`,
`\`${prefix}lastfm play playlist 1\` — Play a playlist`, `\`${prefix}lastfm play playlist 1\` — Play a playlist`,
``, ``,
`💡 Or use inline: \`${prefix}play lastfm:loved\``, `💡 Or use inline: \`${prefix}play lastfm:loved\``,
+1 -1
View File
@@ -5,7 +5,7 @@ import { PROVIDER_CHOICES } from "../src/constants/providers.mjs";
import { playLastFmCategory } from "./lastfm.mjs"; import { playLastFmCategory } from "./lastfm.mjs";
// Last.fm provider keywords that trigger special play logic // Last.fm provider keywords that trigger special play logic
const LASTFM_PLAY_CATEGORIES = ["loved", "top", "recent"]; const LASTFM_PLAY_CATEGORIES = ["loved", "top", "recent", "albums"];
function parseInlineProvider(raw) { function parseInlineProvider(raw) {
const match = raw.match(/^([a-z]+):\s*(.+)$/i); const match = raw.match(/^([a-z]+):\s*(.+)$/i);
+165 -34
View File
@@ -407,41 +407,69 @@ export class LastFmManager {
return data.user; return data.user;
} }
// ── Playlists ──────────────────────────────────────────────────────────────── // ── Playlists & Albums ────────────────────────────────────────────────────────
/** /**
* Get a list of the user's Last.fm playlists. * Get a list of the user's Last.fm playlists by scraping their profile page.
* The Last.fm API removed user.getplaylists, so we fetch the HTML page.
*
* @param {string} userId * @param {string} userId
* @returns {Promise<Array<{ id: string, title: string, trackCount: number, url: string, created: string }>>} * @returns {Promise<Array<{ title: string, trackCount: number, url: string }>>}
*/ */
async getPlaylists(userId) { async getPlaylists(userId) {
const user = await this.getUser(userId); const user = await this.getUser(userId);
if (!user) throw new Error("NOT_LINKED"); if (!user) throw new Error("NOT_LINKED");
const data = await apiCall( const profileUrl = `https://www.last.fm/user/${encodeURIComponent(user.username)}/playlists`;
{ const res = await fetch(profileUrl, {
method: "user.getplaylists", headers: { "User-Agent": "RemixBot/1.0 (Last.fm Integration)" },
api_key: this.apiKey, });
user: user.username,
},
this.apiSecret
);
return (data.playlists?.playlist ?? []).map(p => ({ if (!res.ok) {
id: String(p.id), throw new Error(`Failed to fetch profile page (HTTP ${res.status})`);
title: p.title ?? "Untitled", }
trackCount: +p.size ?? 0,
url: p.url ?? "", const html = await res.text();
created: p.date ?? "", const playlists = [];
}));
// Parse playlist links from the HTML
// Last.fm playlist links look like: /user/USERNAME/playlists/123456
// Each playlist item has a .playlist-item or similar container
const playlistRegex = /href="\/user\/[^/]+\/playlists\/(\d+)"[^>]*>([^<]+)<\/a>/gi;
let match;
while ((match = playlistRegex.exec(html)) !== null) {
const id = match[1];
const title = match[2].trim();
if (title && id) {
playlists.push({
id,
title,
url: `https://www.last.fm/user/${user.username}/playlists/${id}`,
});
}
}
// Try to extract track counts (e.g. "12 tracks" or "1 track")
const countRegex = /(\d+)\s+track/gi;
const counts = [];
let cMatch;
while ((cMatch = countRegex.exec(html)) !== null) {
counts.push(+cMatch[1]);
}
// Pair counts with playlists (order should match)
playlists.forEach((pl, i) => {
pl.trackCount = counts[i] ?? 0;
});
return playlists;
} }
/** /**
* Fetch tracks from a specific Last.fm playlist. * Fetch tracks from a specific Last.fm playlist.
* Uses the playlist.fetch API method with the playlist URL. * Since the API removed playlist.fetch, we scrape the playlist page.
* *
* @param {string} userId * @param {string} userId
* @param {number|string} playlistId - Playlist number (1-based index from getPlaylists) or the playlist URL * @param {number|string} playlistId - Playlist number (1-based from getPlaylists) or full Last.fm playlist URL
* @param {number} [limit=50] - Max tracks to return * @param {number} [limit=50] - Max tracks to return
* @returns {Promise<Array<{ artist, name, url, image }>>} * @returns {Promise<Array<{ artist, name, url, image }>>}
*/ */
@@ -459,28 +487,116 @@ export class LastFmManager {
throw new Error(`Playlist #${playlistId} not found. You have ${playlists.length} playlist(s). Use \`%lastfm playlists\` to see them.`); throw new Error(`Playlist #${playlistId} not found. You have ${playlists.length} playlist(s). Use \`%lastfm playlists\` to see them.`);
} }
playlistUrl = playlists[idx].url; playlistUrl = playlists[idx].url;
} else { } else if (String(playlistId).startsWith("http")) {
// Assume it's a URL
playlistUrl = String(playlistId); playlistUrl = String(playlistId);
} else {
// Assume it's a playlist ID slug — build the URL
playlistUrl = `https://www.last.fm/user/${user.username}/playlists/${playlistId}`;
} }
// Scrape the playlist page for track info
const res = await fetch(playlistUrl, {
headers: { "User-Agent": "RemixBot/1.0 (Last.fm Integration)" },
});
if (!res.ok) {
throw new Error(`Failed to fetch playlist page (HTTP ${res.status})`);
}
const html = await res.text();
const tracks = [];
// Last.fm playlist pages contain track items with:
// <a href="/music/Artist/Track" class="...">Track Name</a>
// We extract artist and track from the URL pattern /music/Artist+Name/Track+Name
const trackLinkRegex = /href="\/music\/([^"]+?)"[^>]*class="[^"]*(?:link-block-target|chartlist-name)[^"]*"[^>]*>([^<]+)<\/a>/gi;
let tMatch;
while ((tMatch = trackLinkRegex.exec(html)) !== null && tracks.length < limit) {
const urlPath = decodeURIComponent(tMatch[1]);
const name = tMatch[2].trim();
// URL format: /music/Artist+Name/Track+Name or /music/Artist+Name/_/Track+Name
const parts = urlPath.split("/");
let artist = "Unknown";
if (parts.length >= 1) {
artist = parts[0].replace(/\+/g, " ");
}
if (parts.length >= 3 && parts[1] === "_") {
// /music/Artist/_/Track format
} else if (parts.length >= 2) {
// /music/Artist/Track format — the track name from URL
}
if (name && name !== "Unknown") {
tracks.push({
artist,
name,
url: `https://www.last.fm/music/${urlPath}`,
image: "",
});
}
}
// Fallback: try a broader regex if the first one didn't find anything
if (!tracks.length) {
const broadRegex = /href="\/music\/([^"]+)"[^>]*>([^<]{2,80})<\/a>/gi;
const seen = new Set();
let bMatch;
while ((bMatch = broadRegex.exec(html)) !== null && tracks.length < limit) {
const urlPath = decodeURIComponent(bMatch[1]);
const name = bMatch[2].trim();
// Only include actual track links (Artist/Track or Artist/_/Track)
const parts = urlPath.split("/");
if (parts.length < 2) continue;
if (seen.has(urlPath)) continue;
seen.add(urlPath);
const artist = parts[0].replace(/\+/g, " ");
const trackName = parts.length >= 3 && parts[1] === "_"
? parts[2].replace(/\+/g, " ")
: parts[1].replace(/\+/g, " ");
if (trackName && artist) {
tracks.push({
artist,
name: trackName,
url: `https://www.last.fm/music/${urlPath}`,
image: "",
});
}
}
}
return tracks;
}
/**
* Get the user's top albums (for %lastfm play albums).
* @param {string} userId
* @param {string} [period="overall"] - overall | 7day | 1month | 3month | 6month | 12month
* @param {number} [limit=20]
* @returns {Promise<Array<{ artist, name, url, playcount, image }>>}
*/
async getTopAlbums(userId, period = "overall", limit = 20) {
const user = await this.getUser(userId);
if (!user) throw new Error("NOT_LINKED");
const data = await apiCall( const data = await apiCall(
{ {
method: "playlist.fetch", method: "user.gettopalbums",
api_key: this.apiKey, api_key: this.apiKey,
playlistURL: playlistUrl, user: user.username,
period,
limit,
}, },
this.apiSecret this.apiSecret
); );
const trackList = data.playlist?.trackList?.track ?? data.playlist?.tracks?.track ?? []; return (data.topalbums?.album ?? []).map(a => ({
const tracks = (Array.isArray(trackList) ? trackList : [trackList]).slice(0, limit); artist: a.artist?.name ?? "Unknown",
name: a.name,
return tracks.map(t => ({ url: a.url ?? "",
artist: t.artist?.name ?? t.artist?.["#text"] ?? t.creator ?? "Unknown", playcount: a.playcount ?? 0,
name: t.title ?? t.name ?? "Unknown", image: a.image?.[2]?.["#text"] ?? a.image?.[1]?.["#text"] ?? "",
url: t.url ?? "",
image: t.image?.[2]?.["#text"] ?? t.image?.[1]?.["#text"] ?? "",
})); }));
} }
@@ -491,7 +607,7 @@ export class LastFmManager {
* that can be resolved by the player's worker (YouTube Music search). * that can be resolved by the player's worker (YouTube Music search).
* *
* @param {string} userId * @param {string} userId
* @param {"loved"|"top"|"recent"|"playlist"} category * @param {"loved"|"top"|"recent"|"playlist"|"albums"} category
* @param {object} [options] * @param {object} [options]
* @param {string} [options.period="overall"] - Period for top tracks (overall|7day|1month|3month|6month|12month) * @param {string} [options.period="overall"] - Period for top tracks (overall|7day|1month|3month|6month|12month)
* @param {number} [options.limit=20] - Max tracks to return * @param {number} [options.limit=20] - Max tracks to return
@@ -521,8 +637,23 @@ export class LastFmManager {
if (!options.playlistId) throw new Error("Playlist ID required. Use `%lastfm playlists` to see your playlists, then `%lastfm play playlist <number>`."); if (!options.playlistId) throw new Error("Playlist ID required. Use `%lastfm playlists` to see your playlists, then `%lastfm play playlist <number>`.");
tracks = await this.getPlaylistTracks(userId, options.playlistId, limit); tracks = await this.getPlaylistTracks(userId, options.playlistId, limit);
break; break;
case "albums":
// Top albums — we fetch albums, then for each album we use the album name + artist as search query
const albums = await this.getTopAlbums(userId, options.period ?? "overall", limit);
tracks = albums.map(a => ({
artist: a.artist,
name: a.name,
url: a.url,
// For albums, search the album name + artist to get the full album
query: `${a.artist} ${a.name} album`,
image: a.image ?? "",
}));
return {
username: user.username,
tracks,
};
default: default:
throw new Error(`Unknown Last.fm category: ${category}. Use loved, top, recent, or playlist.`); throw new Error(`Unknown Last.fm category: ${category}. Use loved, top, recent, playlist, or albums.`);
} }
return { return {