-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from YaleComputerSociety/coursetable_scraper
create basic coursetable scraper
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
|
||
|
||
|
||
|
||
|