Something went wrong. Try again.
A decentralized music tracking and discovery platform built on AT Protocol ๐ต rocksky.app
spotify atproto lastfm musicbrainz scrobbling listenbrainz
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636import fs from "fs";import os from "os";import path from "path";
export const ROCKSKY_API_URL = "https://api.rocksky.app";
interface ActorRangeOpts { skip?: number; limit?: number; startDate?: string; endDate?: string;}
export class RockskyClient { constructor(private readonly token?: string) { this.token = token; }
async getCurrentUser() { const response = await fetch(`${ROCKSKY_API_URL}/profile`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, });
if (!response.ok) { throw new Error(`Failed to fetch user data: ${response.statusText}`); }
return response.json(); }
async getSpotifyNowPlaying(did?: string) { const response = await fetch( `${ROCKSKY_API_URL}/spotify/currently-playing` + (did ? `?did=${did}` : ""), { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, );
if (!response.ok) { throw new Error( `Failed to fetch now playing data: ${response.statusText}`, ); }
return response.json(); }
async getNowPlaying(did?: string) { const response = await fetch( `${ROCKSKY_API_URL}/now-playing` + (did ? `?did=${did}` : ""), { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, );
if (!response.ok) { throw new Error( `Failed to fetch now playing data: ${response.statusText}`, ); }
return response.json(); }
async scrobbles(did?: string, { skip = 0, limit = 20 } = {}) { if (did) { const response = await fetch( `${ROCKSKY_API_URL}/users/${did}/scrobbles?offset=${skip}&size=${limit}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error( `Failed to fetch scrobbles data: ${response.statusText}`, ); } return response.json(); }
const response = await fetch( `${ROCKSKY_API_URL}/public/scrobbles?offset=${skip}&size=${limit}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error(`Failed to fetch scrobbles data: ${response.statusText}`); }
return response.json(); }
async search(query: string, { size }) { const response = await fetch( `${ROCKSKY_API_URL}/xrpc/app.rocksky.feed.search?query=${query}&size=${size}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, );
if (!response.ok) { throw new Error(`Failed to fetch search data: ${response.statusText}`); }
return response.json(); }
async stats(did?: string) { if (!did) { const didFile = path.join(os.homedir(), ".rocksky", "did"); try { await fs.promises.access(didFile); did = await fs.promises.readFile(didFile, "utf-8"); } catch (err) { const user = await this.getCurrentUser(); did = user.did; const didPath = path.join(os.homedir(), ".rocksky"); fs.promises.mkdir(didPath, { recursive: true }); await fs.promises.writeFile(didFile, did); } }
const response = await fetch(`${ROCKSKY_API_URL}/users/${did}/stats`, { method: "GET", headers: { "Content-Type": "application/json", }, });
if (!response.ok) { throw new Error(`Failed to fetch stats data: ${response.statusText}`); }
return response.json(); }
async getArtists(did?: string, { skip = 0, limit = 20 } = {}) { if (!did) { const didFile = path.join(os.homedir(), ".rocksky", "did"); try { await fs.promises.access(didFile); did = await fs.promises.readFile(didFile, "utf-8"); } catch (err) { const user = await this.getCurrentUser(); did = user.did; const didPath = path.join(os.homedir(), ".rocksky"); fs.promises.mkdir(didPath, { recursive: true }); await fs.promises.writeFile(didFile, did); } }
const response = await fetch( `${ROCKSKY_API_URL}/users/${did}/artists?offset=${skip}&size=${limit}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error(`Failed to fetch artists data: ${response.statusText}`); } return response.json(); }
async getAlbums(did?: string, { skip = 0, limit = 20 } = {}) { if (!did) { const didFile = path.join(os.homedir(), ".rocksky", "did"); try { await fs.promises.access(didFile); did = await fs.promises.readFile(didFile, "utf-8"); } catch (err) { const user = await this.getCurrentUser(); did = user.did; const didPath = path.join(os.homedir(), ".rocksky"); fs.promises.mkdir(didPath, { recursive: true }); await fs.promises.writeFile(didFile, did); } }
const response = await fetch( `${ROCKSKY_API_URL}/users/${did}/albums?offset=${skip}&size=${limit}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error(`Failed to fetch albums data: ${response.statusText}`); } return response.json(); }
async getTracks(did?: string, { skip = 0, limit = 20 } = {}) { if (!did) { const didFile = path.join(os.homedir(), ".rocksky", "did"); try { await fs.promises.access(didFile); did = await fs.promises.readFile(didFile, "utf-8"); } catch (err) { const user = await this.getCurrentUser(); did = user.did; const didPath = path.join(os.homedir(), ".rocksky"); fs.promises.mkdir(didPath, { recursive: true }); await fs.promises.writeFile(didFile, did); } }
const response = await fetch( `${ROCKSKY_API_URL}/users/${did}/tracks?offset=${skip}&size=${limit}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error(`Failed to fetch tracks data: ${response.statusText}`); } return response.json(); }
async scrobble(api_key, api_sig, track, artist, timestamp) { const tokenPath = path.join(os.homedir(), ".rocksky", "token.json"); try { await fs.promises.access(tokenPath); } catch (err) { console.error( `You are not logged in. Please run the login command first.`, ); return; } const tokenData = await fs.promises.readFile(tokenPath, "utf-8"); const { token: sk } = JSON.parse(tokenData); const response = await fetch("https://audioscrobbler.rocksky.app/2.0", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: new URLSearchParams({ method: "track.scrobble", "track[0]": track, "artist[0]": artist, "timestamp[0]": timestamp || Math.floor(Date.now() / 1000), api_key, api_sig, sk, format: "json", }), });
if (!response.ok) { throw new Error( `Failed to scrobble track: ${ response.statusText } ${await response.text()}`, ); }
return response.json(); }
async getApiKeys() { const response = await fetch(`${ROCKSKY_API_URL}/apikeys`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, });
if (!response.ok) { throw new Error(`Failed to fetch API keys: ${response.statusText}`); }
return response.json(); }
async createApiKey(name: string, description?: string) { const response = await fetch(`${ROCKSKY_API_URL}/apikeys`, { method: "POST", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, body: JSON.stringify({ name, description, }), });
if (!response.ok) { throw new Error(`Failed to create API key: ${response.statusText}`); }
return response.json(); }
// Paginated list of the authenticated user's uploaded tracks. Each row is // `{ upload, track, ... }`; `upload.id` is what streams via `streamUrl`. // Optionally filtered to a single album (by `albumUri`, or `albumArtist` + // `albumName`). async getUploads({ skip = 0, limit = 50, albumUri, albumArtist, albumName, q, }: { skip?: number; limit?: number; albumUri?: string; albumArtist?: string; albumName?: string; q?: string; } = {}) { const params = new URLSearchParams({ offset: String(skip), size: String(limit), }); if (albumUri) params.set("albumUri", albumUri); if (albumArtist) params.set("albumArtist", albumArtist); if (albumName) params.set("albumName", albumName); if (q) params.set("q", q);
const response = await fetch(`${ROCKSKY_API_URL}/uploads?${params}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }); if (!response.ok) { throw new Error(`Failed to fetch uploads: ${response.statusText}`); } return response.json(); }
// Delete a single uploaded track (by its track id). async deleteUploadByTrack(trackId: string) { const response = await fetch( `${ROCKSKY_API_URL}/uploads/by-track/${trackId}`, { method: "DELETE", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, }, }, ); if (!response.ok) { throw new Error(`Failed to delete track: ${response.statusText}`); } return response.json().catch(() => ({})); }
// Delete all uploaded tracks of an album (by albumUri, or albumArtist+name). async deleteUploadAlbum(params: { albumUri?: string; albumArtist?: string; albumName?: string; }) { const qs = new URLSearchParams(); if (params.albumUri) qs.set("albumUri", params.albumUri); if (params.albumArtist) qs.set("albumArtist", params.albumArtist); if (params.albumName) qs.set("albumName", params.albumName); const response = await fetch(`${ROCKSKY_API_URL}/uploads/album?${qs}`, { method: "DELETE", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, }, }); if (!response.ok) { throw new Error(`Failed to delete album: ${response.statusText}`); } return response.json().catch(() => ({})); }
// Distinct albums the user has uploaded tracks for. async getUploadAlbums({ skip = 0, limit = 200 } = {}) { const response = await fetch( `${ROCKSKY_API_URL}/uploads/albums?offset=${skip}&size=${limit}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error(`Failed to fetch upload albums: ${response.statusText}`); } return response.json(); }
// Distinct album artists the user has uploaded tracks for. async getUploadArtists({ skip = 0, limit = 200 } = {}) { const response = await fetch( `${ROCKSKY_API_URL}/uploads/artists?offset=${skip}&size=${limit}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error(`Failed to fetch upload artists: ${response.statusText}`); } return response.json(); }
// Short-lived opaque token for use as `?token=` in stream URLs (the native // player can't set an Authorization header). Returns `{ token, expiresIn }`. async getStreamToken(): Promise<{ token: string; expiresIn: number }> { // On the critical path for starting playback โ time it out so a stalled // request can't hang play/resume forever. const response = await fetch(`${ROCKSKY_API_URL}/uploads/stream-token`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, signal: AbortSignal.timeout(10_000), }); if (!response.ok) { throw new Error(`Failed to get stream token: ${response.statusText}`); } return response.json(); }
// A range-capable audio URL the rockbox player can stream directly. streamUrl(uploadId: string, streamToken: string) { return `${ROCKSKY_API_URL}/uploads/${uploadId}/stream?token=${streamToken}`; }
// Submit a scrobble / now-playing update for the authenticated user. The // server applies its own dedup + abuse rules and publishes the AT record. async scrobbleNowPlaying(track: { title: string; artist: string; album: string; albumArtist: string; duration: number; albumArt?: string; timestamp?: number; }) { const response = await fetch(`${ROCKSKY_API_URL}/now-playing`, { method: "POST", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, body: JSON.stringify(track), }); if (!response.ok) { throw new Error( `Failed to scrobble: ${response.status} ${await response.text()}`, ); } return response.json(); }
// --- xrpc data endpoints -------------------------------------------------- private async xrpc(method: string, params: Record<string, string>) { const qs = new URLSearchParams(params); const response = await fetch( `${ROCKSKY_API_URL}/xrpc/${method}?${qs}`, { method: "GET", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, }, ); if (!response.ok) { throw new Error(`${method} failed: ${response.statusText}`); } return response.json(); }
// Global recent scrobbles (all users). async getGlobalScrobbles({ skip = 0, limit = 20 } = {}) { const data = await this.xrpc("app.rocksky.scrobble.getScrobbles", { offset: String(skip), limit: String(limit), }); return data.scrobbles || []; }
async getActorScrobbles(did: string, { skip = 0, limit = 20 } = {}) { const data = await this.xrpc("app.rocksky.actor.getActorScrobbles", { did, offset: String(skip), limit: String(limit), }); return data.scrobbles || []; }
async getActorSongs( did: string, { skip = 0, limit = 20, startDate, endDate }: ActorRangeOpts = {}, ) { const data = await this.xrpc("app.rocksky.actor.getActorSongs", { did, offset: String(skip), limit: String(limit), ...(startDate ? { startDate } : {}), ...(endDate ? { endDate } : {}), }); return data.tracks || []; }
// User listening stats via xrpc (the REST /users/:did/stats route 500s). async getStats(did: string) { return this.xrpc("app.rocksky.stats.getStats", { did }); }
// --- likes (xrpc) --------------------------------------------------------- private async xrpcPost(method: string, body: unknown) { const response = await fetch(`${ROCKSKY_API_URL}/xrpc/${method}`, { method: "POST", headers: { Authorization: this.token ? `Bearer ${this.token}` : undefined, "Content-Type": "application/json", }, body: JSON.stringify(body), }); if (!response.ok) { throw new Error(`${method} failed: ${response.status}`); } return response.json().catch(() => ({})); }
likeSong(uri: string) { return this.xrpcPost("app.rocksky.like.likeSong", { uri }); }
dislikeSong(uri: string) { return this.xrpcPost("app.rocksky.like.dislikeSong", { uri }); }
// The user's loved songs (each track carries its song `uri`). async getLovedSongs(did: string, { skip = 0, limit = 500 } = {}) { const data = await this.xrpc("app.rocksky.actor.getActorLovedSongs", { did, offset: String(skip), limit: String(limit), }); return data.tracks || []; }
async getActorArtists( did: string, { skip = 0, limit = 20, startDate, endDate }: ActorRangeOpts = {}, ) { const data = await this.xrpc("app.rocksky.actor.getActorArtists", { did, offset: String(skip), limit: String(limit), ...(startDate ? { startDate } : {}), ...(endDate ? { endDate } : {}), }); return data.artists || []; }
async getActorAlbums( did: string, { skip = 0, limit = 20, startDate, endDate }: ActorRangeOpts = {}, ) { const data = await this.xrpc("app.rocksky.actor.getActorAlbums", { did, offset: String(skip), limit: String(limit), ...(startDate ? { startDate } : {}), ...(endDate ? { endDate } : {}), }); return data.albums || []; }
async matchSong(title: string, artist: string, album?: string) { const q = new URLSearchParams({ title, artist, }); if (album) q.set("album", album); const response = await fetch( `${ROCKSKY_API_URL}/xrpc/app.rocksky.song.matchSong?${q.toString()}`, );
if (!response.ok) { throw new Error( `Failed to match song: ${response.statusText} ${await response.text()}`, ); }
return response.json(); }}