Skip to content

Commit 4327048

Browse files
committed
Uploader
1 parent ec14dd8 commit 4327048

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646
// Flag for map scraping, parsing, and uploading
4747
mapFlag := flag.Bool("map", false, "Alongside -scrape, -parse, or -upload, signifies that the UTD map should be scraped/parsed/uploaded.")
4848
// Flag for academic calendar scraping
49-
academicCalendars := flag.Bool("academicCalendars", false, "Alongside -scrape or -parse, signifies that the academic calendars should be scraped/parsed.")
49+
academicCalendars := flag.Bool("academicCalendars", false, "Alongside -scrape, -parse, or -upload, signifies that the academic calendars should be scraped/parsed/uploaded.")
5050

5151
// Flags for parsing
5252
parse := flag.Bool("parse", false, "Puts the tool into parsing mode.")
@@ -139,6 +139,8 @@ func main() {
139139
uploader.UploadEvents(*inDir)
140140
case *mapFlag:
141141
uploader.UploadMapLocations(*inDir)
142+
case *academicCalendars:
143+
uploader.UploadAcademicCalendars(*inDir)
142144
default:
143145
uploader.Upload(*inDir, *replace, *staticOnly)
144146
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
This file is responsible for handling uploading of parsed academic calendar data to MongoDB.
3+
*/
4+
5+
package uploader
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"log"
11+
"os"
12+
"time"
13+
14+
"github.com/UTDNebula/nebula-api/api/schema"
15+
"github.com/joho/godotenv"
16+
)
17+
18+
// Note that this uploader assumes that the collection names match the names of these files, which they should.
19+
// If the names of these collections ever change, the file names should be updated accordingly.
20+
21+
var academicCalendarFile = "academicCalendars.json"
22+
23+
func UploadAcademicCalendars(inDir string) {
24+
25+
//Load env vars
26+
if err := godotenv.Load(); err != nil {
27+
log.Panic("Error loading .env file")
28+
}
29+
30+
//Connect to mongo
31+
client := connectDB()
32+
33+
// Get 5 minute context
34+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
35+
defer cancel()
36+
37+
// Open data file for reading
38+
fptr, err := os.Open(fmt.Sprintf("%s/"+academicCalendarFile, inDir))
39+
if err != nil {
40+
if os.IsNotExist(err) {
41+
log.Panicf("File not found. Skipping %s", academicCalendarFile)
42+
}
43+
log.Panic(err)
44+
}
45+
46+
defer fptr.Close()
47+
48+
UploadData[schema.AcademicCalendar](client, ctx, fptr, true)
49+
}

0 commit comments

Comments
 (0)