/
generate-seed.ts
302 lines (260 loc) · 7.79 KB
/
generate-seed.ts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { MovieDetailSchema, MovieResultSchema } from "../src/zod";
import Database from "bun:sqlite";
import { csvParse } from "d3";
import { customAlphabet } from "nanoid";
import { z } from "zod";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import {
movies as MoviesTable,
directors as DirectorsTable,
rankings as RankingsTable,
moviesToDirectors,
} from "../src/db/schema";
import { hc } from "hono/client";
import { AppType } from "tspdt-api/src/index";
// const client = hc<AppType>("http://localhost:8787");
const alphabet =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
const nanoid = customAlphabet(alphabet, 9);
const text = await Bun.file("./tspdt-starting-list.csv").text();
const sqlite = new Database("./tspdt.db");
const db = drizzle(sqlite);
migrate(db, { migrationsFolder: "./migrations" });
const FindByIdResultSchema = z.object({
movie_results: z.array(MovieResultSchema),
});
type MovieData = {
"2007": string;
"2008": string;
"2010": string;
"2011": string;
"2012": string;
"2013": string;
"2014": string;
"2015": string;
"2016": string;
"2017": string;
"2018": string;
"2019": string;
"2020": string;
"2021": string;
"2022": string;
"2023": string;
"2024": string;
New: string;
"Director(s)": string;
Title: string;
Year: string;
Country: string;
Length: string;
Colour: string;
Genre: string;
"Dec-06": string;
"Mar-06": string;
IMDb: string;
IMDB_ID: string;
idTSPDT: string;
};
type MovieColumn = keyof MovieData;
const parsed = csvParse<MovieColumn>(text);
type RankingKey = {
[k in keyof MovieData]: k extends `${number}` | "Dec-06" | "Mar-06"
? k
: never;
}[keyof MovieData];
function isRankingKey(key: string): key is RankingKey {
return /\d{4}/.test(key) || key === "Dec-06" || key === "Mar-06";
}
async function imdbToTmdb(imdbId: string) {
const res = await fetch(
`https://api.themoviedb.org/3/find/${imdbId}?external_source=imdb_id`,
{
headers: {
Authorization: `Bearer ${Bun.env.TMDB_TOKEN}`,
accept: "application/json",
},
}
);
const json = await res.json();
console.dir({ json }, { depth: null });
try {
const parsed = FindByIdResultSchema.parse(json);
return parsed.movie_results[0];
} catch (e) {
console.error(e);
}
}
async function tmdbMovieDetails(tmdbId: number) {
const res = await fetch(
`https://api.themoviedb.org/3/movie/${tmdbId}?append_to_response=credits`,
{
headers: {
Authorization: `Bearer ${Bun.env.TMDB_TOKEN}`,
accept: "application/json",
},
}
);
try {
const json = await res.json();
const parsed = MovieDetailSchema.parse(json);
return parsed;
} catch (e) {
console.error(e);
}
}
// TODO: set currentRanking
const reverseName = (name: string) =>
name
.split(",")
.map((n) => n.trim())
.reverse()
.join(" ");
// first let's deal with everything that has tmdb id... then we'll deal with everything else
const moviesWithTmdbId = parsed
.filter((p) => p.IMDB_ID && p.IMDB_ID !== "#N/A" && +p["2024"])
// sort by rankings
.sort((a, b) => parseInt(a["2024"]) - parseInt(b["2024"]));
const delay = async (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));
// TODO: tmdb has rough api limit of 50 requests per second. we should respect that and aim for no more than 25 requests per second.
const directorToIdLookup = new Map<string, string>();
// TODO: amidst all this is to ingest data into vector database.
let i = 0;
// let's get first 1000
for (const p of moviesWithTmdbId.slice(0, 1000)) {
if (!p.IMDB_ID) continue;
i++;
const movie = await imdbToTmdb(p.IMDB_ID);
const genres = p.Genre.split("-").map((g) => g.trim());
const color =
p.Colour === "BW"
? "bw"
: p.Colour === "Col" || p.Colour === "Colour"
? "col"
: "col-bw";
const country = p.Country.split("-").map((c) => c.trim());
// TODO: fix title if not in tmdb
let directors: string[] = [];
const directorToTmdbId = new Map<string, number>();
const twoDirectors = p["Director(s)"].split("&");
const manyDirectors = p["Director(s)"].split("/");
if (twoDirectors.length > 1) {
for (const director of twoDirectors) {
const name = reverseName(director.trim());
if (!directors.includes(name)) {
directors.push(name);
}
}
} else if (manyDirectors.length > 1) {
for (const director of manyDirectors) {
const name = reverseName(director.trim());
if (!directors.includes(name)) {
directors.push(name);
}
}
} else {
const name = reverseName(p["Director(s)"].trim());
if (!directors.includes(name)) {
directors.push(name);
}
directors.push(reverseName(p["Director(s)"].trim()));
}
if (movie) {
// https://tspdt-api.floral-violet-deef.workers.dev
// const vector = await run("@cf/baai/bge-base-en-v1.5", {
// text: movie.overview,
// });
// console.log({ vector });
// const movieDetails =
const tmovie = await tmdbMovieDetails(movie.id);
console.log({ tmovie });
console.log({ genres });
const tdirectors = tmovie?.credits.crew.filter((c) => c.job === "Director");
if (tdirectors) {
directors = tdirectors.map((d) => d.name);
for (const director of tdirectors) {
directorToTmdbId.set(director.name, director.id);
}
}
// const res = await client.movie.$post({
// json: {
// overview: movie.overview,
// title: movie.title,
// year: +p.Year,
// budget: tmovie?.budget ?? null,
// cast: (tmovie?.credits.cast.map((c) => c.name) ?? []).join(", "),
// color,
// country: country.join(", "),
// director: (
// tmovie?.credits.crew
// .filter((c) => c.job === "Director")
// .map((c) => c.name) ?? directors
// ).join(", "),
// genre: genres.join(", "),
// runtime: p.Length ? +p.Length : null,
// id: p.idTSPDT,
// tmdbPosterPath: movie.poster_path,
// },
// });
// console.log({ res });
db.insert(MoviesTable)
.values({
id: p.idTSPDT,
title: movie.title ?? p.Title,
year: +p.Year,
imdbId: p.IMDB_ID,
tmdbId: movie.id,
tmdbPosterPath: movie.poster_path,
tmdbBackdropPath: movie.backdrop_path,
color,
country,
genre: genres,
overview: movie.overview,
runtime: p.Length ? +p.Length : null,
currentRanking: +p["2024"] ?? null,
})
.onConflictDoNothing()
.run();
}
const rankingKeys = Object.keys(p).filter(isRankingKey);
for (const key of rankingKeys) {
// for clarity, we'll just use 2006 for Dec-06 and 2005 for Mar-06
const year = key === "Dec-06" ? 2006 : key === "Mar-06" ? 2005 : +key;
db.insert(RankingsTable)
.values({
movieId: p.idTSPDT,
year,
ranking: +p[key],
})
.run();
}
// lookup director...? seems wasteful to not cache rest of data in some way. could also just parse director name from csv, but then we won't have tmdb id. could look this up just in time, and then cache it.
for (const director of directors) {
let id = directorToIdLookup.get(director);
let tmdbId = directorToTmdbId.get(director);
if (!id) {
id = nanoid();
directorToIdLookup.set(director, id);
}
db.insert(DirectorsTable)
.values({
id,
name: director,
tmdbId,
})
.onConflictDoNothing()
.run();
db.insert(moviesToDirectors)
.values({
movieId: p.idTSPDT,
directorId: id,
})
.onConflictDoNothing()
.run();
}
if (i % 40 === 0) {
console.log("sleeping for 1 second");
await delay(1000);
}
}