-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
84 lines (65 loc) · 2.75 KB
/
views.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
from django.shortcuts import render_to_response
from django.http import HttpResponse
import urllib2
import urllib
import json
#LAPIKey
#Token
def login(request):
return render_to_response('Templates2/login.html')
def main(request):
if ('key' in request.GET) and ('token' in request.GET):
LAPIKey= request.GET['key']
Token = request.GET['token']
username= getUserName(LAPIKey,Token)
modules = getModules(LAPIKey,Token)
parsedm=parseModule(modules)
resultm= json.dumps(parsedm)
forums = {}
for module in parsedm:
courseID=module["ID"]
forum = getForum(LAPIKey, Token, courseID)
#forums.update(module)
#forums.update(forum)
forums[courseID] = forum
forums["num"] = len(parsedm)
forums_json=json.dumps(forums)
return render_to_response('Templates/main.html',{'username':username, 'modules':resultm,'forums':forums_json})
else:
return HttpResponse("Not enough data")
def rmain(request):
return render_to_response('Templates/main.html')
def getUserName(LAPIKey,Token):
getUsernameURL = "https://ivle.nus.edu.sg/api/Lapi.svc/UserName_Get?APIKey=%s&Token=%s&output=json" % (LAPIKey,Token)
content = urllib2.urlopen(getUsernameURL)
name_json_raw = content.read()
name = json.loads(name_json_raw)
return name
def getModules(LAPIKey,Token):
getModulesURL="https://ivle.nus.edu.sg/api/Lapi.svc/Modules?APIKey=%s&AuthToken=%s&Duration=0&IncludeAllInfo=false&output=json"%(LAPIKey, Token)
content = urllib2.urlopen(getModulesURL)
modules_json_raw = content.read()
modules = json.loads(modules_json_raw)
return modules
def getForum(LAPIKey,Token,CourseID):
getForumURL="https://ivle.nus.edu.sg/api/Lapi.svc/Forums?APIKey=%s&AuthToken=%s&CourseID=%s&Duration=0&IncludeThreads=true&TitleOnly=false&output=json"%(LAPIKey,Token,CourseID)
content = urllib2.urlopen(getForumURL)
forum_json_raw=content.read()
forum = json.loads(forum_json_raw)
return forum_json_raw #forum
def postNewThread(LAPIKey,Token,HeadingID, Title, Body):
data = urllib.urlencode({'APIKey':'%s' %(LAPIKey),'AuthToken':'%s'%(Token),'HeadingID':'%s' %(HeadingID),'Title':'%s'%(Title),'Reply':'%s'%(Body)})
u = urllib2.urlopen("https://ivle.nus.edu.sg/api/Lapi.svc/Forum_PostNewThread_JSON",data)
return u.read()
def replyNewThread(LAPIKey,Token,ThreadID,Title, Body):
data = urllib.urlencode({'APIKey':'%s' %(LAPIKey),'AuthToken':'%s'%(Token),'ThreadID':'%s' %(ThreadID),'Title':'%s'%(Title),'Reply':'%s'%(Body)})
u = urllib2.urlopen("https://ivle.nus.edu.sg/api/Lapi.svc/Forum_ReplyThread_JSON",data)
return u.read()
def parseModule(module):
i=0
parsed_modules = []
for result in module["Results"]:
parsed_module={"CourseCode":result["CourseCode"], "ID":result["ID"]}
#bla = json.dumps(parsed_module)
parsed_modules.append(parsed_module)
return parsed_modules