Skip to content

Commit

Permalink
fetch less often when not near wan or if wan already started
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Sep 16, 2024
1 parent 5f8934e commit 32c5b40
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/fetchers/youtube/scrape.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down Expand Up @@ -58,6 +67,8 @@ async function realGetLivestreamId() {
return undefined;
}

lastHadId = Date.now();

return v;
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 32c5b40

Please sign in to comment.