Implement metadata refresh

This commit is contained in:
Zoe Roux
2026-03-27 10:40:22 +01:00
parent 3a2aa61ac1
commit 59187a024b
27 changed files with 2153 additions and 35 deletions
-1
View File
@@ -99,7 +99,6 @@ export const entrySort = Sort(
episodeNumber: entries.episodeNumber,
number: entries.episodeNumber,
airDate: entries.airDate,
nextRefresh: entries.nextRefresh,
playedDate: entryProgressQ.playedDate,
},
{
-1
View File
@@ -35,7 +35,6 @@ const seasonSort = Sort(
endAir: seasons.endAir,
entriesCount: seasons.entriesCount,
availableCount: seasons.availableCount,
nextRefresh: seasons.nextRefresh,
},
{
default: ["seasonNumber"],
@@ -20,7 +20,7 @@ export const insertCollection = record(
| ({ kind: "movie" } & SeedMovie)
| ({ kind: "serie" } & SeedSerie)
) & {
nextRefresh: Date;
nextRefresh: string;
},
original: Original,
) => {
@@ -12,7 +12,6 @@ import { KErrorT } from "~/models/error";
import { record } from "~/otel";
import { duplicates } from "~/utils";
import { enqueueOptImage, flushImageQueue, type ImageTask } from "../images";
import { guessNextRefresh } from "../refresh";
import { updateAvailableCount, updateAvailableSince } from "./shows";
type SeedEntry = SEntry & {
@@ -66,10 +65,6 @@ export const insertEntries = record(
url: seed.thumbnail,
column: entries.thumbnail,
}),
nextRefresh:
entry.kind !== "extra"
? guessNextRefresh(entry.airDate ?? new Date())
: guessNextRefresh(new Date()),
episodeNumber:
entry.kind === "episode"
? entry.episodeNumber
@@ -4,7 +4,6 @@ import { conflictUpdateAllExcept, unnestValues } from "~/db/utils";
import type { SeedSeason } from "~/models/season";
import { record } from "~/otel";
import { enqueueOptImage, flushImageQueue, type ImageTask } from "../images";
import { guessNextRefresh } from "../refresh";
type SeasonI = typeof seasons.$inferInsert;
type SeasonTransI = typeof seasonTranslations.$inferInsert;
@@ -25,7 +24,6 @@ export const insertSeasons = record(
season.seasonNumber === 0
? `${show.slug}-specials`
: `${show.slug}-s${season.seasonNumber}`,
nextRefresh: guessNextRefresh(season.startAir ?? new Date()),
};
});
const ret = await tx
+1 -1
View File
@@ -53,7 +53,7 @@ export const seedMovie = async (
}
const { translations, videos, collection, studios, staff, ...movie } = seed;
const nextRefresh = guessNextRefresh(movie.airDate ?? new Date());
const nextRefresh = guessNextRefresh({ ...seed, kind: "movie" });
const ori = translations[movie.originalLanguage];
const original = ori
? {
+23 -2
View File
@@ -1,6 +1,27 @@
import type { SeedMovie } from "~/models/movie";
import type { SeedSerie } from "~/models/serie";
export const guessNextRefresh = (
show: (SeedSerie & { kind: "serie" }) | (SeedMovie & { kind: "movie" }),
) => {
if (show.kind === "movie") {
return fromAirDate(show.airDate ?? new Date());
}
const lastAirDate = show.entries
.filter((x) => x.airDate)
.map((x) => new Date(x.airDate!))
.reduce((max, cur) => (cur > max ? cur : max));
return fromAirDate(lastAirDate);
};
// oh i hate js dates so much.
export const guessNextRefresh = (airDate: Date | string) => {
const fromAirDate = (airDate: string | Date) => {
if (typeof airDate === "string") airDate = new Date(airDate);
if (airDate.getTime() > Date.now()) {
return airDate.toISOString().split("T")[0];
}
const diff = Date.now() - airDate.getTime();
const days = diff / (24 * 60 * 60 * 1000);
@@ -8,5 +29,5 @@ export const guessNextRefresh = (airDate: Date | string) => {
if (days <= 4) ret.setDate(ret.getDate() + 4);
else if (days <= 21) ret.setDate(ret.getDate() + 14);
else ret.setMonth(ret.getMonth() + 2);
return ret;
return ret.toISOString().split("T")[0];
};
+1 -1
View File
@@ -89,7 +89,7 @@ export const seedSerie = async (
staff,
...serie
} = seed;
const nextRefresh = guessNextRefresh(serie.startAir ?? new Date());
const nextRefresh = guessNextRefresh({ ...seed, kind: "serie" });
const ori = translations[serie.originalLanguage];
const original = ori
? {
+1
View File
@@ -91,6 +91,7 @@ export const showFilters: FilterDef = {
column: (source: string) => sql`(${shows.rating}->>${source})::int`,
type: "int",
},
nextRefresh: { column: shows.nextRefresh, type: "date" },
};
export const showSort = Sort(
{
-1
View File
@@ -78,7 +78,6 @@ export const entries = schema.table(
.notNull()
.$onUpdate(() => new Date()),
availableSince: timestamp({ withTimezone: true, precision: 3 }),
nextRefresh: timestamp({ withTimezone: true, precision: 3 }).notNull(),
},
(t) => [
unique().on(t.showPk, t.seasonNumber, t.episodeNumber),
-1
View File
@@ -51,7 +51,6 @@ export const seasons = schema.table(
updatedAt: timestamp({ withTimezone: true, precision: 3 })
.notNull()
.$onUpdate(() => new Date()),
nextRefresh: timestamp({ withTimezone: true, precision: 3 }).notNull(),
},
(t) => [
unique().on(t.showPk, t.seasonNumber),
+1 -1
View File
@@ -93,7 +93,7 @@ export const shows = schema.table(
updatedAt: timestamp({ withTimezone: true, precision: 3 })
.notNull()
.$onUpdate(() => new Date()),
nextRefresh: timestamp({ withTimezone: true, precision: 3 }).notNull(),
nextRefresh: date().notNull(),
},
(t) => [
unique("kind_slug").on(t.kind, t.slug),
-1
View File
@@ -32,7 +32,6 @@ const BaseCollection = t.Object({
descrpition: "Date of the last item of the collection",
}),
),
nextRefresh: t.Date(),
externalId: ExternalId(),
});
-2
View File
@@ -11,8 +11,6 @@ export const BaseEntry = () =>
}),
),
thumbnail: t.Nullable(Image),
nextRefresh: t.Date(),
});
export const EntryTranslation = () =>
+1 -1
View File
@@ -33,7 +33,7 @@ const BaseMovie = t.Object({
t.Number({ minimum: 0, description: "Runtime of the movie in minutes." }),
),
airDate: t.Nullable(t.String({ format: "date" })),
nextRefresh: t.Date(),
nextRefresh: t.Nullable(t.String({ format: "date" })),
externalId: ExternalId(),
});
-2
View File
@@ -12,8 +12,6 @@ export const BaseSeason = t.Object({
startAir: t.Nullable(t.String({ format: "date" })),
endAir: t.Nullable(t.String({ format: "date" })),
nextRefresh: t.Date(),
externalId: SeasonId,
});
+1 -1
View File
@@ -43,7 +43,7 @@ const BaseSerie = t.Object({
),
startAir: t.Nullable(t.String({ format: "date" })),
endAir: t.Nullable(t.String({ format: "date" })),
nextRefresh: t.Date(),
nextRefresh: t.Nullable(t.String({ format: "date" })),
externalId: ExternalId(),
});
+2 -2
View File
@@ -34,7 +34,7 @@ export type Property = {
export type Value =
| { type: "int"; value: number }
| { type: "float"; value: number }
| { type: "date"; value: string }
| { type: "date"; value: Date }
| { type: "string"; value: string }
| { type: "enum"; value: string }
| { type: "bool"; value: boolean };
@@ -92,7 +92,7 @@ const dateVal = t(
),
map(([year, month, day]) => ({
type: "date" as const,
value: `${year}-${month}-${day}`,
value: new Date(`${year}-${month}-${day}`),
})),
),
).expects("a date");