Skip to content

Commit c85059c

Browse files
committed
Refactor video scrape cuttoff checking
1 parent 615d4b3 commit c85059c

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

functions/src/events/helpers.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { subDays } from "date-fns"
2+
import { withinCutoff } from "./helpers"
3+
4+
describe("withinCutoff", () => {
5+
beforeEach(() => {
6+
jest.useFakeTimers()
7+
})
8+
9+
afterEach(() => {
10+
jest.useRealTimers()
11+
})
12+
13+
it("should return true for a date within three days", () => {
14+
const now = new Date()
15+
16+
const threeDaysAgo = subDays(now, 3)
17+
18+
const result = withinCutoff(threeDaysAgo)
19+
expect(result).toEqual(true)
20+
})
21+
})

functions/src/events/helpers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { isAfter, subDays } from "date-fns"
2+
3+
export const withinCutoff = (date: Date) => {
4+
const now = new Date()
5+
const cutoff = subDays(now, 8)
6+
7+
return isAfter(date, cutoff) && !isAfter(date, now)
8+
}

functions/src/events/scrapeEvents.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import { currentGeneralCourt } from "../shared"
2020
import { randomBytes } from "node:crypto"
2121
import { sha256 } from "js-sha256"
22-
import { differenceInDays } from "date-fns"
22+
import { withinCutoff } from "./helpers"
2323

2424
const assembly = new AssemblyAI({
2525
apiKey: process.env.ASSEMBLY_API_KEY ? process.env.ASSEMBLY_API_KEY : ""
@@ -156,14 +156,11 @@ class HearingScraper extends EventScraper<HearingListItem, Hearing> {
156156
.get()
157157
const eventData = eventInDb.data()
158158
const hearing = Hearing.check(eventData)
159-
const now = new Date()
160-
161-
const hearingIsTodayOrFuture =
162-
differenceInDays(hearing.startsAt.toDate(), now) < 8
159+
const shouldScrape = withinCutoff(hearing.startsAt.toDate())
163160

164161
let maybeVideoURL = null
165162
let transcript = null
166-
if (!hearing.videoFetchedAt && hearingIsTodayOrFuture) {
163+
if (!hearing.videoFetchedAt && shouldScrape) {
167164
const req = await fetch(
168165
`https://malegislature.gov/Events/Hearings/Detail/${EventId}`
169166
)
@@ -204,9 +201,7 @@ class HearingScraper extends EventScraper<HearingListItem, Hearing> {
204201
.collection("private")
205202
.doc("webhookAuth")
206203
.set({
207-
videoAssemblyWebhookToken: hearingIsTodayOrFuture
208-
? sha256(newToken)
209-
: null
204+
videoAssemblyWebhookToken: sha256(newToken)
210205
})
211206
}
212207
}

functions/src/webhooks/transcription.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as functions from "firebase-functions"
22
import { AssemblyAI } from "assemblyai"
33
import { db } from "../firebase"
4-
import { Hearing } from "../events/types"
54
import { sha256 } from "js-sha256"
65

76
const assembly = new AssemblyAI({

0 commit comments

Comments
 (0)