Skip to content

Commit

Permalink
Merge pull request #8 from YaleComputerSociety/coursetable_scraper
Browse files Browse the repository at this point in the history
create basic coursetable scraper
  • Loading branch information
OzGitelson authored Nov 12, 2023
2 parents dfd1278 + dad50f1 commit 5eb72ae
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
34 changes: 34 additions & 0 deletions backend/coursetable_scraper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import requests
import json
import datetime
import time


#TODO change these to env variables
cookies = {
'session': 'enter session',
'session.sig': 'etner session.sig',
}

# response = requests.get('https://api.coursetable.com/api/static/catalogs/202301.json', cookies=cookies)

course_dic = {}
for year in range(datetime.datetime.now().year-6, datetime.datetime.now().year + 6 + 1):
for season in range(1, 4):
if year not in course_dic:
course_dic[year]={}

data_url = f'https://api.coursetable.com/api/static/catalogs/{year}0{season}.json'
response = requests.get(data_url, cookies=cookies)

if response.status_code == 404:
print(f'unable to access {year} {season}')
continue
else:
print(f'scraping {year} {season}')

course_dic[year][season] = json.loads(response.text)
time.sleep(1)

with open('courses.json', 'w') as infile:
json.dump(course_dic, infile)
56 changes: 56 additions & 0 deletions backend/display_courses.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import json
import argparse


with open('courses.json', 'r') as infile:
courses=json.load(infile)
new_courses={}
for year in courses:
if year not in new_courses:
new_courses[year] = {}

for season in courses[year]:
if season not in new_courses[year]:
new_courses[year][season] = {}

for course in courses[year][season]:
for code in course["all_course_codes"]:
dep=code.split(' ')[0]
num = str(code.split(' ')[1])
if dep not in new_courses[year][season]:
new_courses[year][season][dep]={}

new_courses[year][season][dep][num]=course

courses=new_courses

with open

while True:
request=input('enter course:\n')
request=request.split(' ')
if len(request)==2:
year='2023'
season='3'
dep = str(request[0]).upper()
c_num = str(request[1])
else:
year=str(request[0])
season=str(request[1])
dep=str(request[2]).upper()
c_num=str(request[3])

try:
course=courses[year][season][dep][c_num]
print(f'{course["title"]}:')
print(f'{course["description"]}\n')
print(f'rating: {course["average_rating"]}')
print(f'difficulty: {course["average_workload"]}\n')

except:
print('invalid course')





0 comments on commit 5eb72ae

Please sign in to comment.