forked from DS-100/overall-score
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_client_2.py
92 lines (76 loc) · 3.4 KB
/
api_client_2.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
#!/usr/bin/env python3
import numpy as np
import pandas as pd
import json
import requests
import getpass
BASE_URL = 'https://www.gradescope.com'
COURSE_ID = 292247 # CHANGE THIS
ASSIGNMENT_ID = 1573263 # CHANGE THIS
class APIClient:
def __init__(self):
self.session = requests.Session()
def post(self, *args, **kwargs):
return self.session.post(*args, **kwargs)
def log_in(self, email, password):
url = "{base}/api/v1/user_session".format(base=BASE_URL)
form_data = {
"email": email,
"password": password
}
r = self.post(url, data=form_data)
print(r)
self.token = r.json()['token']
def upload_pdf_submission(self, course_id, assignment_id, student_email, filename):
url = "{base}/api/v1/courses/{0}/assignments/{1}/submissions".format(
course_id, assignment_id, base=BASE_URL
)
form_data = {
"owner_email": student_email
}
files = {'pdf_attachment': open(filename, 'rb')}
request_headers = {'access-token': self.token}
r = self.post(url, data=form_data, headers=request_headers, files=files)
return r
def replace_pdf_submission(self, course_id, assignment_id, student_email, filename):
url = "{base}/api/v1/courses/{0}/assignments/{1}/submissions/replace_pdf".format(
course_id, assignment_id, base=BASE_URL
)
form_data = {
"owner_email": student_email
}
files = {'pdf_attachment': open(filename, 'rb')}
request_headers = {'access-token': self.token}
r = self.post(url, data=form_data, headers=request_headers, files=files)
return r
def upload_programming_submission(self, course_id, assignment_id, student_email, filenames):
url = "{base}/api/v1/courses/{0}/assignments/{1}/submissions".format(
course_id, assignment_id, base=BASE_URL
)
form_data = {
"owner_email": student_email
}
files = [('files[]', (filename, open(filename, 'rb'))) for filename in filenames]
request_headers = {'access-token': self.token}
r = self.post(url, data=form_data, headers=request_headers, files=files)
return r
if __name__ == '__main__':
client = APIClient()
email = input("Please provide the email address on your Gradescope account: ")
password = getpass.getpass('Password: ')
client.log_in(email, password)
grades = pd.read_csv('grades.csv')
for i in grades.index:
if np.isnan(grades.loc[i, 'SID']):
continue
sid = {'SID' : int(grades.loc[i, 'SID'])}
with open('SID.json', 'w') as f:
f.write(json.dumps(sid))
print(grades.loc[i, 'Email Address'], int(grades.loc[i, 'SID']))
client.upload_programming_submission(COURSE_ID, ASSIGNMENT_ID, grades.loc[i, 'Email Address'], ['SID.json'])
# Use the APIClient to upload submissions after logging in, e.g:
# client.upload_pdf_submission(1234, 5678, 'student@example.edu', 'submission.pdf')
# client.upload_programming_submission(1234, 5678, 'student@example.edu', ['README.md', 'src/calculator.py'])
# You can get course and assignment IDs from the URL, e.g.
# https://www.gradescope.com/courses/1234/assignments/5678
# course_id = 1234, assignment_id = 5678