-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrefresh.py
144 lines (115 loc) · 4.12 KB
/
refresh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import firebase_admin
from firebase_admin import credentials, db, firestore
import os
import requests
import json
from time import sleep
import datetime
#constants
baseSubjectsURI = "https://sis.rutgers.edu/oldsoc/subjects.json"
baseCoursesURI = "http://sis.rutgers.edu/oldsoc/courses.json"
seasonInt = {
'winter': 0,
'spring': 1,
'summer': 7,
'fall': 9
}
intSeason = {
0: 'winter',
1: 'spring',
7: 'summer',
9: 'fall'
}
# year = datetime.datetime.now().year
# credentials
cred = credentials.Certificate('/root/rct-backend/rutgers-course-tracker-firebase-adminsdk-7pvr2-00983f5cf0.json')
# /root/rct-backend/refresh.py
# initialize the firebase admin app with a database URL
app = firebase_admin.initialize_app(cred, {
'databaseURL': 'https://rutgers-course-tracker.firebaseio.com/'
})
# Helper functions
## Get Semeseter from file
def getSemesterFromFile():
return
## Cleaner wrap of using map function
def returnValueDictionary(indexMap: dict, value: str) -> dict:
return dict(map(lambda kv: (kv[0], kv[1][value]), indexMap.items()))
# main function for updating firestore
def firestoreCourseData(db, season: int, year: int):
#https://sis.rutgers.edu/oldsoc/subjects.json?semester=92020&campus=NB&level=U
# Checks for spring semester and changes year accordingly
isSpring = season == 1
currentYear = year
# Constructs the URI
requestURI = f'{baseSubjectsURI}?semester={season}{currentYear}&campus=NB&level=U'
print("Requesting URI:", requestURI)
# Sends a request to the URI
res = None
try:
res = requests.get(requestURI)
except:
print("SOC API Connection error.")
return
# Loads in the JSON data into a object
subjects = json.loads(res.text)
# Final map contain relevent data from seach sections
indexMap = {}
# Going through the subjects to get request all the courses in that subject
for x in subjects:
code = x['code']
requestCoursesURI = f'{baseCoursesURI}?subject={code}&semester={season}{currentYear}&campus=NB&level=U'
try:
res = requests.get(requestCoursesURI)
except:
print("SOC API Connection error.")
continue
courses = json.loads(res.text)
# Going through the courses in the subject
for course in courses:
sections = course['sections']
title = course['title']
subject = course['subject']
courseNumber = course['courseNumber']
# going through the sections in the course
for section in sections:
index = section['index']
sectionNumber = section['number']
indexMap[index] = {
'subject': subject,
'name': title,
'section': sectionNumber,
'course': courseNumber
}
updateFirestore(indexMap, intSeason[season])
# Method that takes in a map of sections values and updates firestore
def updateFirestore(indexMap: dict, season: str):
db = firestore.client()
seasonColl = db.collection(season)
seasonColl.document("sections").set({
'sections' : returnValueDictionary(indexMap, 'section')
})
seasonColl.document("names").set({
'names' : returnValueDictionary(indexMap, 'name')
})
seasonColl.document("subjects").set({
'subjects' : returnValueDictionary(indexMap, 'subject')
})
seasonColl.document("courses").set({
'courses' : returnValueDictionary(indexMap, 'course')
})
# Entry method
def enter():
# Gets current main semester from the text file
f = open("/root/rct-backend/season.txt", "r")# /root/rct-backend/season.txt
fileBuffer = f.readlines()
sem = fileBuffer[0].rsplit('\n')[0]
year = int(fileBuffer[1].rsplit('\n')[0])
f.close()
# Sets the seasons according to the int value
seasons = [9, 7] if sem == 'fall' else [1, 0];
# Starts the process of going to SOC and setting data in firestore
for season in seasons:
firestoreCourseData(firestore.client(), season, year)
# Start script
enter()