How to get due assignment using google classroom api?

60 Views Asked by At

I am creating a python script that get how many due assignments I have left in google classroom among all courses, but I could get direct api call from Google Classroom API.

I followed official google classroom python documentation, but I couldn't find a way to get pending assignments. Currently I am using work around.

...
courses = service.courses().courseWork().list(courseId= <my_course_id>).execute()
subm = service.courses().courseWork().studentSubmissions().list(courseId= <my_course_id>, userId='me',courseWorkId='-').execute()

for i in courses['courseWork']:
    for j in subm['studentSubmissions']:
        if i['id'] == j['courseWorkId'] and j['state'] == 'CREATED':
            print(i['title'])

It works but I need to make 2 request per course, and I have 10 courses. So, it will be too much expensive for me as I`m calling this script every 5 min. Is there any easier way to get it

1

There are 1 best solutions below

0
chronosirius On

I don't think there's a way around sending 2 requests, but you can speed it up by only requesting the fields you need (title) and set the submissionStates[] filter to submissionStates=['CREATED']. It should greatly speed up your requests.

...
courses = service.courses().courseWork().list(courseId= <your_course_id>, fields="title,id").execute()
subm = service.courses().courseWork().studentSubmissions().list(courseId= <your_course_id>, userId='me',courseWorkId='-', fields="studentSubmissions/courseWorkId", submissionStates=["CREATED"]).execute()

for i in courses['courseWork']:
    for j in subm['studentSubmissions']:
        if i['id'] == j['courseWorkId']:
            print(i['title'])

Hope that helps (also the code might be wrong, I wasn't able to test it but I think you see the point).