ErikBoesen / schoolopy

:school: Python wrapper for Schoology's REST API.
MIT License
47 stars 19 forks source link

TypeError: string indices must be integers #7

Closed fjmoon1 closed 6 years ago

fjmoon1 commented 6 years ago

I am attempting to pull User Actions from the Schoology API using the Schoolopy wrapper. When I call get_user_actions(), I get: TypeError: indices must be integers. Also, I am able to Authenticate with my key and secret but I did not list them in the following code snippet:

import schoolopy import time import datetime import json

sc = schoolopy.Schoology(schoolopy.Auth('xxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxx')) sc.limit = 10 # Only retrieve 10 objects max

d = datetime.date(2018,2,6) unixtime = time.mktime(d.timetuple())

print(time.time())

print(unixtime)

print('get_me: ' + sc.get_me()['uid'])

z = ''

start_time = int(time.time() - 100000) end_time = int(time.time())

print('Start_time: ' + str(start_time)) print('End_time: ' + str(end_time))

for x in sc.get_users(): print(x['uid']) y = int(x['uid']) print(sc.get_user_actions(y, int(start_time), int(end_time)))

ErikBoesen commented 6 years ago

Can I get a full stack trace for this error? Just the title of the error doesn't tell me very much about where it came from.

rmduddy commented 6 years ago

@ErikBoesen I took a look at this one too... it is similar to the other, but needs a bit more. It's at 2070 in main. The return for the get_user_actions function.

return [Action(raw) for raw in self._get('analytics/users/%s?start_time=%s&end_time=%s' % (user_id, start, end))['actions']]

I was going to take a crack at it, but I'm tired and my brain isn't working. Two additional, optional parameters should be added to the function. It needs the "&start=" added to the endpoint like in the other (0 should be the default if not specified by the user), and a "&limit=" could also be added after, it seems to default to 20 on it's own if nothing is specified.

There's also an issue with the code in the return itself... Even with the additional parameters there is an error from Action not being defined. I'm thinking you wanted the return to be human readable line by line list of actions? I couldn't think of a clean way to do that in the return (my own limitations I'm sure), if I just remove that portion and the "for" portion as well and leave:

return self._get('analytics/users/%s?start_time=%s&end_time=%s&start=0' % (user_id, start, end))['actions']

I return an object that looks like this:

[{'action': 'course/1009559402/materials/assignments/1415370487', 'time': 1518038367, 'duration': 263}, {'action': 'course/1009559402/materials/assignments/1415370487', 'time': 1518038630, 'duration': 82}, {'action': 'course/1009559436/materials/assignments/1416891099', 'time': 1518038712, 'duration': 43}, {'action': 'template/582459411', 'time': 1518038755, 'duration': 14}, {'action': 'course/1009559436/materials/assignments/1416891083', 'time': 1518038769, 'duration': 220}, {'action': 'course/1009559436/materials/assignments/1416891333', 'time': 1518038989, 'duration': 240}, {'action': 'course/1009559476/materials/assignments/1415685123', 'time': 1518039229, 'duration': 0}]

Maybe this info helps, if I don't get a ton of other things dropped on me tomorrow I'll try to give it a go again.

AndrewLester commented 6 years ago

@rmduddy One of the issues with it might be that there is no Action model inside of the models file. This is probably what's causing the error with Action not being defined.

ErikBoesen commented 6 years ago

Uh, yes, that would seem to be a problem!

ErikBoesen commented 6 years ago

Anyone else want to make that fix or shall I?

AndrewLester commented 6 years ago

I got it. But for future testing how will we fix things that require higher level access? Because whenever we try to get actions it just returns nothing, and therefore we can't find bugs like this one.

ErikBoesen commented 6 years ago

I don't really think there's a way to do that, unfortunately. If Schoology was open-source, we could host our own copy, but it's sadly not.

AndrewLester commented 6 years ago

@ErikBoesen Should I add a global start variable to be used in requests which take it? (Much like the limit one we currently have)

ErikBoesen commented 6 years ago

Sure, feel free.