A decentralized music tracking and discovery platform built on AT Protocol 馃幍 rocksky.app
spotify atproto lastfm musicbrainz scrobbling listenbrainz
at feat/pgpull 53 lines 1.6 kB view raw
1import { RockskyClient } from "client"; 2import fs from "fs/promises"; 3import os from "os"; 4import path from "path"; 5 6export async function nowplaying(did?: string): Promise<string> { 7 const tokenPath = path.join(os.homedir(), ".rocksky", "token.json"); 8 try { 9 await fs.access(tokenPath); 10 } catch (err) { 11 if (!did) { 12 return "You are not logged in. Please run `rocksky login <username>.bsky.social` first."; 13 } 14 } 15 16 const tokenData = await fs.readFile(tokenPath, "utf-8"); 17 const { token } = JSON.parse(tokenData); 18 if (!token && !did) { 19 return "You are not logged in. Please run `rocksky login <username>.bsky.social` first."; 20 } 21 22 const client = new RockskyClient(token); 23 try { 24 const nowPlaying = await client.getSpotifyNowPlaying(did); 25 if (!nowPlaying || Object.keys(nowPlaying).length === 0) { 26 const nowPlaying = await client.getNowPlaying(did); 27 if (!nowPlaying || Object.keys(nowPlaying).length === 0) { 28 return "No track is currently playing."; 29 } 30 return JSON.stringify( 31 { 32 title: nowPlaying.title, 33 artist: nowPlaying.artist, 34 album: nowPlaying.album, 35 }, 36 null, 37 2 38 ); 39 } 40 41 return JSON.stringify( 42 { 43 title: nowPlaying.item.name, 44 artist: nowPlaying.item.artists.map((a) => a.name).join(", "), 45 album: nowPlaying.item.album.name, 46 }, 47 null, 48 2 49 ); 50 } catch (err) { 51 return `Failed to fetch now playing data. Please check your token and try again, error: ${err.message}`; 52 } 53}