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";
3
4export async function search(
5 query: string,
6 { limit = 20, albums = false, artists = false, tracks = false, users = false }
7) {
8 const client = new RockskyClient();
9 const results = await client.search(query, { size: limit });
10 if (results.records.length === 0) {
11 console.log(`No results found for ${chalk.magenta(query)}.`);
12 return;
13 }
14
15 // merge all results into one array with type and sort by xata_scrore
16 let mergedResults = results.records.map((record) => ({
17 ...record,
18 type: record.table,
19 }));
20
21 if (albums) {
22 mergedResults = mergedResults.filter((record) => record.table === "albums");
23 }
24
25 if (artists) {
26 mergedResults = mergedResults.filter(
27 (record) => record.table === "artists"
28 );
29 }
30
31 if (tracks) {
32 mergedResults = mergedResults.filter(({ table }) => table === "tracks");
33 }
34
35 if (users) {
36 mergedResults = mergedResults.filter(({ table }) => table === "users");
37 }
38
39 mergedResults.sort((a, b) => b.xata_score - a.xata_score);
40
41 for (const { table, record } of mergedResults) {
42 if (table === "users") {
43 console.log(
44 `${chalk.bold.magenta(record.handle)} ${
45 record.display_name
46 } ${chalk.yellow(`https://rocksky.app/profile/${record.did}`)}`
47 );
48 }
49
50 if (table === "albums") {
51 const link = record.uri
52 ? `https://rocksky.app/${record.uri?.split("at://")[1]}`
53 : "";
54 console.log(
55 `${chalk.bold.magenta(record.title)} ${record.artist} ${chalk.yellow(
56 link
57 )}`
58 );
59 }
60
61 if (table === "tracks") {
62 const link = record.uri
63 ? `https://rocksky.app/${record.uri?.split("at://")[1]}`
64 : "";
65 console.log(
66 `${chalk.bold.magenta(record.title)} ${record.artist} ${chalk.yellow(
67 link
68 )}`
69 );
70 }
71 }
72}