-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
107 lines (79 loc) · 3.56 KB
/
api.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import requests
import urllib.parse as urlparse
class Learn(object):
WWW_ROOT = "http://learn.canterbury.ac.nz/webservice/rest/server.php"
def __init__(self, username=None, password=None, token=None):
if token:
self.token = token # TODO : Check token if invalid
else:
self.token = Learn.get_token(username, password)
self.userid = self.get_userid()
@staticmethod
def get_token(username, password):
r = requests.post(
"http://learn.canterbury.ac.nz/login/token.php",
params={"username": username,
"password": password,
"service": "moodle_mobile_app"
}).json()
if "error" in r:
raise PermissionError(r['error'])
elif "token" in r:
return r['token']
else:
raise NotImplementedError(r)
def make_request(self, function, **kwargs):
params = {
"wstoken": self.token,
"wsfunction": function,
"moodlewsrestformat": "json"
}
params.update(kwargs)
return requests.get(Learn.WWW_ROOT, params=params).json()
def get_userid(self):
return self.make_request("core_webservice_get_site_info")['userid']
def get_course_list(self):
return self.make_request("core_enrol_get_users_courses", userid=self.userid)
def all_files(self):
for course in self.get_course_list():
for file in self.get_course_files(course['id']):
file['course_name'] = course['shortname']
yield file
for file in self.get_course_resources(course['id']):
file['course_name'] = course['shortname']
yield file
def get_course_files(self, courseid):
""" Sorry """
sections = self.make_request("core_course_get_contents", courseid=courseid)
for section in sections:
if section["name"] != "Section":
for module in section["modules"]:
if "contents" in module:
for content in module["contents"]:
if content["type"] == "file":
yield {
"url": inject_param(content["fileurl"], token=self.token),
"section_name": section["name"],
"name": module["name"],
"filename": content["filename"],
"filesize": content["filesize"],
"timemodified": content["timemodified"]
}
def get_course_resources(self, courseid):
courses = self.make_request("mod_resource_get_resources_by_courses", **{"courseids[0]": courseid})['resources']
for course in courses:
for content in course['contentfiles']:
yield {
"url": inject_param(content["fileurl"], token=self.token),
"section_name": course["name"],
"name": content["filename"],
"filename": content["filename"],
"filesize": content["filesize"],
"timemodified": content["timemodified"]
}
def inject_param(url, **kwargs):
url_parts = list(urlparse.urlparse(url))
query = dict(urlparse.parse_qsl(url_parts[4]))
query.update(kwargs)
url_parts[4] = urlparse.urlencode(query)
return urlparse.urlunparse(url_parts)