Readme

processTracksBlobsJob

Periodically fetch all Spotify tracks from Blob storage and update the tracks database with a subset of display information.

Keys take the format: spotify_track_id:4tVhBXOhSlqYCL2IjyoUNu.

Runs every 1 hrs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { blob } from "https://esm.town/v/std/blob";
import { sqlite } from "https://esm.town/v/std/sqlite";
import { sql } from "npm:drizzle-orm";
import { drizzle } from "npm:drizzle-orm/libsql";
import { integer, sqliteTable, text } from "npm:drizzle-orm/sqlite-core";
async function dropDb() {
await sqlite.execute(`
drop table if exists tracks`);
}
// spotify_track_id:4tVhBXOhSlqYCL2IjyoUNu
const db = drizzle(sqlite as any);
const tracks = sqliteTable("tracks", {
id: integer("id").primaryKey(),
track_id: text("track_id").notNull(),
artist: text("artist"),
title: text("title"),
url: text("url"),
image_url: text("image_url"),
thumbnail_url: text("thumbnail_url"),
icon_url: text("icon_url"),
duration_ms: text("duration_ms"),
duration: text("duration"),
is_hidden: integer("is_hidden", { mode: "boolean" }),
});
async function setUpDb() {
await sqlite.execute(`
create table if not exists tracks (
id integer primary key autoincrement,
track_id text UNIQUE,
artist text,
title text,
url text,
image_url text,
thumbnail_url text,
icon_url text,
duration_ms int,
duration int,
is_hidden int DEFAULT 0
)
`);
}
export async function refreshTrackFromBlobKey(key: string) {
const trackInfo = await blob.getJSON(key);
if (!trackInfo) {
console.warn("Could not find key", key);
}
console.debug("Found key", key);
const { id, album, name: title, artists, external_urls, duration_ms } = trackInfo;
const result = await db.insert(tracks)
.values({
track_id: id,
artist: artists[0].name,
title,
url: external_urls.spotify,
image_url: album.images[0].url,
thumbnail_url: album.images[1].url,
icon_url: album.images[2].url,
duration_ms,
duration: duration_ms / 1_000.0,
is_hidden: 0,
})
.onConflictDoNothing();
console.debug("Updated", id, artists[0].name, title);
}
export async function perform() {
const spotifyTracks = await blob.list("spotify_track_id");
for (const spotifyTrack of spotifyTracks) {
await refreshTrackFromBlobKey(spotifyTrack.key);
}
return;
}
export default async function(interval: Interval) {
// await dropDb();
// await setUpDb();
await perform();
return;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 28, 2024