-
Notifications
You must be signed in to change notification settings - Fork 1
/
chamilo.py
198 lines (143 loc) · 5.9 KB
/
chamilo.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import logging
import requests
from BeautifulSoup import BeautifulSoup
from config import fix_folder_names, download_only
USERNAME = 'esi_id'
PASSWORD = 'esi_pass'
CHAMI_URL = 'https://elearning.esi.heb.be'
CHECK_SIZE = False
DOWNLOAD_ALL = False
s = requests.Session()
logging.basicConfig(filename='chamilo.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)
log = logging.getLogger(__name__)
def authenticate(username, password):
url = '%s/%s' % (CHAMI_URL, 'index.php')
payload = {'login': username, 'password': password}
return s.post(url, data=payload, verify=False)
def soup_content(url):
return BeautifulSoup(s.get(url, verify=False).content)
def get_courses():
if DOWNLOAD_ALL:
url = '%s/%s' % (CHAMI_URL, 'main/auth/courses.php?action=display_courses&category_code=ALL&hidden_links=0')
soup = soup_content(url)
courses = [c for c in soup.findAll('a') if u'Accéder au cours' in c.text]
else:
url = '%s/%s' % (CHAMI_URL, 'user_portal.php')
soup = soup_content(url)
courses = [c.find('a') for c in soup.findAll('div', attrs={'class': 'well course-box'})]
return courses
def download_course(course_info):
url = course_info['href']
name = url.split('/')[4]
soup = soup_content(url)
urls = soup.findAll('a', attrs={'target': '_self'})
for url in urls:
if 'Document' in url.text:
url = url['href']
break
if 'document.php' in url:
document_url = url
soup = soup_content(url)
folders = [x['value'] for x in soup.findAll('option')]
for folder in folders:
save_folders(name, folder)
def save_folders(name, number):
url = '%s/main/document/document.php?id=%s&_qf__selector=&cidReq=%s' % (CHAMI_URL, number, name)
soup = soup_content(url)
soup = soup.find('table')
if not soup:
return
prev_files = '' # XXX: some files appear twice or more
for td in soup.findAll('tr'):
if td.find('small'):
files = td.find('a', attrs={'style': 'float:left'})
date = td.find('small').text.split()[0]
if files and files != prev_files and 'courses' in files.get('href'):
url = files.get('href')
save_file(name, url, date, CHECK_SIZE)
prev_files = files
def save_file(path, url, date, check=False):
name = '/'.join(url.split('/')[4:]).replace('document/', '')
course_name = name.split('/')[0]
name = name.replace(course_name, fix_folder_names.get(course_name))
path = '/'.join(name.split('/')[:-1])
if not os.path.exists(path):
log.info('"%s" created' % (path))
os.makedirs(path)
name = '.'.join(name.split('.')[:-1]) + '-' + date + '.' + name.split('.')[-1] if '.' in name else name + '-' + date
same_filesize = check_size(url, name) if check else True
if not os.path.exists(name) or not same_filesize:
with open(name, 'wb') as f:
f.write(s.get(url, verify=False).content)
log.info('"%s"... saved' % (name))
def check_size(url, name):
chami_filesize = int(s.head(url, verify=False).headers['content-length'])
local_filesize = os.path.getsize(name) if os.path.exists(name) else 0
# ugly hack due to false content length for empty file
if chami_filesize == 20:
chami_filesize = 0
return chami_filesize == local_filesize
if __name__ == '__main__':
from sys import argv, exit, platform
import ConfigParser
import HTMLParser
h = HTMLParser.HTMLParser()
def _exit():
if platform == 'win32':
raw_input('Press Enter to close')
exit()
try:
config = ConfigParser.RawConfigParser()
config.read('credentials.ini')
USERNAME = config.get('chamilo', 'username') if USERNAME == 'esi_id' else USERNAME
PASSWORD = config.get('chamilo', 'password') if PASSWORD == 'esi_pass' else PASSWORD
except:
pass
if USERNAME == 'esi_id' or PASSWORD == 'esi_pass':
log.error('Please enter your credentials. Quitting.')
_exit()
if 'check' in argv:
log.warn('Checking size while downloading (slower).')
CHECK_SIZE = True
auth = authenticate(USERNAME, PASSWORD)
if 'user_password_incorrect' in auth.url:
log.error('Could not login, check user & password.')
_exit()
if 'all' in argv:
DOWNLOAD_ALL = True
log.info('Download of all courses in progress.')
if 'list-courses' in argv:
DOWNLOAD_ALL = True
log.info('Checking courses...')
courses = get_courses()
if download_only:
courses = [c for c in courses if c['href'].split('/')[4] in download_only]
if not courses:
log.info('No courses found.')
for course in courses:
name = course['href'].split('/')[4]
if 'list-courses' in argv:
print(name + ' - ' + h.unescape(course.find('img')['alt']) if course.find('img') else '[none]')
continue
if 'update' in argv:
if not 'Depuis votre dernière visite' in str(courses):
log.info('Nothing to do.')
break
if 'Depuis votre dernière visite' in str(course):
log.info('Updating files for %s' % name)
download_course(course)
else:
log.info('Downloading files for %s' % name)
download_course(course)
_exit()
# TODO: test on Windows if can be removed
# if platform == 'win32':
# raw_input('Press Enter to close')