File tree Expand file tree Collapse file tree 3 files changed +52
-3
lines changed Expand file tree Collapse file tree 3 files changed +52
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { addDays , subDays } from "date-fns"
2
- import { withinCutoff } from "./helpers"
2
+ import { isValidVideoUrl , withinCutoff } from "./helpers"
3
3
4
4
describe ( "withinCutoff true" , ( ) => {
5
5
beforeEach ( ( ) => {
@@ -37,3 +37,26 @@ describe("withinCutoff true", () => {
37
37
expect ( result ) . toEqual ( false )
38
38
} )
39
39
} )
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 1
1
import { isAfter , subDays } from "date-fns"
2
2
3
+ const VIDEO_FORMAT_ALLOWLIST = [ "mp4" ]
4
+
3
5
export const withinCutoff = ( date : Date ) => {
4
6
const now = new Date ( )
5
7
const cutoff = subDays ( now , 8 )
6
8
7
9
return isAfter ( date , cutoff ) && ! isAfter ( date , now )
8
10
}
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 {
19
19
import { currentGeneralCourt } from "../shared"
20
20
import { randomBytes } from "node:crypto"
21
21
import { sha256 } from "js-sha256"
22
- import { withinCutoff } from "./helpers"
22
+ import { isValidVideoUrl , withinCutoff } from "./helpers"
23
23
import ffmpeg from "fluent-ffmpeg"
24
24
import fs from "fs"
25
25
abstract class EventScraper < ListItem , Event extends BaseEvent > {
@@ -256,7 +256,9 @@ const getHearingVideoUrl = async (EventId: number) => {
256
256
dom . window . document . querySelectorAll ( "video source" )
257
257
if ( maybeVideoSource . length && maybeVideoSource [ 0 ] ) {
258
258
const firstVideoSource = maybeVideoSource [ 0 ] as HTMLSourceElement
259
- return firstVideoSource . src
259
+ const maybeVideoUrl = firstVideoSource . src
260
+
261
+ return isValidVideoUrl ( maybeVideoUrl ) ? maybeVideoUrl : null
260
262
}
261
263
}
262
264
}
You can’t perform that action at this time.
0 commit comments