Add caching
This commit is contained in:
parent
f971d7fd41
commit
a124b7f234
2 changed files with 14 additions and 3 deletions
|
@ -8,15 +8,26 @@ class ServerLdScrape extends Script {
|
|||
}
|
||||
async main(args: string[]): Promise<void> {
|
||||
const port = Number(args[0] ?? 2346)
|
||||
|
||||
const cache: Record<string, { date: Date; contents: any }> = {}
|
||||
|
||||
new Server(port, {}, [
|
||||
{
|
||||
mustMatch: "extractgraph",
|
||||
mimetype: "application/ld+json",
|
||||
async handle(content, searchParams: URLSearchParams) {
|
||||
const url = searchParams.get("url")
|
||||
if (cache[url] !== undefined) {
|
||||
const { date, contents } = cache[url]
|
||||
// In seconds
|
||||
const tdiff = (new Date().getTime() - date.getTime()) / 1000
|
||||
if (tdiff < 24 * 60 * 60) {
|
||||
return contents
|
||||
}
|
||||
}
|
||||
const dloaded = await Utils.download(url, {
|
||||
"User-Agent":
|
||||
"MapComplete/openstreetmap scraper; pietervdvn@posteo.net; https://github.com/pietervdvn/MapComplete",
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36", // MapComplete/openstreetmap scraper; pietervdvn@posteo.net; https://github.com/pietervdvn/MapComplete",
|
||||
})
|
||||
const parsed = parse(dloaded)
|
||||
const scripts = Array.from(parsed.getElementsByTagName("script"))
|
||||
|
@ -32,7 +43,7 @@ class ServerLdScrape extends Script {
|
|||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
cache[url] = { contents: snippets, date: new Date() }
|
||||
return JSON.stringify(snippets)
|
||||
},
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue