@kud/qobuz-cli 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +244 -35
  2. package/package.json +5 -3
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/index.ts
4
- import { defineCommand as defineCommand14, runMain } from "citty";
4
+ import { defineCommand as defineCommand16, runMain } from "citty";
5
5
 
6
6
  // src/help.ts
7
7
  import { renderUsage } from "citty";
@@ -135,9 +135,173 @@ ${bold2(`\u{1F3A4} ${artist2.name}`)}`);
135
135
  }
136
136
  });
137
137
 
138
- // src/commands/fav.ts
138
+ // src/commands/convert.ts
139
139
  import { defineCommand as defineCommand3 } from "citty";
140
- var list = defineCommand3({
140
+
141
+ // src/resolve.ts
142
+ var CRAWLER_UA = "facebookexternalhit/1.1";
143
+ var spotifySearchUrl = (query) => `https://open.spotify.com/search/${encodeURIComponent(query)}`;
144
+ var ytMusicSearchUrl = (query) => `https://music.youtube.com/search?q=${encodeURIComponent(query)}`;
145
+ var deezerByIsrc = async (isrc) => {
146
+ const res = await fetch(`https://api.deezer.com/track/isrc:${isrc}`).catch(
147
+ () => null
148
+ );
149
+ if (!res || !res.ok) return void 0;
150
+ const track2 = await res.json();
151
+ return track2.link;
152
+ };
153
+ var isQobuz = (host) => host.endsWith("qobuz.com");
154
+ var isSpotify = (host) => host.endsWith("open.spotify.com");
155
+ var isYouTube = (host) => host.endsWith("music.youtube.com") || host.endsWith("youtube.com") || host.endsWith("youtu.be");
156
+ var metaContent = (html, property) => html.match(
157
+ new RegExp(`<meta[^>]+property="${property}"[^>]+content="([^"]*)"`, "i")
158
+ )?.[1];
159
+ var resolveSpotify = async (url2) => {
160
+ const res = await fetch(url2, { headers: { "User-Agent": CRAWLER_UA } });
161
+ if (!res.ok) return null;
162
+ const html = await res.text();
163
+ const title = metaContent(html, "og:title");
164
+ const artist2 = metaContent(html, "og:description")?.split(" \xB7 ")[0];
165
+ if (title && artist2)
166
+ return { title: decodeEntities(title), artist: decodeEntities(artist2) };
167
+ const match = html.match(/<title>(.*?)<\/title>/i)?.[1]?.match(/^(.+?) - song(?: and lyrics)? by (.+?) \| Spotify/i);
168
+ if (!match?.[1] || !match[2]) return null;
169
+ return { title: decodeEntities(match[1]), artist: decodeEntities(match[2]) };
170
+ };
171
+ var resolveYouTube = async (url2) => {
172
+ const endpoint = `https://www.youtube.com/oembed?url=${encodeURIComponent(url2)}&format=json`;
173
+ const res = await fetch(endpoint);
174
+ if (!res.ok) return null;
175
+ const data = await res.json();
176
+ if (!data.title || !data.author_name) return null;
177
+ return {
178
+ title: data.title,
179
+ artist: data.author_name.replace(/ - Topic$/, "")
180
+ };
181
+ };
182
+ var resolveLink = async (input) => {
183
+ let url2;
184
+ try {
185
+ url2 = new URL(input);
186
+ } catch {
187
+ return { ok: false, reason: "invalid" };
188
+ }
189
+ const host = url2.hostname;
190
+ const ok = (track2) => track2 ? { ok: true, direction: "to-qobuz", track: track2 } : { ok: false, reason: "unknown" };
191
+ if (isQobuz(host)) {
192
+ const match = url2.pathname.match(/\/track\/(\d+)/);
193
+ return match ? { ok: true, direction: "from-qobuz", qobuzTrackId: Number(match[1]) } : { ok: false, reason: "unsupported-type" };
194
+ }
195
+ if (isSpotify(host))
196
+ return url2.pathname.includes("/track/") ? ok(await resolveSpotify(input)) : { ok: false, reason: "unsupported-type" };
197
+ if (isYouTube(host))
198
+ return url2.pathname === "/watch" || host.endsWith("youtu.be") ? ok(await resolveYouTube(input)) : { ok: false, reason: "unsupported-type" };
199
+ return { ok: false, reason: "unknown" };
200
+ };
201
+ var deezerSearch = async (query) => {
202
+ const res = await fetch(
203
+ `https://api.deezer.com/search?q=${encodeURIComponent(query)}&limit=1`
204
+ );
205
+ if (!res.ok) return void 0;
206
+ const data = await res.json();
207
+ return data.data?.[0]?.id;
208
+ };
209
+ var findIsrc = async ({
210
+ title,
211
+ artist: artist2
212
+ }) => {
213
+ const id = await deezerSearch(`artist:"${artist2}" track:"${title}"`) ?? await deezerSearch(`${artist2} ${title}`);
214
+ if (id === void 0) return void 0;
215
+ const res = await fetch(`https://api.deezer.com/track/${id}`);
216
+ if (!res.ok) return void 0;
217
+ const track2 = await res.json();
218
+ return track2.isrc;
219
+ };
220
+ var normalize = (text) => text.toLowerCase().normalize("NFD").replace(new RegExp("\\p{Diacritic}", "gu"), "").replace(/\([^)]*\)|\[[^\]]*\]/g, " ").replace(/\b(feat|ft|with)\b\.?/g, " ").replace(/[^a-z0-9]+/g, " ").trim();
221
+ var tokenSet = (text) => new Set(normalize(text).split(" ").filter(Boolean));
222
+ var overlap = (a, b) => {
223
+ if (a.size === 0 || b.size === 0) return 0;
224
+ let hits = 0;
225
+ for (const token of a) if (b.has(token)) hits += 1;
226
+ return hits / Math.min(a.size, b.size);
227
+ };
228
+ var isLikelyMatch = (resolved, candidate) => {
229
+ const titleScore = overlap(
230
+ tokenSet(resolved.title),
231
+ tokenSet(candidate.title)
232
+ );
233
+ const artistScore = overlap(
234
+ tokenSet(resolved.artist),
235
+ tokenSet(candidate.artist?.name ?? "")
236
+ );
237
+ return titleScore >= 0.6 && artistScore > 0;
238
+ };
239
+ var decodeEntities = (text) => text.replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&quot;/g, '"').replace(/&#39;/g, "'").replace(/&#x27;/g, "'");
240
+
241
+ // src/commands/convert.ts
242
+ var FAILURE = {
243
+ invalid: "Not a URL. Pass a Spotify, YouTube Music, or Qobuz track link.",
244
+ qobuz: "Unsupported Qobuz link \u2014 pass a track link.",
245
+ "unsupported-type": "Only track links are supported (not albums, playlists, or podcasts).",
246
+ unknown: "Unsupported link \u2014 Spotify, YouTube Music, or Qobuz only."
247
+ };
248
+ var convert = defineCommand3({
249
+ meta: {
250
+ name: "convert",
251
+ description: "Convert a Spotify / YouTube Music / Qobuz track link to the other services"
252
+ },
253
+ args: {
254
+ url: { type: "positional", description: "Track link", required: true }
255
+ },
256
+ run: async ({ args: args2 }) => {
257
+ const client = await connect();
258
+ const outcome = await resolveLink(args2.url);
259
+ if (!outcome.ok) {
260
+ console.error(FAILURE[outcome.reason]);
261
+ process.exit(1);
262
+ }
263
+ if (outcome.direction === "from-qobuz") {
264
+ const track3 = await client.tracks.get(outcome.qobuzTrackId);
265
+ const query2 = `${track3.artist?.name ?? ""} ${track3.title}`.trim();
266
+ const deezer = track3.isrc ? await deezerByIsrc(track3.isrc) : void 0;
267
+ console.log(`
268
+ ${bold2(`\u{1F3B5} ${track3.title}`)}`);
269
+ console.log(` ${dim(`by ${track3.artist?.name ?? "?"}`)}`);
270
+ heading("Find elsewhere");
271
+ console.log(` ${dim("YouTube Music")} ${link(ytMusicSearchUrl(query2))}`);
272
+ console.log(` ${dim("Spotify ")} ${link(spotifySearchUrl(query2))}`);
273
+ if (deezer)
274
+ console.log(
275
+ ` ${dim("Deezer ")} ${link(deezer)} ${good("exact")}`
276
+ );
277
+ return;
278
+ }
279
+ const resolved = outcome.track;
280
+ const query = `${resolved.artist} ${resolved.title}`;
281
+ const isrc = await findIsrc(resolved);
282
+ const exact = isrc ? await client.tracks.match({ isrc, query }) : void 0;
283
+ const track2 = exact ?? (await client.search.search(query, { limit: 5 })).tracks.find(
284
+ (t) => isLikelyMatch(resolved, t)
285
+ );
286
+ console.log(`
287
+ ${dim(`From ${resolved.artist} \u2014 ${resolved.title}`)}`);
288
+ if (!track2) {
289
+ console.error(`
290
+ No Qobuz match found.`);
291
+ process.exit(1);
292
+ }
293
+ console.log(`${bold2(`\u{1F3B5} ${track2.title}`)}`);
294
+ console.log(` ${dim(`by ${track2.artist?.name ?? "?"}`)}`);
295
+ console.log(
296
+ ` ${exact ? good("exact match (ISRC)") : muted("approximate match")}`
297
+ );
298
+ console.log(` ${link(client.deepLink.track(track2.id))}`);
299
+ }
300
+ });
301
+
302
+ // src/commands/fav.ts
303
+ import { defineCommand as defineCommand4 } from "citty";
304
+ var list = defineCommand4({
141
305
  meta: { name: "list", description: "List favourites" },
142
306
  args: {
143
307
  type: {
@@ -186,7 +350,7 @@ var list = defineCommand3({
186
350
  }
187
351
  }
188
352
  });
189
- var add = defineCommand3({
353
+ var add = defineCommand4({
190
354
  meta: { name: "add", description: "Add a favourite" },
191
355
  args: {
192
356
  type: {
@@ -206,7 +370,7 @@ var add = defineCommand3({
206
370
  success(`added ${args2.type} ${args2.id} to favourites`);
207
371
  }
208
372
  });
209
- var remove = defineCommand3({
373
+ var remove = defineCommand4({
210
374
  meta: { name: "remove", description: "Remove a favourite" },
211
375
  args: {
212
376
  type: {
@@ -226,13 +390,13 @@ var remove = defineCommand3({
226
390
  success(`removed ${args2.type} ${args2.id} from favourites`);
227
391
  }
228
392
  });
229
- var fav = defineCommand3({
393
+ var fav = defineCommand4({
230
394
  meta: { name: "fav", description: "Manage favourites" },
231
395
  subCommands: { list, add, remove }
232
396
  });
233
397
 
234
398
  // src/commands/login.ts
235
- import { defineCommand as defineCommand4 } from "citty";
399
+ import { defineCommand as defineCommand5 } from "citty";
236
400
 
237
401
  // src/connect.ts
238
402
  import { fetchAppId, validateCredentials } from "@kud/qobuz";
@@ -330,7 +494,7 @@ var runConnect = async () => {
330
494
  };
331
495
 
332
496
  // src/commands/login.ts
333
- var login = defineCommand4({
497
+ var login = defineCommand5({
334
498
  meta: {
335
499
  name: "login",
336
500
  description: "Connect your Qobuz account (stores a token in the Keychain)"
@@ -339,8 +503,8 @@ var login = defineCommand4({
339
503
  });
340
504
 
341
505
  // src/commands/logout.ts
342
- import { defineCommand as defineCommand5 } from "citty";
343
- var logout = defineCommand5({
506
+ import { defineCommand as defineCommand6 } from "citty";
507
+ var logout = defineCommand6({
344
508
  meta: {
345
509
  name: "logout",
346
510
  description: "Remove the stored Qobuz session from the Keychain"
@@ -351,9 +515,33 @@ var logout = defineCommand5({
351
515
  }
352
516
  });
353
517
 
518
+ // src/commands/now-playing.ts
519
+ import { defineCommand as defineCommand7 } from "citty";
520
+ var nowPlaying = defineCommand7({
521
+ meta: {
522
+ name: "now-playing",
523
+ description: "Show the track currently playing in the Qobuz desktop app"
524
+ },
525
+ run: async () => {
526
+ const client = await connect();
527
+ const track2 = await client.nowPlaying();
528
+ if (!track2) {
529
+ console.error("Nothing playing in Qobuz.");
530
+ process.exit(1);
531
+ }
532
+ console.log(`
533
+ ${bold2(`\u{1F3B5} ${track2.title}`)}`);
534
+ console.log(` ${dim(`by ${track2.artist?.name ?? "?"}`)}`);
535
+ if (track2.album?.title) console.log(` ${dim(track2.album.title)}`);
536
+ const duration = formatDuration(track2.duration);
537
+ if (duration) console.log(` ${dim(duration)}`);
538
+ console.log(` ${link(client.deepLink.track(track2.id))}`);
539
+ }
540
+ });
541
+
354
542
  // src/commands/open.ts
355
- import { defineCommand as defineCommand6 } from "citty";
356
- var open = defineCommand6({
543
+ import { defineCommand as defineCommand8 } from "citty";
544
+ var open = defineCommand8({
357
545
  meta: {
358
546
  name: "open",
359
547
  description: "Open an album/track/playlist/artist in the Qobuz app"
@@ -379,15 +567,15 @@ var open = defineCommand6({
379
567
  });
380
568
 
381
569
  // src/commands/playback.ts
382
- import { defineCommand as defineCommand7 } from "citty";
383
- import { sendMediaKey } from "@kud/macos-media-keys";
384
- var playbackCommand = (name, description, key) => defineCommand7({
570
+ import { defineCommand as defineCommand9 } from "citty";
571
+ var playbackCommand = (name, description, key) => defineCommand9({
385
572
  meta: { name, description },
386
573
  run: async () => {
387
574
  try {
575
+ const { sendMediaKey } = await import("@kud/macos-media-keys");
388
576
  await sendMediaKey(key);
389
577
  } catch (error) {
390
- console.error(error.message);
578
+ console.error(error.message ?? `playback (${key}) failed`);
391
579
  process.exit(1);
392
580
  }
393
581
  }
@@ -415,8 +603,8 @@ var rewind = playbackCommand(
415
603
  );
416
604
 
417
605
  // src/commands/playlist.ts
418
- import { defineCommand as defineCommand8 } from "citty";
419
- var list2 = defineCommand8({
606
+ import { defineCommand as defineCommand10 } from "citty";
607
+ var list2 = defineCommand10({
420
608
  meta: { name: "list", description: "List your playlists" },
421
609
  run: async () => {
422
610
  const client = await connect();
@@ -434,7 +622,7 @@ var list2 = defineCommand8({
434
622
  );
435
623
  }
436
624
  });
437
- var show = defineCommand8({
625
+ var show = defineCommand10({
438
626
  meta: { name: "show", description: "Show a playlist" },
439
627
  args: {
440
628
  id: { type: "positional", description: "Playlist ID", required: true }
@@ -449,7 +637,7 @@ ${bold2(`\u{1F4DD} ${playlist2.name}`)}`);
449
637
  console.log(` ${link(client.deepLink.playlist(playlist2.id))}`);
450
638
  }
451
639
  });
452
- var create = defineCommand8({
640
+ var create = defineCommand10({
453
641
  meta: { name: "create", description: "Create a playlist" },
454
642
  args: {
455
643
  name: { type: "positional", description: "Playlist name", required: true },
@@ -468,7 +656,7 @@ var create = defineCommand8({
468
656
  success(`created playlist "${playlist2.name}" [${playlist2.id}]`);
469
657
  }
470
658
  });
471
- var add2 = defineCommand8({
659
+ var add2 = defineCommand10({
472
660
  meta: { name: "add", description: "Add a track to a playlist" },
473
661
  args: {
474
662
  playlist: {
@@ -486,7 +674,25 @@ var add2 = defineCommand8({
486
674
  success(`added track ${args2.track} to playlist ${args2.playlist}`);
487
675
  }
488
676
  });
489
- var remove2 = defineCommand8({
677
+ var edit = defineCommand10({
678
+ meta: { name: "edit", description: "Edit a playlist's description" },
679
+ args: {
680
+ id: { type: "positional", description: "Playlist ID", required: true },
681
+ description: {
682
+ type: "positional",
683
+ description: "New description",
684
+ required: true
685
+ }
686
+ },
687
+ run: async ({ args: args2 }) => {
688
+ const client = await connect();
689
+ await client.playlists.update(Number(args2.id), {
690
+ description: args2.description
691
+ });
692
+ success(`updated description for playlist ${args2.id}`);
693
+ }
694
+ });
695
+ var remove2 = defineCommand10({
490
696
  meta: { name: "remove", description: "Delete a playlist" },
491
697
  args: {
492
698
  id: { type: "positional", description: "Playlist ID", required: true }
@@ -497,14 +703,14 @@ var remove2 = defineCommand8({
497
703
  success(`deleted playlist ${args2.id}`);
498
704
  }
499
705
  });
500
- var playlist = defineCommand8({
706
+ var playlist = defineCommand10({
501
707
  meta: { name: "playlist", description: "Manage playlists" },
502
- subCommands: { list: list2, show, create, add: add2, remove: remove2 }
708
+ subCommands: { list: list2, show, create, add: add2, edit, remove: remove2 }
503
709
  });
504
710
 
505
711
  // src/commands/search.ts
506
- import { defineCommand as defineCommand9 } from "citty";
507
- var search = defineCommand9({
712
+ import { defineCommand as defineCommand11 } from "citty";
713
+ var search = defineCommand11({
508
714
  meta: {
509
715
  name: "search",
510
716
  description: "Search Qobuz for albums, tracks, and artists"
@@ -563,8 +769,8 @@ var search = defineCommand9({
563
769
  });
564
770
 
565
771
  // src/commands/similar.ts
566
- import { defineCommand as defineCommand10 } from "citty";
567
- var similar = defineCommand10({
772
+ import { defineCommand as defineCommand12 } from "citty";
773
+ var similar = defineCommand12({
568
774
  meta: {
569
775
  name: "similar",
570
776
  description: "List artists similar to the given artist"
@@ -589,7 +795,7 @@ var similar = defineCommand10({
589
795
  });
590
796
 
591
797
  // src/commands/stats.ts
592
- import { defineCommand as defineCommand11 } from "citty";
798
+ import { defineCommand as defineCommand13 } from "citty";
593
799
  import { readLibraryStats } from "@kud/qobuz";
594
800
  var n = (value) => value.toLocaleString("en-US");
595
801
  var pct = (count, total) => total ? `${Math.round(1e3 * count / total) / 10}%` : "";
@@ -605,7 +811,7 @@ ${bold2(title)}`);
605
811
  console.log(suffix ? `${line} ${dim(suffix)}` : line);
606
812
  }
607
813
  };
608
- var stats = defineCommand11({
814
+ var stats = defineCommand13({
609
815
  meta: {
610
816
  name: "stats",
611
817
  description: "Show collection analytics from your Qobuz desktop library"
@@ -656,8 +862,8 @@ var stats = defineCommand11({
656
862
  });
657
863
 
658
864
  // src/commands/track.ts
659
- import { defineCommand as defineCommand12 } from "citty";
660
- var track = defineCommand12({
865
+ import { defineCommand as defineCommand14 } from "citty";
866
+ var track = defineCommand14({
661
867
  meta: { name: "track", description: "Show track details" },
662
868
  args: {
663
869
  id: { type: "positional", description: "Track ID", required: true }
@@ -680,7 +886,7 @@ ${bold2(`\u{1F3B5} ${track2.title}`)}`);
680
886
  });
681
887
 
682
888
  // src/commands/url.ts
683
- import { defineCommand as defineCommand13 } from "citty";
889
+ import { defineCommand as defineCommand15 } from "citty";
684
890
  import { readNowPlayingTrackId } from "@kud/qobuz";
685
891
  var currentTrackLink = async (client) => {
686
892
  const trackId = await readNowPlayingTrackId();
@@ -704,7 +910,7 @@ var specificLink = (client, type, id) => {
704
910
  }
705
911
  return link2;
706
912
  };
707
- var url = defineCommand13({
913
+ var url = defineCommand15({
708
914
  meta: {
709
915
  name: "url",
710
916
  description: "Copy a deep-link to the clipboard \u2014 the playing track, or a given item"
@@ -739,7 +945,7 @@ var url = defineCommand13({
739
945
  });
740
946
 
741
947
  // src/index.ts
742
- var main = defineCommand14({
948
+ var main = defineCommand16({
743
949
  meta: {
744
950
  name: "qobuz",
745
951
  description: "Qobuz from the command line \u2014 search, library, and quick-open in the app"
@@ -754,6 +960,8 @@ var main = defineCommand14({
754
960
  similar,
755
961
  fav,
756
962
  playlist,
963
+ convert,
964
+ "now-playing": nowPlaying,
757
965
  stats,
758
966
  url,
759
967
  open,
@@ -766,6 +974,7 @@ var main = defineCommand14({
766
974
  });
767
975
  var aliases = {
768
976
  "copy-url": "url",
977
+ np: "now-playing",
769
978
  prev: "previous",
770
979
  ff: "forward",
771
980
  rew: "rewind"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kud/qobuz-cli",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Command-line interface for Qobuz — search, library, and quick-open in the app",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,10 +23,12 @@
23
23
  "node": ">=20"
24
24
  },
25
25
  "dependencies": {
26
- "@kud/macos-media-keys": "0.1.0",
27
- "@kud/qobuz": "0.5.0",
26
+ "@kud/qobuz": "0.5.5",
28
27
  "citty": "0.2.2"
29
28
  },
29
+ "optionalDependencies": {
30
+ "@kud/macos-media-keys": "0.1.0"
31
+ },
30
32
  "devDependencies": {
31
33
  "@types/node": "26.0.0",
32
34
  "tsup": "8.5.1",