-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor for better clarity and fix several data layer bugs
- Loading branch information
1 parent
b125241
commit 2b83051
Showing
5 changed files
with
224 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { z } from "zod"; | ||
|
||
const articleSchema = z.object({ | ||
title: z | ||
.string() | ||
.refine((title) => title.split(" ").length >= 3, { | ||
message: "Title must contain at least 3 words", | ||
}) | ||
.refine( | ||
(title) => | ||
!["Video Duration", "play", "play-inverse"].some((prefix) => | ||
title.startsWith(prefix), | ||
), | ||
{ | ||
message: "Title starts with an invalid prefix", | ||
}, | ||
), | ||
link: z.string().url(), | ||
source: z.string(), | ||
}); | ||
|
||
export default articleSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import db from "./db"; | ||
import { Article } from "../types"; | ||
import articleSchema from "./articleSchema"; | ||
|
||
const isValidArticle = (article: Article) => { | ||
try { | ||
articleSchema.parse(article); | ||
return true; | ||
} catch (e) { | ||
if (process.env["DEBUG"] === "true") { | ||
console.log( | ||
`*** INVALID: ${article.source}: ${article.title} - ${e.errors.map((err: any) => err.message).join(", ")}`, | ||
); | ||
} | ||
return false; | ||
} | ||
}; | ||
|
||
const insertArticle = (article: Article) => { | ||
const insert = db.prepare( | ||
"INSERT INTO articles (id, title, link, source, created_at) VALUES (?, ?, ?, ?, ?)", | ||
); | ||
|
||
const checkExistence = db.prepare( | ||
"SELECT COUNT(*) as count FROM articles WHERE title = ?", | ||
); | ||
|
||
const result = checkExistence.get(article.title) as { count: number }; | ||
if (result.count === 0) { | ||
try { | ||
insert.run( | ||
article.id, | ||
article.title, | ||
article.link, | ||
article.source, | ||
article.created_at, | ||
); | ||
} catch (error) { | ||
if (process.env["DEBUG"] === "true") { | ||
console.log(`*** ERROR: ${error.message}`); | ||
} | ||
} | ||
} else { | ||
if (process.env["DEBUG"] === "true") { | ||
console.log(`*** DUPLICATE: ${article.title}`); | ||
} | ||
} | ||
}; | ||
|
||
const clearCacheIfNeeded = () => { | ||
const oldestArticle = db | ||
.prepare("SELECT created_at FROM articles ORDER BY created_at ASC LIMIT 1") | ||
.get() as { created_at: string } | undefined; | ||
|
||
if (oldestArticle) { | ||
const articleDate = new Date(oldestArticle.created_at); | ||
const now = new Date(); | ||
const hoursDifference = | ||
(now.getTime() - articleDate.getTime()) / (1000 * 60 * 60); | ||
|
||
if (hoursDifference >= 8) { | ||
if (process.env["DEBUG"] === "true") { | ||
console.log("*** CLEARING CACHE"); | ||
} | ||
db.prepare("DELETE FROM articles").run(); | ||
} | ||
} | ||
}; | ||
|
||
export { isValidArticle, insertArticle, clearCacheIfNeeded }; |
Oops, something went wrong.