tangled
alpha
login
or
join now
rocksky.app
/
rocksky
98
fork
atom
A decentralized music tracking and discovery platform built on AT Protocol 🎵
rocksky.app
spotify
atproto
lastfm
musicbrainz
scrobbling
listenbrainz
98
fork
atom
overview
issues
7
pulls
pipelines
[api] add sync library script
tsiry-sandratraina.com
8 months ago
e26e734f
8ceb99ef
+24
-1
1 changed file
expand all
collapse all
unified
split
apps
api
src
scripts
sync-library.ts
+24
-1
apps/api/src/scripts/sync-library.ts
···
1
import chalk from "chalk";
2
import { ctx } from "context";
3
-
import { and, count, eq } from "drizzle-orm";
4
import tables from "schema";
5
import { InsertArtistAlbum } from "schema/artist-albums";
6
···
8
const total = await ctx.db
9
.select({ value: count() })
10
.from(tables.tracks)
0
0
0
0
0
0
11
.execute()
12
.then(([row]) => row.value);
13
0
0
14
for (let i = 0; i < total; i += size) {
15
const skip = i;
16
console.log(
···
19
const results = await ctx.db
20
.select()
21
.from(tables.tracks)
0
0
0
0
0
0
22
.limit(size)
23
.offset(skip)
24
.execute();
···
63
.execute()
64
.then((rows) => rows[0]),
65
]);
0
0
0
0
0
0
0
0
0
66
67
await ctx.db
68
.insert(tables.artistAlbums)
···
1
import chalk from "chalk";
2
import { ctx } from "context";
3
+
import { and, count, eq, not } from "drizzle-orm";
4
import tables from "schema";
5
import { InsertArtistAlbum } from "schema/artist-albums";
6
···
8
const total = await ctx.db
9
.select({ value: count() })
10
.from(tables.tracks)
11
+
.where(
12
+
and(
13
+
not(eq(tables.tracks.albumUri, null)),
14
+
not(eq(tables.tracks.artistUri, null))
15
+
)
16
+
)
17
.execute()
18
.then(([row]) => row.value);
19
20
+
console.log(`Total tracks to process: ${chalk.magentaBright(total)}`);
21
+
22
for (let i = 0; i < total; i += size) {
23
const skip = i;
24
console.log(
···
27
const results = await ctx.db
28
.select()
29
.from(tables.tracks)
30
+
.where(
31
+
and(
32
+
not(eq(tables.tracks.albumUri, null)),
33
+
not(eq(tables.tracks.artistUri, null))
34
+
)
35
+
)
36
.limit(size)
37
.offset(skip)
38
.execute();
···
77
.execute()
78
.then((rows) => rows[0]),
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
+
}
89
90
await ctx.db
91
.insert(tables.artistAlbums)