-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
165 lines (144 loc) · 6.38 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python
import re
import mechanize
import urllib, os
#from BeautifulSoup import BeautifulSoup
import math, random, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Sync(QThread):
def __init__(self, parent = None):
QThread.__init__(self, parent)
self.auth_set = False
self.logged_in = False
self.br = mechanize.Browser()
self.courses = {}
self.br.set_handle_robots(False)
self.br.addheaders = [('User-Agent', 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)')]
def loginCredentials(self, username, password):
self.username = username
self.password = password
self.method = "loginCredentials"
print "starting thread"
self.start()
def loginCredentialsRun(self):
username = self.username
password = self.password
self.auth_set = True
try:
self.login()
if self.logged_in:
self.emit(SIGNAL("login_status(QString)"), "Logged In")
else:
self.emit(SIGNAL("login_status(QString)"), "Login Failed")
except:
self.emit(SIGNAL("login_status(QString)"), "Error")
def login(self):
self.br.open("http://moodle.iitb.ac.in")
print self.br.title()
assert self.br.viewing_html()
self.br.select_form(nr=1)
for i in range(3):
self.br.form.controls[i].readonly = False
self.br["username"] = self.username
self.br["password"] = self.password
self.br.submit()
# Title of login page is "Login to Moodle"
# If no Login in title => We are not on login page
# Assuming we landed inside moodle
if not "Login" in self.br.title():
self.logged_in = True
print "Logged In"
assert self.br.viewing_html()
def listCourses(self):
self.method = "listCourses"
self.start()
def listCoursesRun(self):
check = ('title', 'Click to enter this course')
for link in self.br.links():
if check in link.attrs:
text = link.text.split(':')
self.courses[text[0].strip()] = link
self.emit(SIGNAL("courses(PyQt_PyObject)"), self.courses.keys())
def syncCourses(self, storedCourses, folderNames, getpdf):
self.storedCourses = storedCourses
self.folderNames = folderNames
self.getpdf = getpdf
self.method = "syncCourses"
self.start()
def syncCoursesRun(self):
for course in self.storedCourses:
self.br.open(self.courses[course].url)
assert self.br.viewing_html()
allFiles = {}
links=self.br.links()
for link in links:
if "mod/resource/view" in link.url:
name=link.text.replace('[IMG]','').replace('File ','').replace(' File','')
allFiles[name] = link.url
if "mod/folder" in link.url:
dirName=link.text.replace('[IMG]','').replace('Folder ','')
self.br.open(link.url)
links2=self.br.links()
for link2 in links2:
if "mod_folder" in link2.url:
name=link2.text.replace('[IMG]','').replace('File ','') #.rsplit('.',2)[0]
name= name[0:len(name)/2]
allFiles[name]=link2.url+"@"+dirName
if(not(os.path.isdir(course))):
os.mkdir(course)
storedFiles=os.listdir(self.folderNames[course])
for i in xrange(0,len(storedFiles)):
storedFiles[i] = storedFiles[i].rsplit('.',1)[0].strip()
for afile in allFiles:
flag=False
storelocation=[]
if afile.split()[-1] == "document":
afilename=afile.rsplit(' ',2)[0].strip()
else:
afilename=afile
print [afilename]
if "mod_folder" in allFiles[afile]:
dirName=allFiles[afile].split('@')[1]
relDir=allFiles[afile].split('@')[0].split('mod_folder/content/')[1].split('/',1)[1].replace('%20',' ')
if not os.path.exists(self.folderNames[course]+'/'+dirName):
os.makedirs(self.folderNames[course]+'/'+dirName)
output=open(self.folderNames[course]+'/'+dirName+'/.synced','w')
output.write('')
output.close()
for line in open(self.folderNames[course]+'/'+dirName+'/.synced','r').readlines():
if relDir in line:
flag=True
if not(flag):
output=open(self.folderNames[course]+'/'+dirName+'/.synced','a')
output.write(relDir+'\n')
output.close()
d = os.path.dirname(self.folderNames[course]+'/'+dirName+"/"+relDir)
if not os.path.exists(d):
os.makedirs(d)
storelocation=(dirName+'/'+relDir).rsplit('.',2)[0].split("/")
afilename=storelocation[len(storelocation)-1].split(".")[0]
storelocation=storelocation[0:len(storelocation)-1]
if (not((afilename in storedFiles) or flag)):
response=self.br.open(allFiles[afile])
if(self.getpdf):
if (response.info()["Content-type"].split(";")[0]=="text/html"):
for link in self.br.links():
if (link.url.find("pluginfile.php")!=-1):
response=self.br.open(link.url)
extension=response.geturl().rsplit('.',1)[1].split('?')[0]
save_path=os.path.join(*([self.folderNames[course]]+storelocation+[afilename+'.'+extension]))
output=open(save_path,'w')
output.write(response.read())
output.close()
self.emit(SIGNAL("sync_courses(QString)"), "done")
def run(self):
if self.method == "listCourses":
self.listCoursesRun()
elif self.method == "syncCourses":
self.syncCoursesRun()
elif self.method == "loginCredentials":
self.loginCredentialsRun()
def __del__(self):
self.exiting = True
self.wait()