From 32c5b4088538b1a331962334621d0cab6c211e75 Mon Sep 17 00:00:00 2001 From: ajgeiss0702 Date: Sun, 15 Sep 2024 17:28:17 -0700 Subject: [PATCH] fetch less often when not near wan or if wan already started --- src/fetchers/youtube/scrape.ts | 13 ++++++++++++- src/utils.ts | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/utils.ts diff --git a/src/fetchers/youtube/scrape.ts b/src/fetchers/youtube/scrape.ts index 24b8c3f..a4df3ac 100644 --- a/src/fetchers/youtube/scrape.ts +++ b/src/fetchers/youtube/scrape.ts @@ -1,15 +1,24 @@ import { CHANNEL, v } from './index'; import { Env } from '../../worker'; +import { isNearWan } from '../../utils'; let lastIdFetch = 0; let lastId: string | undefined; +let lastHadId = 0; + let wEnv: Env; export async function getLivestreamId(env: Env) { wEnv = env; - if(Date.now() - lastIdFetch < 10e3) { + let cacheTime = isNearWan() ? 10e3 : 30e3; + if(Date.now() - lastIdFetch < cacheTime) { + return lastId; + } + + // If we have an id (they are live) then dont update as often (only once per minute) + if(Date.now() - lastHadId < 60e3) { return lastId; } @@ -58,6 +67,8 @@ async function realGetLivestreamId() { return undefined; } + lastHadId = Date.now(); + return v; } diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..7c854ee --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,10 @@ +export function isNearWan(now?: Date) { + const d = now ? now : new Date(); + if(d.getUTCDay() === 5) { + return d.getUTCHours() > 20; + } else if(d.getUTCDay() === 6) { + return d.getUTCHours() <= 11; + } else { + return false; + } +}