Skip to content

Commit e289632

Browse files
committed
Dynamical retrieval working
1 parent 9a0c058 commit e289632

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

parser/cometCalendarParser.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"regexp"
1313
"slices"
1414
"strings"
15+
"time"
1516

17+
"github.com/UTDNebula/api-tools/scrapers"
1618
"github.com/UTDNebula/api-tools/utils"
1719
"github.com/UTDNebula/nebula-api/api/schema"
1820
)
1921

20-
// TODO: FIND A WAY TO DYNAMICALLY RETRIEVE THIS
21-
2222
// Some events have only the building name, not the abbreviation
2323
// Maps building names to their abbreviations
2424
var DefaultBuildings = map[string]string{
@@ -152,7 +152,7 @@ func ParseCometCalendar(inDir string, outDir string) {
152152
}
153153

154154
multiBuildingMap := make(map[string]map[string]map[string][]schema.Event)
155-
buildingAbbreviations, validAbbreviations := getAbbreviations(inDir)
155+
buildingAbbreviations, validAbbreviations := getLocationAbbreviations(inDir)
156156

157157
for _, event := range allEvents {
158158

@@ -248,32 +248,45 @@ func ParseCometCalendar(inDir string, outDir string) {
248248
}
249249

250250
// getAbbreviations dynamically retrieves the all of the locations abbreviations
251-
func getAbbreviations(inDir string) (map[string]string, []string) {
251+
func getLocationAbbreviations(inDir string) (map[string]string, []string) {
252252
// Get the locations from the map scraper
253+
var mapFile []byte
254+
253255
mapFile, err := os.ReadFile(inDir + "/mapLocations.json")
254256
if err != nil {
255-
// Fall back if we haven't scraped the locations yet
256-
return DefaultBuildings, DefaultValid
257+
if os.IsNotExist(err) {
258+
// Scrape the data if the it doesn't exist yet and then get the map file
259+
scrapers.ScrapeMapLocations(inDir)
260+
time.Sleep(2 * time.Second)
261+
ParseMapLocations(inDir, inDir)
262+
263+
// If fail to get the locations again, not because unscraped
264+
mapFile, _ = os.ReadFile(inDir + "/mapLocations.json")
265+
} else {
266+
panic(err)
267+
}
257268
}
258-
var locations []map[string]any
269+
var locations []schema.MapBuilding
259270
if err = json.Unmarshal(mapFile, &locations); err != nil {
260271
panic(err)
261272
}
262273

263274
// Process the abbreviations
264-
buildingsAbbrs := make(map[string]string, 0)
265-
validAbbrs := make([]string, 0)
275+
buildingsAbbreviations := make(map[string]string, 0)
276+
validAbbreviations := make([]string, 0)
266277

267278
for _, location := range locations {
268-
name := *utils.ConvertFromInterface[string](location["name"])
269-
acronym := *utils.ConvertFromInterface[string](location["acronym"])
270-
271-
// Trim the tailing acronym in the name
272-
trimmedName := strings.Split(name, " (")[0]
273-
buildingsAbbrs[trimmedName] = acronym
279+
// Trim the following acronym in the name
280+
trimmedName := strings.Split(*location.Name, " (")[0]
281+
// Fallback on the locations that have no acronyms
282+
acronym := ""
283+
if location.Acronym != nil {
284+
acronym = *location.Acronym
285+
}
274286

275-
validAbbrs = append(validAbbrs, acronym)
287+
buildingsAbbreviations[trimmedName] = acronym
288+
validAbbreviations = append(validAbbreviations, acronym)
276289
}
277290

278-
return buildingsAbbrs, validAbbrs
291+
return buildingsAbbreviations, validAbbreviations
279292
}

scrapers/cometCalendar.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"go.mongodb.org/mongo-driver/bson/primitive"
2020
)
2121

22-
const BASE_CAL_URL string = "https://calendar.utdallas.edu/api/2/events"
22+
const CAL_URL string = "https://calendar.utdallas.edu/api/2/events"
2323

2424
// RawEvent mirrors the nested event payload returned by the calendar API.
2525
type RawEvent struct {
@@ -44,7 +44,8 @@ func ScrapeCometCalendar(outDir string) {
4444

4545
// Get the total number of pages
4646
log.Printf("Getting the number of pages...")
47-
if err := callAPIAndUnmarshal(&client, 0, &calendarData); err != nil {
47+
48+
if err := callAndUnmarshal(&client, 0, &calendarData); err != nil {
4849
panic(err)
4950
}
5051
numPages := calendarData.Page["total"]
@@ -53,7 +54,7 @@ func ScrapeCometCalendar(outDir string) {
5354
var calendarEvents []schema.Event
5455
for page := range numPages {
5556
log.Printf("Scraping events of page %d...", page+1)
56-
if err := callAPIAndUnmarshal(&client, page+1, &calendarData); err != nil {
57+
if err := callAndUnmarshal(&client, page+1, &calendarData); err != nil {
5758
panic(err)
5859
}
5960
for _, rawEvent := range calendarData.Events {
@@ -81,20 +82,22 @@ func ScrapeCometCalendar(outDir string) {
8182
ContactPhoneNumber: contactInfo[2],
8283
})
8384
}
85+
8486
log.Printf("Scraped events of page %d successfully!\n", page+1)
8587
}
8688

8789
writePath := fmt.Sprintf("%s/cometCalendarScraped.json", outDir)
8890
if err := utils.WriteJSON(writePath, calendarEvents); err != nil {
8991
panic(err)
9092
}
93+
9194
log.Printf("Finished scraping %d events successfully!\n\n", len(calendarEvents))
9295
}
9396

9497
// scrapeAndUnmarshal fetches a calendar page and decodes it into data.
95-
func callAPIAndUnmarshal(client *http.Client, page int, data *APICalendarResponse) error {
98+
func callAndUnmarshal(client *http.Client, page int, data *APICalendarResponse) error {
9699
// Call API to get the byte data
97-
calendarUrl := fmt.Sprintf("%s?days=365&pp=100&page=%d", BASE_CAL_URL, page)
100+
calendarUrl := fmt.Sprintf("%s?days=365&pp=100&page=%d", CAL_URL, page)
98101
request, err := http.NewRequest("GET", calendarUrl, nil)
99102
if err != nil {
100103
return err
@@ -162,7 +165,7 @@ func getEventLocation(event RawEvent) string {
162165
// getFilters parses the types, topics, and target audiences
163166
func getFilters(event RawEvent) ([]string, []string, []string) {
164167
types := []string{}
165-
targetAudiences := []string{}
168+
audiences := []string{}
166169
topics := []string{}
167170

168171
filters := convert[map[string]any](event.Event["filters"])
@@ -174,15 +177,15 @@ func getFilters(event RawEvent) ([]string, []string, []string) {
174177

175178
rawAudiences := convert[[]any](filters["event_target_audience"])
176179
for _, audience := range rawAudiences {
177-
targetAudiences = append(targetAudiences, convert[string](convert[map[string]any](audience)["name"]))
180+
audiences = append(audiences, convert[string](convert[map[string]any](audience)["name"]))
178181
}
179182

180183
rawTopics := convert[[]any](filters["event_topic"])
181184
for _, topic := range rawTopics {
182185
topics = append(topics, convert[string](convert[map[string]any](topic)["name"]))
183186
}
184187

185-
return types, targetAudiences, topics
188+
return types, audiences, topics
186189
}
187190

188191
// getDepartmentsAndTags parses the departments, and tags

0 commit comments

Comments
 (0)