1
- from urllib .parse import urlencode
2
- from urllib .request import urlopen
3
- import pickle
4
1
import json
2
+ import os
3
+ import pickle
5
4
from collections import OrderedDict
5
+
6
6
import numpy as np
7
- import os
7
+ import requests
8
8
9
9
10
10
class SubmissionBase :
11
-
12
- submit_url = 'https://www-origin.coursera.org/api/' \
13
- 'onDemandProgrammingImmediateFormSubmissions.v1'
11
+ submit_url = 'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1?includes=evaluation'
14
12
save_file = 'token.pkl'
15
13
16
- def __init__ (self , assignment_slug , part_names ):
14
+ def __init__ (self , assignment_slug , assignment_key , part_names , part_names_key ):
17
15
self .assignment_slug = assignment_slug
16
+ self .assignment_key = assignment_key
18
17
self .part_names = part_names
18
+ self .part_names_key = part_names_key
19
19
self .login = None
20
20
self .token = None
21
21
self .functions = OrderedDict ()
@@ -28,24 +28,25 @@ def grade(self):
28
28
# Evaluate the different parts of exercise
29
29
parts = OrderedDict ()
30
30
for part_id , result in self :
31
- parts [str ( part_id ) ] = {'output' : sprintf ('%0.5f ' , result )}
32
- result , response = self .request (parts )
31
+ parts [self . part_names_key [ part_id - 1 ] ] = {'output' : sprintf ('%0.5f ' , result )}
32
+ response = self .request (parts )
33
33
response = json .loads (response .decode ("utf-8" ))
34
34
35
35
# if an error was returned, print it and stop
36
- if 'errorMessage ' in response :
37
- print (response ['errorMessage ' ])
36
+ if 'errorCode ' in response :
37
+ print (response ['message' ], response [ 'details' ][ 'learnerMessage ' ])
38
38
return
39
39
40
40
# Print the grading table
41
41
print ('%43s | %9s | %-s' % ('Part Name' , 'Score' , 'Feedback' ))
42
42
print ('%43s | %9s | %-s' % ('---------' , '-----' , '--------' ))
43
- for part in parts :
44
- part_feedback = response ['partFeedbacks' ][part ]
45
- part_evaluation = response ['partEvaluations' ][part ]
43
+ for index , part in enumerate (parts ):
44
+ part_feedback = response ['linked' ]['onDemandProgrammingScriptEvaluations.v1' ][0 ]['parts' ][str (part )][
45
+ 'feedback' ]
46
+ part_evaluation = response ['linked' ]['onDemandProgrammingScriptEvaluations.v1' ][0 ]['parts' ][str (part )]
46
47
score = '%d / %3d' % (part_evaluation ['score' ], part_evaluation ['maxScore' ])
47
- print ('%43s | %9s | %-s' % (self .part_names [int (part ) - 1 ], score , part_feedback ))
48
- evaluation = response ['evaluation' ]
48
+ print ('%43s | %9s | %-s' % (self .part_names [int (index ) - 1 ], score , part_feedback ))
49
+ evaluation = response ['linked' ][ 'onDemandProgrammingScriptEvaluations.v1' ][ 0 ]
49
50
total_score = '%d / %d' % (evaluation ['score' ], evaluation ['maxScore' ])
50
51
print (' --------------------------------' )
51
52
print ('%43s | %9s | %-s\n ' % (' ' , total_score , ' ' ))
@@ -71,18 +72,15 @@ def login_prompt(self):
71
72
pickle .dump ((self .login , self .token ), f )
72
73
73
74
def request (self , parts ):
74
- params = {
75
- 'assignmentSlug' : self .assignment_slug ,
75
+ payload = {
76
+ 'assignmentKey' : self .assignment_key ,
77
+ 'submitterEmail' : self .login ,
76
78
'secret' : self .token ,
77
- 'parts' : parts ,
78
- 'submitterEmail' : self .login }
79
-
80
- params = urlencode ({'jsonBody' : json .dumps (params )}).encode ("utf-8" )
81
- f = urlopen (self .submit_url , params )
82
- try :
83
- return 0 , f .read ()
84
- finally :
85
- f .close ()
79
+ 'parts' : dict (eval (str (parts )))}
80
+ headers = {}
81
+
82
+ r = requests .post (self .submit_url , data = json .dumps (payload ), headers = headers )
83
+ return r .content
86
84
87
85
def __iter__ (self ):
88
86
for part_id in self .functions :
0 commit comments