File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { addDays , subDays } from "date-fns"
2- import { withinCutoff } from "./helpers"
2+ import { isValidVideoUrl , withinCutoff } from "./helpers"
33
44describe ( "withinCutoff true" , ( ) => {
55 beforeEach ( ( ) => {
@@ -37,3 +37,26 @@ describe("withinCutoff true", () => {
3737 expect ( result ) . toEqual ( false )
3838 } )
3939} )
40+
41+ describe ( "isValidVideoUrl" , ( ) => {
42+ it ( "should return true for a valid video URL" , ( ) => {
43+ const validUrl = "https://example.com/video.mp4"
44+ const result = isValidVideoUrl ( validUrl )
45+ expect ( result ) . toEqual ( true )
46+ } )
47+
48+ it ( "should return false for a missing URL" , ( ) => {
49+ const result = isValidVideoUrl ( null )
50+ expect ( result ) . toEqual ( false )
51+ } )
52+
53+ it ( "should return false for a URL with no file format" , ( ) => {
54+ const result = isValidVideoUrl ( "https://example.com/video" )
55+ expect ( result ) . toEqual ( false )
56+ } )
57+
58+ it ( "should return false for a URL with an unsupported format" , ( ) => {
59+ const result = isValidVideoUrl ( "https://example.com/video.m3u8" )
60+ expect ( result ) . toEqual ( false )
61+ } )
62+ } )
Original file line number Diff line number Diff line change 11import { isAfter , subDays } from "date-fns"
22
3+ const VIDEO_FORMAT_ALLOWLIST = [ "mp4" ]
4+
35export const withinCutoff = ( date : Date ) => {
46 const now = new Date ( )
57 const cutoff = subDays ( now , 8 )
68
79 return isAfter ( date , cutoff ) && ! isAfter ( date , now )
810}
11+
12+ // This isn't perfect because it relies on the file extension,
13+ // but it's a reasonable heuristic for now and should differentiate
14+ // the livestreams we don't want from the archived video we do want.
15+ export const isValidVideoUrl = ( url : string | null | undefined ) => {
16+ if ( ! url ) return false
17+
18+ const fileFormat = url . split ( "." ) . pop ( ) ?. toLowerCase ( )
19+ if ( ! fileFormat ) {
20+ console . log ( `Could not find file format for video URL: {url}` )
21+ return false
22+ } else if ( ! VIDEO_FORMAT_ALLOWLIST . includes ( fileFormat ) ) {
23+ console . log (
24+ `Url ${ url } has unsupported video format: ${ fileFormat } . Supported formats: ${ VIDEO_FORMAT_ALLOWLIST . join (
25+ ", "
26+ ) } `
27+ )
28+ return false
29+ }
30+
31+ return true
32+ }
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ import {
1919import { currentGeneralCourt } from "../shared"
2020import { randomBytes } from "node:crypto"
2121import { sha256 } from "js-sha256"
22- import { withinCutoff } from "./helpers"
22+ import { isValidVideoUrl , withinCutoff } from "./helpers"
2323import ffmpeg from "fluent-ffmpeg"
2424import fs from "fs"
2525abstract class EventScraper < ListItem , Event extends BaseEvent > {
@@ -256,7 +256,9 @@ const getHearingVideoUrl = async (EventId: number) => {
256256 dom . window . document . querySelectorAll ( "video source" )
257257 if ( maybeVideoSource . length && maybeVideoSource [ 0 ] ) {
258258 const firstVideoSource = maybeVideoSource [ 0 ] as HTMLSourceElement
259- return firstVideoSource . src
259+ const maybeVideoUrl = firstVideoSource . src
260+
261+ return isValidVideoUrl ( maybeVideoUrl ) ? maybeVideoUrl : null
260262 }
261263 }
262264 }
You can’t perform that action at this time.
0 commit comments