Skip to content

Commit 414a777

Browse files
committed
Add calendar parser
1 parent fdb5f9b commit 414a777

File tree

1 file changed

+205
-13
lines changed

1 file changed

+205
-13
lines changed

parser/calendarParser.go

Lines changed: 205 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,34 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"regexp"
9+
"slices"
810
"strings"
11+
"slices"
12+
"regexp"
913

1014
"github.com/UTDNebula/api-tools/utils"
1115
"github.com/UTDNebula/nebula-api/api/schema"
1216
)
1317

14-
func ParseCalendar(inDir string, outDir string) {
15-
16-
calendarFile, err := os.ReadFile(inDir + "/events.json")
17-
if err != nil {
18-
panic(err)
19-
}
20-
21-
var result []schema.MultiBuildingEvents[schema.Event]
22-
log.Printf("Test")
23-
}
24-
18+
<<<<<<< HEAD
19+
=======
20+
// Some events have only the building name, not the abbreviation
21+
// Maps building names to their abbreviations
22+
>>>>>>> 0af6300 (Add calendar parser)
2523
var buildingAbbreviations = map[string]string{
26-
"Other Building": "",
2724
"Activity Center": "AB",
2825
"Activity Center Bookstore": "ACB",
2926
"Administration": "AD",
30-
"Edith and Peter O'Donnell Jr. Athenaeum": "APC",
27+
"Edith and Peter O’Donnell Jr. Athenaeum": "APC",
28+
"Edith O'Donnell Arts and Technology Building": "ATC",
3129
"Lloyd V. Berkner Hall": "BE",
3230
"Bioengineering and Sciences Building": "BSB",
3331
"Classroom Building": "CB",
3432
"Callier Center Richardson": "CR",
3533
"Callier Center Addition": "CRA",
3634
"Davidson-Gundy Alumni Center": "DGA",
35+
"Dining Hall West": "DHW",
3736
"Engineering and Computer Science North": "ECSN",
3837
"Engineering and Computer Science South": "ECSS",
3938
"Engineering and Computer Science West": "ECSW",
@@ -75,4 +74,197 @@ var buildingAbbreviations = map[string]string{
7574
"Helix Hall": "RHH",
7675
"Sirius Hall": "RHS",
7776
"Vega Hall": "RHV",
77+
"Recreation Center West": "RCW",
78+
"SP/N Gallery": "SP2",
79+
}
80+
81+
// Valid building abreviations for checking
82+
var validAbbreviations []string = []string{
83+
"AB",
84+
"ACB",
85+
"AD",
86+
"APC",
87+
"ATC",
88+
"BE",
89+
"BSB",
90+
"CB",
91+
"CR",
92+
"CRA",
93+
"DGA",
94+
"DHW",
95+
"ECSN",
96+
"ECSS",
97+
"ECSW",
98+
"EP",
99+
"FA",
100+
"FM",
101+
"FN",
102+
"FO",
103+
"GR",
104+
"HH",
105+
"JO",
106+
"JSOM",
107+
"MC",
108+
"ML1",
109+
"ML2",
110+
"NB",
111+
"NL",
112+
"PD",
113+
"PHA",
114+
"PHY",
115+
"RL",
116+
"ROC",
117+
"ROW",
118+
"SB",
119+
"SCI",
120+
"SG",
121+
"SLC",
122+
"SSA",
123+
"SSB",
124+
"SU",
125+
"SUFC",
126+
"SPN",
127+
"SP2",
128+
"TH",
129+
"VC",
130+
"WSTC",
131+
"RHA",
132+
"RHC",
133+
"RHH",
134+
"RHS",
135+
"RHV",
136+
"RCW",
78137
}
138+
139+
// Some events refer to the room name instead of their number
140+
// It's very likely that there are other named rooms with room numbers not added yet
141+
// Maps room names to room number
142+
var roomNumbers = map[string]string {
143+
"Artemis I": "2.905A",
144+
"Artemis II": "2.905B",
145+
"Main Gym": "1.2",
146+
"Auxiliary Gym": "1.318",
147+
"Axxess Atrium": "1.100",
148+
"Ballroom A": "1.102A",
149+
"Ballroom B": "1.102B",
150+
"Ballroom C": "1.102C",
151+
"AHT Gallery": "3.102",
152+
"SP/N Gallery": "11.150",
153+
"Galaxy Rooms": "2.602",
154+
"ATC Auditorium": "1.102",
155+
"ATC Lecture Hall": "l.l02",
156+
"TI Auditorium": "2.102",
157+
"SSA Auditorium": "13.330",
158+
"Clark Auditorium": "1.315",
159+
"ATC Lobby": "1.700",
160+
}
161+
162+
func ParseCalendar(inDir string, outDir string) {
163+
164+
calendarFile, err := os.ReadFile(inDir + "/events.json")
165+
if err != nil {
166+
panic(err)
167+
}
168+
169+
var allEvents []schema.Event
170+
171+
err = json.Unmarshal(calendarFile, &allEvents)
172+
if err != nil {
173+
panic(err)
174+
}
175+
176+
multiBuildingMap := make(map[string]map[string]map[string][]schema.Event)
177+
178+
for _, event := range(allEvents) {
179+
180+
// Get date
181+
dateTime := event.StartTime
182+
dateTimeString := dateTime.String()
183+
date := dateTimeString[:10]
184+
185+
// Get building and room
186+
location := utils.ConvertFromInterface[string](event.Location)
187+
188+
// Regexp to match building abbreviations and room numbers
189+
buildingRegexp := regexp.MustCompile(`[A-Z]{2,4}`)
190+
roomRegexp := regexp.MustCompile(`([0-9]{1,2}\.[0-9]{3})([A-Z])?`)
191+
192+
building := buildingRegexp.FindString(*location)
193+
room := roomRegexp.FindString(*location)
194+
195+
// buildingRegexp might capture something that isn't a valid building abbreviation (e.g., UTD)
196+
if checkBuilding := slices.Contains(validAbbreviations, building); !checkBuilding {
197+
building = ""
198+
}
199+
200+
lowercaseLocation := strings.ToLower(*location)
201+
// If location doesn't have building abbreviation, check for the full building name
202+
if building == "" {
203+
for key := range buildingAbbreviations {
204+
if strings.Contains(lowercaseLocation, strings.ToLower(key)) {
205+
building = buildingAbbreviations[key]
206+
}
207+
}
208+
}
209+
210+
// If location doesn't have room number, check for room names
211+
if room == "" {
212+
for key := range roomNumbers {
213+
if strings.Contains(lowercaseLocation, strings.ToLower(key)) {
214+
room = roomNumbers[key]
215+
}
216+
}
217+
}
218+
219+
// If building is still empty string, then location was initally an empty string
220+
// or was a place off campus
221+
if building == "" {
222+
building = "Other"
223+
}
224+
225+
// If room is still empty string, then location was initally an empty string, or
226+
// the room had no equivalent room number, or was a place off campus
227+
if room == "" {
228+
room = "Other"
229+
}
230+
231+
if _, exists := multiBuildingMap[date]; !exists {
232+
multiBuildingMap[date] = make(map[string]map[string][]schema.Event)
233+
}
234+
235+
if _, exists := multiBuildingMap[date][building]; !exists {
236+
multiBuildingMap[date][building] = make(map[string][]schema.Event)
237+
}
238+
239+
multiBuildingMap[date][building][room] = append(multiBuildingMap[date][building][room], event)
240+
}
241+
242+
var result []schema.MultiBuildingEvents[schema.Event]
243+
244+
for date, buildings := range multiBuildingMap {
245+
var singleBuildings []schema.SingleBuildingEvents[schema.Event]
246+
for building, rooms := range buildings {
247+
var roomEvents []schema.RoomEvents[schema.Event]
248+
for room, events := range rooms {
249+
roomEvents = append(roomEvents, schema.RoomEvents[schema.Event]{
250+
Room: room,
251+
Events: events,
252+
})
253+
}
254+
255+
singleBuildings = append(singleBuildings, schema.SingleBuildingEvents[schema.Event]{
256+
Building: building,
257+
Rooms: roomEvents,
258+
})
259+
}
260+
261+
result = append(result, schema.MultiBuildingEvents[schema.Event]{
262+
Date: date,
263+
Buildings: singleBuildings,
264+
})
265+
}
266+
267+
log.Print("Parsed Calendar!")
268+
269+
utils.WriteJSON(fmt.Sprintf("%s/eventsTEST.json", outDir), result)
270+
}

0 commit comments

Comments
 (0)