|
| 1 | +import requests |
| 2 | +import json |
| 3 | +from urllib.parse import urlencode, quote |
| 4 | + |
| 5 | +BASE_URL = 'https://api.planetterp.com/v1/' |
| 6 | + |
| 7 | +def course(name, reviews = False): |
| 8 | + """ |
| 9 | + Get a course. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + name: string |
| 14 | + The name of the course. |
| 15 | + reviews: bool, optional |
| 16 | + Whether to also return reviews for the course, specifically reviews for |
| 17 | + professors that taught the course and have the course listed as the one |
| 18 | + being reviewed. Defaults to False. |
| 19 | + """ |
| 20 | + params = {"name" : name, "reviews": "true" if reviews else "false"} |
| 21 | + url = BASE_URL + "course?" + urlencode(params) |
| 22 | + |
| 23 | + return requests.get(url).json() |
| 24 | + |
| 25 | + |
| 26 | +def courses(department = None, reviews = False, limit = 100, offset = 0): |
| 27 | + """ |
| 28 | + Get all courses meeting some criteria. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + department: string, optional |
| 33 | + Only return courses in the given department. The department name must be |
| 34 | + four characters. Defaults to all departments. |
| 35 | + reviews: bool, optional |
| 36 | + Also return reviews for the course, specifically reviews for |
| 37 | + professors that taught the course and have the course listed as the one |
| 38 | + being reviewed. Defaults to False. |
| 39 | + limit: int, optional |
| 40 | + Maximum number of courses to return. Must be between 1 and 1000 |
| 41 | + inclusive. Defaults to 100. |
| 42 | + offset: int, optional |
| 43 | + Number of courses to skip (offered for pagination). Defaults to 0. |
| 44 | + """ |
| 45 | + params = {"department" : department, |
| 46 | + "reviews": "true" if reviews else "false", |
| 47 | + "limit": limit, "offset": offset} |
| 48 | + |
| 49 | + # filter out None args |
| 50 | + params = {k:v for k, v in params.items() if v is not None} |
| 51 | + url = BASE_URL + "courses?" + urlencode(params) |
| 52 | + |
| 53 | + return requests.get(url).json() |
| 54 | + |
| 55 | + |
| 56 | +def professor(name, reviews = False): |
| 57 | + """ |
| 58 | + Get a professor. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + name: str |
| 63 | + The name of the professor. |
| 64 | + reviews: bool, optional |
| 65 | + Whether to also return reviews for the given professor. Defaults to |
| 66 | + false. |
| 67 | + """ |
| 68 | + params = {"name" : name, "reviews": "true" if reviews else "false"} |
| 69 | + url = BASE_URL + "professor?" + urlencode(params) |
| 70 | + |
| 71 | + return requests.get(url).json() |
| 72 | + |
| 73 | + |
| 74 | +def professors(type_ = None, reviews = False, limit = 100, offset = 0): |
| 75 | + """ |
| 76 | + Get all professors meeting some criteria. |
| 77 | +
|
| 78 | + Parameters |
| 79 | + ---------- |
| 80 | + type_: {"professor", "ta"}, optional |
| 81 | + Only return reviews for the specified instructor type, either professors |
| 82 | + or TAs. Defaults to returning both. |
| 83 | + reviews: bool, optional |
| 84 | + Whether to also return reviews for the instructors. Defaults to false. |
| 85 | + limit: int, optional |
| 86 | + Maximum number of instructors to return. Must be between 1 and 1000 |
| 87 | + inclusive. Defaults to 100. |
| 88 | + offset: int, optional |
| 89 | + Number of instructors to skip (offered for pagination). Defaults to 0. |
| 90 | + """ |
| 91 | + if type_ and type_ not in ["professor", "ta"]: |
| 92 | + raise ValueError("Expected type to be one of ['professor', 'ta'], got " |
| 93 | + + str(type_)) |
| 94 | + |
| 95 | + params = {"type" : type_, "reviews": "true" if reviews else "false", |
| 96 | + "limit": limit, "offset": offset} |
| 97 | + |
| 98 | + # filter out None args |
| 99 | + params = {k:v for k, v in params.items() if v is not None} |
| 100 | + url = BASE_URL + "professors?" + urlencode(params) |
| 101 | + |
| 102 | + return requests.get(url).json() |
| 103 | + |
| 104 | + |
| 105 | +def grades(course = None, professor = None, semester = None, section = None): |
| 106 | + """ |
| 107 | + Get grades for a given course, professor, semester, section, or combination |
| 108 | + thereof. |
| 109 | +
|
| 110 | + Parameters |
| 111 | + ---------- |
| 112 | + course: str |
| 113 | + Limit the grades returned to only this course. Defaults to all courses. |
| 114 | + Either `course` or `professor` must be passed. |
| 115 | + professor: str |
| 116 | + The name of the professor to return grades for. Defaults to all |
| 117 | + professors. Either `course` or `professor` must be passed. |
| 118 | + semester: str, optional |
| 119 | + The semester to return grades for. This should be passed as the year |
| 120 | + followed by the semester code. The spring semester code is 01 and the |
| 121 | + fall semester code is 08. For example, 202001 is Spring 2020. |
| 122 | + section: str, optional |
| 123 | + Limit the grades returned to only this section. Defaults to all |
| 124 | + sections. |
| 125 | +
|
| 126 | + Notes |
| 127 | + ----- |
| 128 | + Either `course` or `professor` must be passed. |
| 129 | + """ |
| 130 | + params = {"course" : course, "professor": professor, "semester": semester, |
| 131 | + "section": section} |
| 132 | + |
| 133 | + # filter out None args |
| 134 | + params = {k:v for k, v in params.items() if v is not None} |
| 135 | + url = BASE_URL + "grades?" + urlencode(params) |
| 136 | + |
| 137 | + return requests.get(url).json() |
0 commit comments