@@ -15,14 +15,14 @@ interface CalendarData {
15
15
16
16
export class AutoScraper {
17
17
private static readonly calendarURL : string = 'https://lostarkcodex.com/us/eventcalendar/'
18
- private static buffer_data : string = ''
19
- private static calendar_data : CalendarData = { }
18
+ private static bufferData : string = ''
19
+ private static calendarData : CalendarData = { }
20
20
21
21
public static scrapeCalendarData ( ) : void {
22
22
https . get ( this . calendarURL , ( res : any ) => {
23
23
res . setEncoding ( 'utf8' )
24
24
res . on ( 'data' , ( chunk : string ) => {
25
- this . buffer_data += chunk
25
+ this . bufferData += chunk
26
26
} )
27
27
res . on ( 'end' , ( ) => {
28
28
this . extractCalendarData ( )
@@ -34,30 +34,30 @@ export class AutoScraper {
34
34
35
35
// Strip everything except the calendar_data variable
36
36
private static extractCalendarData ( ) : void {
37
- this . buffer_data = this . buffer_data
37
+ this . bufferData = this . bufferData
38
38
. replace ( / \r \n / gmi, ' ' )
39
39
. replace ( / .* v a r c a l e n d a r _ d a t a = / i, '' )
40
40
. replace ( / (?< = ; ) .* / i, '' )
41
41
. replace ( ';' , '' )
42
42
43
43
try {
44
- JSON . parse ( this . buffer_data )
44
+ JSON . parse ( this . bufferData )
45
45
} catch ( err : unknown ) {
46
46
console . error ( err ) ;
47
- this . buffer_data = '{}'
47
+ this . bufferData = '{}'
48
48
}
49
49
}
50
50
51
51
// Removes duplicates from entries
52
52
private static cleanCalendarData ( ) : void {
53
- this . calendar_data = JSON . parse ( this . buffer_data )
54
- Object . keys ( this . calendar_data ) . forEach (
55
- type => Object . keys ( this . calendar_data [ type ] ) . forEach (
56
- month => Object . keys ( this . calendar_data [ type ] [ month ] ) . forEach (
57
- day => Object . keys ( this . calendar_data [ type ] [ month ] [ day ] ) . forEach (
58
- event => Object . keys ( this . calendar_data [ type ] [ month ] [ day ] [ event ] ) . forEach (
53
+ this . calendarData = JSON . parse ( this . bufferData )
54
+ Object . keys ( this . calendarData ) . forEach (
55
+ type => Object . keys ( this . calendarData [ type ] ) . forEach (
56
+ month => Object . keys ( this . calendarData [ type ] [ month ] ) . forEach (
57
+ day => Object . keys ( this . calendarData [ type ] [ month ] [ day ] ) . forEach (
58
+ event => Object . keys ( this . calendarData [ type ] [ month ] [ day ] [ event ] ) . forEach (
59
59
// Overwrite current array with one without duplicates
60
- id => this . calendar_data [ type ] [ month ] [ day ] [ event ] [ id ] = Array . from ( new Set ( this . calendar_data [ type ] [ month ] [ day ] [ event ] [ id ] ) )
60
+ id => this . calendarData [ type ] [ month ] [ day ] [ event ] [ id ] = Array . from ( new Set ( this . calendarData [ type ] [ month ] [ day ] [ event ] [ id ] ) )
61
61
)
62
62
)
63
63
)
@@ -67,9 +67,11 @@ export class AutoScraper {
67
67
68
68
// If data was successfully formed, backs up the previous data file, then creates a new data file
69
69
private static writeData ( ) : void {
70
- if ( Object . values ( this . calendar_data ) !== Object . values ( { } ) ) {
71
- fs . renameSync ( './data/data.json' , './data/data.old.json' )
72
- fs . writeFileSync ( './data/data.json' , JSON . stringify ( this . calendar_data ) , 'utf8' )
70
+ if ( Object . values ( this . calendarData ) . length > 0 ) {
71
+ fs . rename ( './data/data.json' , './data/data.old.json' , ( err : Error ) => {
72
+ if ( err ) console . error ( err )
73
+ fs . writeFileSync ( './data/data.json' , JSON . stringify ( this . calendarData ) , 'utf8' )
74
+ } )
73
75
}
74
76
}
75
77
}
0 commit comments