forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1import chalk from "chalk";
2import { RockskyClient } from "client";
3import _ from "lodash";
4
5export async function search(
6 query: string,
7 {
8 limit = 20,
9 albums = false,
10 artists = false,
11 tracks = false,
12 users = false,
13 },
14) {
15 const client = new RockskyClient();
16 const results = await client.search(query, { size: limit });
17 if (results.hits.length === 0) {
18 console.log(`No results found for ${chalk.magenta(query)}.`);
19 return;
20 }
21
22 let mergedResults = results.hits.map((record) => ({
23 ...record,
24 type: _.get(record, "_federation.indexUid"),
25 }));
26
27 if (albums) {
28 mergedResults = mergedResults.filter((record) => record.type === "albums");
29 }
30
31 if (artists) {
32 mergedResults = mergedResults.filter((record) => record.type === "artists");
33 }
34
35 if (tracks) {
36 mergedResults = mergedResults.filter(({ type }) => type === "tracks");
37 }
38
39 if (users) {
40 mergedResults = mergedResults.filter(({ type }) => type === "users");
41 }
42
43 for (const { type, ...record } of mergedResults) {
44 if (type === "users") {
45 console.log(
46 `${chalk.bold.magenta(record.handle)} ${
47 record.displayName
48 } ${chalk.yellow(`https://rocksky.app/profile/${record.did}`)}`,
49 );
50 }
51
52 if (type === "albums") {
53 const link = record.uri
54 ? `https://rocksky.app/${record.uri?.split("at://")[1]?.replace("app.rocksky.", "")}`
55 : "";
56 console.log(
57 `${chalk.bold.magenta(record.title)} ${record.artist} ${chalk.yellow(
58 link,
59 )}`,
60 );
61 }
62
63 if (type === "tracks") {
64 const link = record.uri
65 ? `https://rocksky.app/${record.uri?.split("at://")[1]?.replace("app.rocksky.", "")}`
66 : "";
67 console.log(
68 `${chalk.bold.magenta(record.title)} ${record.artist} ${chalk.yellow(
69 link,
70 )}`,
71 );
72 }
73 }
74}