A decentralized music tracking and discovery platform built on AT Protocol 🎵 rocksky.app
spotify atproto lastfm musicbrainz scrobbling listenbrainz

[api] add sync library script

+24 -1
+24 -1
apps/api/src/scripts/sync-library.ts
··· 1 1 import chalk from "chalk"; 2 2 import { ctx } from "context"; 3 - import { and, count, eq } from "drizzle-orm"; 3 + import { and, count, eq, not } from "drizzle-orm"; 4 4 import tables from "schema"; 5 5 import { InsertArtistAlbum } from "schema/artist-albums"; 6 6 ··· 8 8 const total = await ctx.db 9 9 .select({ value: count() }) 10 10 .from(tables.tracks) 11 + .where( 12 + and( 13 + not(eq(tables.tracks.albumUri, null)), 14 + not(eq(tables.tracks.artistUri, null)) 15 + ) 16 + ) 11 17 .execute() 12 18 .then(([row]) => row.value); 13 19 20 + console.log(`Total tracks to process: ${chalk.magentaBright(total)}`); 21 + 14 22 for (let i = 0; i < total; i += size) { 15 23 const skip = i; 16 24 console.log( ··· 19 27 const results = await ctx.db 20 28 .select() 21 29 .from(tables.tracks) 30 + .where( 31 + and( 32 + not(eq(tables.tracks.albumUri, null)), 33 + not(eq(tables.tracks.artistUri, null)) 34 + ) 35 + ) 22 36 .limit(size) 23 37 .offset(skip) 24 38 .execute(); ··· 63 77 .execute() 64 78 .then((rows) => rows[0]), 65 79 ]); 80 + 81 + if (!artist || !album) { 82 + console.error( 83 + `Artist or album not found for track: ${track.uri}. Skipping...` 84 + ); 85 + console.log("artist", artist); 86 + console.log("album", album); 87 + continue; 88 + } 66 89 67 90 await ctx.db 68 91 .insert(tables.artistAlbums)