1+ /*
2+ This file contains the code for the comet calendar events parser.
3+ */
4+
15package parser
26
37import (
@@ -8,14 +12,16 @@ import (
812 "regexp"
913 "slices"
1014 "strings"
15+ "time"
1116
17+ "github.com/UTDNebula/api-tools/scrapers"
1218 "github.com/UTDNebula/api-tools/utils"
1319 "github.com/UTDNebula/nebula-api/api/schema"
1420)
1521
1622// Some events have only the building name, not the abbreviation
1723// Maps building names to their abbreviations
18- var buildingAbbreviations = map [string ]string {
24+ var DefaultBuildings = map [string ]string {
1925 "Activity Center" : "AB" ,
2026 "Activity Center Bookstore" : "ACB" ,
2127 "Administration" : "AD" ,
@@ -74,7 +80,7 @@ var buildingAbbreviations = map[string]string{
7480}
7581
7682// Valid building abreviations for checking
77- var validAbbreviations []string = []string {
83+ var DefaultValid []string = []string {
7884 "AB" ,
7985 "ACB" ,
8086 "AD" ,
@@ -146,6 +152,11 @@ func ParseCometCalendar(inDir string, outDir string) {
146152 }
147153
148154 multiBuildingMap := make (map [string ]map [string ]map [string ][]schema.Event )
155+ // Some events have only the building name, not the abbreviation
156+ buildingAbbreviations , validAbbreviations , err := getLocationAbbreviations (inDir )
157+ if err != nil {
158+ panic (err )
159+ }
149160
150161 for _ , event := range allEvents {
151162
@@ -239,3 +250,52 @@ func ParseCometCalendar(inDir string, outDir string) {
239250
240251 utils .WriteJSON (fmt .Sprintf ("%s/cometCalendar.json" , outDir ), result )
241252}
253+
254+ // getAbbreviations dynamically retrieves the all of the locations abbreviations
255+ func getLocationAbbreviations (inDir string ) (map [string ]string , []string , error ) {
256+ // Get the locations from the map scraper
257+ var mapFile []byte
258+
259+ mapFile , err := os .ReadFile (inDir + "/mapLocations.json" )
260+ if err != nil {
261+ if os .IsNotExist (err ) {
262+ // Scrape the data if the it doesn't exist yet and then get the map file
263+ scrapers .ScrapeMapLocations (inDir )
264+ time .Sleep (2 * time .Second )
265+ ParseMapLocations (inDir , inDir )
266+ time .Sleep (2 * time .Second )
267+
268+ // If fail to get the locations again, it's not because location is unscraped
269+ mapFile , err = os .ReadFile (inDir + "/mapLocations.json" )
270+ if err != nil {
271+ return nil , nil , err
272+ }
273+ } else {
274+ return nil , nil , err
275+ }
276+ }
277+
278+ var locations []schema.MapBuilding
279+ if err = json .Unmarshal (mapFile , & locations ); err != nil {
280+ return nil , nil , err
281+ }
282+
283+ // Process the abbreviations
284+ buildingsAbbreviations := make (map [string ]string , 0 ) // Maps building names to their abbreviations
285+ validAbbreviations := make ([]string , 0 ) // Valid building abreviations for checking
286+
287+ for _ , location := range locations {
288+ // Trim the following acronym in the name
289+ trimmedName := strings .Split (* location .Name , " (" )[0 ]
290+ // Fallback on the locations that have no acronyms
291+ abbreviation := ""
292+ if location .Acronym != nil {
293+ abbreviation = * location .Acronym
294+ }
295+
296+ buildingsAbbreviations [trimmedName ] = abbreviation
297+ validAbbreviations = append (validAbbreviations , abbreviation )
298+ }
299+
300+ return buildingsAbbreviations , validAbbreviations , nil
301+ }
0 commit comments