-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKUPython.py
More file actions
93 lines (77 loc) · 3.46 KB
/
Copy pathKUPython.py
File metadata and controls
93 lines (77 loc) · 3.46 KB
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
#########################################################################
# #
# KUPython is a module intended to help building #
# scripts for KU's Absalon in Python #
# #
# In order for KUPython to work Requests need to be #
# installed. #
# #
# link to install Requests: #
# http://docs.python-requests.org/en/latest/user/installed #
# #
# #
# #
# #
#########################################################################
import requests
import re
class absalonSession:
""" A class to begin a user session on KU's studentnet Absalon
as well as providing some basic navigation on the site """
def __init__(self,
username,
password,
curl = 'Z2F',
flags = 0,
forcedownlevel = 0,
formdir = 6):
# assign parameters to POST to server
self.params = {
'username' : username,
'password' : password,
'curl' : curl,
'flags' : flags,
'forcedownlevel' : forcedownlevel,
'formdir' : formdir
}
# assign a request session object to self
self.session = requests.Session()
# start user session
tmp = self.session.post("https://pabsws.ku.dk/CookieAuth.dll?Logon", data=self.params)
# self.courses is a list of course IDS for the current user
self.courses = re.findall('(?<=CourseID=)[0-9]+', tmp.text.encode('utf-8'))
# Get a list of coursefolders IDs. One from each course
self.coursefolders = [self.coursefolder(courseID) for courseID in self.courses]
def coursefolder(self,
courseID, navigate=True):
""" Given a course ID coursefolder returns the course folder as a requests.response object
by default. Returns the Folder ID if navigate is set to False"""
#bind parameters
params = {'CourseID' : courseID}
# execute GET
page = self.session.request('GET', "https://absalon.itslearning.com/main.aspx", params=params).text.encode('utf-8')
#search for the course Folder ID
folderID = re.search('(?<=FolderID=)[0-9]+', page).group()
if navigate == False:
return folderID
else:
# and bind it to params
params = { 'FolderID' : folderID }
folder = self.session.request('GET', 'https://absalon.itslearning.com/Folder/processfolder.aspx', params)
return folder
def dump(page, responseobject=True, override=True, file='dump.html'):
""" Dump page or text to a file
the page parameter specifies what is to be written to the file
if what is to be dumped is not a responseobject then pass responseobject=False
by default dump() writes to 'dump.html' pass any other filename to dump to another
folder.
override can be set to False to not override what is already in file """
if override == True:
dump = open('dump.html', 'w')
else:
dump = open('dump.html', 'a')
if responseobject == True:
dump.write(page.text.encode('utf-8'))
else:
dump.write(page)
dump.close()