Skip to content

Commit

Permalink
db: updated schema and db-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwiseman committed Jul 8, 2024
1 parent 4782501 commit c52178f
Show file tree
Hide file tree
Showing 5 changed files with 537 additions and 9 deletions.
19 changes: 12 additions & 7 deletions packages/db-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const showId = 1
const srtParser = new SrtParser2()

async function generateSrtForDir(dirPath: string): Promise<void> {
const dirContents = await fs.readdir(dirPath, { withFileTypes: true })
const dirContents = await fs.readdir(path.join(basePath, dirPath), { withFileTypes: true })
for (const item of dirContents) {
if (item.isDirectory()) await generateSrtForDir(path.join(dirPath, item.name))
if (item.isDirectory()) await generateSrtForDir(path.join(basePath, dirPath, item.name))
if (item.name.endsWith(".mkv")) {
generateSubs(path.join(dirPath, item.name))
}
Expand All @@ -24,24 +24,29 @@ function generateSubs(filePath: string): void {
}

async function insertSubsForDir(dirPath: string, seasonId: number): Promise<void> {
const dirContents = await fs.readdir(dirPath, { withFileTypes: true })
const dirContents = await fs.readdir(path.join(basePath, dirPath), { withFileTypes: true })
for (const item of dirContents) {


if (item.isDirectory()) {
console.log(`Inserting season ${item.name}...`)
const inserted = await db.insert(season).values({ showId, title: item.name, dirName: item.name, number: parseInt(/Season [0-9]+(?= )/.exec(item.name)?.toString().replace("Season ", "") ?? "") }).returning({ insertedId: season.id })
const data = { showId, title: item.name, number: parseInt(/Season [0-9]+(?= )/.exec(item.name)?.toString().replace("Season ", "") ?? "") }
const inserted = await db.insert(season).values(data).returning({ insertedId: season.id }).onConflictDoUpdate({ target: season.id, set: data })
if (typeof inserted[0]?.insertedId === 'number')
await insertSubsForDir(path.join(dirPath, item.name), inserted[0].insertedId)
else console.error("Didn't get ID back from database...")
}


if (item.name.endsWith(".srt")) {
const srtContents = await fs.readFile(path.join(dirPath, item.name))
const srtContents = await fs.readFile(path.join(basePath, dirPath, item.name))
const srtData = srtParser.fromSrt(srtContents.toString())
console.log(`Inserting episode ${item.name}...`)
const inserted = await db.insert(episode).values({ title: item.name.includes("Top Gear - ") ? undefined : item.name.replace(".srt", ""), number: /S[0-9]{2}E[0-9]{2}/.test(item.name) ? parseInt(/E[0-9]{2}/.exec(item.name)?.toString().replace("E", "") ?? "") : undefined, fileName: item.name.replace(".srt", ".mkv"), seasonId }).returning({ insertedId: episode.id })

const data = { title: item.name.includes("Top Gear - ") ? undefined : item.name.replace(".srt", ""), number: /S[0-9]{2}E[0-9]{2}/.test(item.name) ? parseInt(/E[0-9]{2}/.exec(item.name)?.toString().replace("E", "") ?? "") : undefined, filePath: path.join(dirPath, item.name.replace(".srt", ".mkv")), seasonId }

const inserted = await db.insert(episode).values(data).returning({ insertedId: episode.id }).onConflictDoUpdate({ target: episode.id, set: data })

if (typeof inserted[0]?.insertedId === "number")
await db.insert(quote).values(srtData.map((quoteItem) => ({
text: quoteItem.text.replace(/[\u{0080}-\u{10FFFF}]/gu, ""),
Expand All @@ -55,4 +60,4 @@ async function insertSubsForDir(dirPath: string, seasonId: number): Promise<void
}
}

await insertSubsForDir(basePath, 0)
await insertSubsForDir("", 0)
4 changes: 4 additions & 0 deletions packages/db/src/migrations/0001_lumpy_lila_cheney.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE `episode` RENAME COLUMN `file_name` TO `file_path`;--> statement-breakpoint
ALTER TABLE season ADD `streaming_link` text;--> statement-breakpoint
ALTER TABLE season ADD `streaming_icon_url` text;--> statement-breakpoint
ALTER TABLE `season` DROP COLUMN `dir_name`;
Loading

0 comments on commit c52178f

Please sign in to comment.