forked from KartikTalwar/Coursera
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoursera.py
224 lines (162 loc) · 6.84 KB
/
coursera.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import re
import sys
import os
import getpass
import mechanize
import BeautifulSoup
class Coursera:
def __init__(self, options):
self.username = options["user"]
self.password = options["pass"]
self.course = options["course"]
self.courseName = self.course
self.browser = mechanize.Browser()
self.browser.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) \
Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
self.browser.set_handle_robots(False)
def login(self, course=None):
if course is None:
course = self.course
loginurl = "https://class.coursera.org/" + course + "/auth/auth_redirector?" + \
"type=login&subtype=normal&email="
homeurl = "https://class.coursera.org/" + course + "/class/index"
try:
self.browser.open(loginurl)
self.browser.select_form(nr=0)
self.browser.form["email"] = self.username
self.browser.form["password"] = self.password
self.browser.submit()
except Exception, e:
pass
main = self.browser.open(homeurl).read()
search = re.findall('dropdown_my', main)
if len(search) == 1:
return False
return True
def getContent(self, course=None):
if course is None:
course = self.course
self.login(course)
pageurl = "https://class.coursera.org/" + course + "/lecture/index"
data = self.browser.open(pageurl).read()
html = BeautifulSoup.BeautifulSoup(data)
resp = []
lectureName = html.find('h1', {'class' : 'hidden'}).string
self.courseName = self._renameFolder(lectureName)
for i, week in enumerate(html.findAll('h3', {'class':'list_header'})):
topic = "%02d - %s" % (i+1, week.string)
temp = []
for j, lectures in enumerate(week.parent.nextSibling.findAll('li')):
lecture = "%02d %s" % (j+1, self._strip(lectures.a.contents[0]))
lecurls = []
for links in lectures.findAll('a'):
url = links['href'].replace('%2F', '/')
if 'view?' not in url and url.find('http') is 0:
lecurls.append(url)
temp.append((lecture, lecurls))
resp.append((topic, temp))
return resp
def downloadTree(self, course=None):
if course is None:
course = self.course
content = self.getContent(course)
print "\n" + self.courseName
for topic in content:
topicName = self._renameFolder(topic[0])
pathName = self.courseName.rstrip(' ') + '/' + topicName.rstrip(' ') + '/'
print "\n" + ' >> ' + topicName
for lecture in topic[1]:
lectureName = self._renameFolder(lecture[0])
dlPathName = pathName + lectureName.rstrip(' ') + '/'
try:
os.makedirs(dlPathName)
except OSError as e:
if e.errno != 17 and e.errno != 2:
raise e
pass
print " " * 8 + lectureName
for link in lecture[1]:
fileName = self._renameFile(link, lectureName)
filePath = dlPathName + fileName
self.downloadFile(link, filePath)
print "\n"
def downloadFile(self, url, fileName):
if os.path.exists(fileName):
print " " * 10 + " - %s (Already Saved)" % fileName.split('/')[-1]
else:
print " " * 10 + " - %s" % fileName.split('/')[-1]
try:
self.browser.retrieve(url, fileName, self._progressBar)
except KeyboardInterrupt:
if os.path.exists(fileName):
os.remove(fileName)
raise
except Exception, e:
err = e
if os.path.exists(fileName):
os.remove(fileName)
if e.errno == 2:
err = "Errno 2 (No such file or directory)"
print " " * 10 + " X %s - %s" % (fileName.split('/')[-1], err)
def _renameFile(self, url, name):
allowed = ['ppt', 'pptx', 'doc', 'docx', 'pdf',
'srt', 'txt', 'mp3', 'zip', 'mp4',
'exe', 'xls', 'xlsx', 'flv', 'wmv']
fname = None
name = re.sub("\([^\(]*$", "", name)
name = name.strip().replace(':','-')
name = re.sub("[^A-Za-z0-9\.\(\)\_\s\:]", "", name)
name = re.sub("_+", "_", name.replace(' ', '_'))
# file extension
if url.split('.')[-1] in allowed:
fname = url.split('/')[-1]
if url.split('format=')[-1] in allowed:
fname = name + '.' + url.split('format=')[-1]
if fname is None:
ext = re.search("(\w+)(?:\?.*)?$", url).group(1)
if ext in allowed:
fname = name + '.' + ext
fname = self._strReplace(['%20', '-', '%27'], ['_', '_', ''], fname)
return re.sub('_+', '_', fname)
def _renameFolder(self, name):
name = re.sub("[^A-Za-z0-9\.\(\)\_\s\-]", "", name.replace(':', '-'))
name = re.sub(" +", " ", name)
return name
def _progressBar(self, blocknum, bs, size):
if size > 0:
if size % bs != 0:
blockCount = size/bs + 1
else:
blockCount = size/bs
fraction = blocknum*1.0/blockCount
width = 50
stars = '*' * int(width * fraction)
spaces = ' ' * (width - len(stars))
progress = ' ' * 12 + ' [%s%s] (%s%%)' % (stars, spaces, int(fraction * 100))
if fraction*100 < 100:
sys.stdout.write(progress)
if blocknum < blockCount:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
else:
sys.stdout.write(' ' * int(width * 1.5) + '\r')
sys.stdout.flush()
def _strip(self, data):
return re.sub('(\n|\r|\t)', '', data)
def _strReplace(self, search, replace, subject):
for i in range(len(search)):
if search[i] != '':
subject = subject.replace(str(search[i]), str(replace[i]))
return subject
if __name__ == '__main__':
username = raw_input('Username: ')
password = getpass.getpass()
course = raw_input('Course: ')
options = {"user" : username,
"pass" : password,
"course" : course}
course = Coursera(options)
course.login()
course.getContent()
course.downloadTree()