diff options
Diffstat (limited to 'app/lib')
| -rw-r--r-- | app/lib/scraper-runner.server.ts | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/app/lib/scraper-runner.server.ts b/app/lib/scraper-runner.server.ts index 070a568..191dd00 100644 --- a/app/lib/scraper-runner.server.ts +++ b/app/lib/scraper-runner.server.ts @@ -1,6 +1,24 @@ import { upsertVenue, upsertEvent } from "./db.server"; import { generateVenueMarkdown, generateAllVenueMarkdown } from "./markdown-writer.server"; import { ALL_SCRAPERS } from "~/scrapers/index"; +import type { EventInput } from "./db.server"; + +const SCRAPE_WINDOW_DAYS = 35; // ~1 month + +function scrapeWindow(): { from: string; to: string } { + const from = new Date(); + from.setHours(0, 0, 0, 0); + const to = new Date(from); + to.setDate(to.getDate() + SCRAPE_WINDOW_DAYS); + return { + from: from.toISOString().slice(0, 10), + to: to.toISOString().slice(0, 10), + }; +} + +function withinWindow(event: EventInput, from: string, to: string): boolean { + return event.date >= from && event.date <= to; +} export interface ScrapeResult { venue_id: string; @@ -19,7 +37,10 @@ export async function runAllScrapers(): Promise<ScrapeResult[]> { upsertVenue(venue.id, venue.name, venue.url, venue.area); try { - const events = await scraper.scrape(); + const { from, to } = scrapeWindow(); + const events = (await scraper.scrape()).filter((e) => + withinWindow(e, from, to) + ); for (const event of events) { upsertEvent(event); } @@ -55,7 +76,10 @@ export async function runScraper(venueId: string): Promise<ScrapeResult> { upsertVenue(venue.id, venue.name, venue.url, venue.area); try { - const events = await scraper.scrape(); + const { from, to } = scrapeWindow(); + const events = (await scraper.scrape()).filter((e) => + withinWindow(e, from, to) + ); for (const event of events) { upsertEvent(event); } |
