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