-
Notifications
You must be signed in to change notification settings - Fork 0
/
g4l_rlms_amrita.py
393 lines (301 loc) · 13.1 KB
/
g4l_rlms_amrita.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# -*-*- encoding: utf-8 -*-*-
import os
import re
import sys
import time
import sys
import urlparse
import json
import datetime
import uuid
import hashlib
import threading
import Queue
import functools
import traceback
import pprint
import webpage2html
import requests
from bs4 import BeautifulSoup
from flask import Blueprint, url_for, jsonify
from flask.ext.wtf import TextField, PasswordField, Required, URL, ValidationError
from labmanager.forms import AddForm
from labmanager.rlms import register, Laboratory, CacheDisabler, LabNotFoundError, register_blueprint
from labmanager.rlms.base import BaseRLMS, BaseFormCreator, Capabilities, Versions
from labmanager.rlms.queue import QueueTask, run_tasks
def dbg(msg):
if DEBUG:
print "[%s]" % time.asctime(), msg
sys.stdout.flush()
def dbg_lowlevel(msg, scope):
if DEBUG_LOW_LEVEL:
print "[%s][%s][%s]" % (time.asctime(), threading.current_thread().name, scope), msg
sys.stdout.flush()
class AmritaAddForm(AddForm):
DEFAULT_URL = 'https://amrita.olabs.edu.in'
DEFAULT_LOCATION = 'Coimbatore, India'
DEFAULT_PUBLICLY_AVAILABLE = True
DEFAULT_PUBLIC_IDENTIFIER = 'amrita'
DEFAULT_AUTOLOAD = True
amrita_username = TextField("Amrita username", validators = [Required()])
amrita_password = PasswordField("Amrita password")
def __init__(self, add_or_edit, *args, **kwargs):
super(AmritaAddForm, self).__init__(*args, **kwargs)
self.add_or_edit = add_or_edit
@staticmethod
def process_configuration(old_configuration, new_configuration):
old_configuration_dict = json.loads(old_configuration)
new_configuration_dict = json.loads(new_configuration)
if new_configuration_dict.get('amrita_password', '') == '':
new_configuration_dict['amrita_password'] = old_configuration_dict.get('amrita_password','')
return json.dumps(new_configuration_dict)
def validate_amrita_password(form, field):
if form.add_or_edit and field.data == '':
raise ValidationError("This field is required.")
class AmritaFormCreator(BaseFormCreator):
def get_add_form(self):
return AmritaAddForm
def create_amrita_session(username, password):
session = requests.Session()
session.post("https://amrita.olabs.edu.in/?pg=bindex&bsub=login_page", data={'submit':'Login', 'username':username, 'password':password}, timeout=(30,30), verify=False)
return session
class ObtainAmritaLabDataTask(QueueTask):
def __init__(self, laboratory_id, username, password):
self.username = username
self.password = password
self.result = {}
super(ObtainAmritaLabDataTask, self).__init__(laboratory_id)
def task(self):
session = requests.Session()
session.post("https://amrita.olabs.edu.in/?pg=bindex&bsub=login_page", data={'submit':'Login', 'username':self.username, 'password':self.password}, timeout=(30,30), verify=False)
text = session.get(self.laboratory_id, timeout=(30,30), verify=False).text
soup = BeautifulSoup(text, 'lxml')
element = soup.find(text="Simulator")
if not element:
return
a_element = None
for parent in element.parents:
if parent.name == 'a':
a_element = parent
break
if not a_element:
return
simulator_link = a_element['href']
if simulator_link.startswith('?'):
simulator_link = 'http://' + urlparse.urlparse(self.laboratory_id).netloc + '/' + simulator_link
soup_sim = BeautifulSoup(session.get(simulator_link, timeout=(30,30), verify=False).text, 'lxml')
iframe = soup_sim.find("iframe")
if not iframe:
return
iframe_url = iframe['src'].strip()
if iframe_url.startswith('//'):
iframe_url = 'http:{}'.format(iframe_url)
if iframe_url.startswith('http://180.149.57.33/'):
iframe_url = iframe_url.replace('180.149.57.33', 'www.olabs.edu.in')
base_url, args = iframe_url.split('?', 1)
args = '&'.join([ arg for arg in args.split('&') if arg.split('=')[0] not in ['elink_title', 'linktoken', 'elink_lan'] ])
self.result = {
'url' : base_url + '?' + args,
'sim_url': simulator_link
}
MIN_TIME = datetime.timedelta(hours=24)
def get_laboratories(username, password, force_cached=False):
laboratories = AMRITA.global_cache.get('get_laboratories', min_time = MIN_TIME)
if laboratories:
return laboratories
if force_cached: # It should have returned already
return None
physics = 'https://www.olabs.edu.in/?pg=topMenu&id=40'
biology = 'https://www.olabs.edu.in/?pg=topMenu&id=53'
chemistry = 'https://www.olabs.edu.in/?pg=topMenu&id=41'
all_category_urls = physics, biology, chemistry
all_lab_links = {
# url: name
}
lab_tasks = []
session = create_amrita_session(username, password)
for category_url in all_category_urls:
text = session.get(category_url, timeout=(30, 30), verify=False).text
soup = BeautifulSoup(text, 'lxml')
for div_element in soup.find_all(class_='exptPadng'):
for a_element in div_element.find_all('a'):
inner_text = a_element.get_text().strip()
if inner_text:
href = a_element['href']
if href.startswith('//'):
href = 'http:{}'.format(href)
all_lab_links[href] = inner_text
lab_tasks.append(ObtainAmritaLabDataTask(href, username, password))
# for lab_task in lab_tasks:
# print(lab_task.laboratory_id)
# import json
# print(json.dumps(all_lab_links, indent=4))
run_tasks(lab_tasks, threads=4)
result = {
'laboratories' : [],
'all_links': [],
}
all_labs = []
for task in lab_tasks:
if task.result:
name = all_lab_links[task.laboratory_id]
iframe_url = task.result['url'] # TODO: remove linktoken
sim_url = task.result['sim_url']
lab = Laboratory(name=name, laboratory_id=iframe_url, description=name, home_url=sim_url)
result['laboratories'].append(lab)
result['all_links'].append({
'lab': lab,
'name': name,
'base-url': task.laboratory_id,
'sim-url': sim_url,
'iframe-url': iframe_url,
})
AMRITA.global_cache['get_laboratories'] = result
return result
FORM_CREATOR = AmritaFormCreator()
CAPABILITIES = [ Capabilities.WIDGET, Capabilities.URL_FINDER, Capabilities.CHECK_URLS, Capabilities.DOWNLOAD_LIST ]
class RLMS(BaseRLMS):
def __init__(self, configuration, *args, **kwargs):
self.configuration = json.loads(configuration or '{}')
self.amrita_username = self.configuration.get('amrita_username', os.environ.get('AMRITA_USERNAME'))
self.amrita_password = self.configuration.get('amrita_password', os.environ.get('AMRITA_PASSWORD'))
if not self.amrita_username or not self.amrita_password:
raise Exception("Invalid Amrita settings: credentials required")
def get_version(self):
return Versions.VERSION_1
def get_capabilities(self):
return CAPABILITIES
def get_laboratories(self, **kwargs):
return get_laboratories(self.amrita_username, self.amrita_password)['laboratories']
def get_base_urls(self):
return [ 'http://amrita.olabs.edu.in', 'http://amrita.olabs.co.in', 'http://cdac.olabs.edu.in', 'https://amrita.olabs.edu.in', 'https://amrita.olabs.co.in', 'https://cdac.olabs.edu.in' ]
def get_lab_by_url(self, url):
if '?' in url:
base_url, args = url.split('?', 1)
args = '&'.join([ arg for arg in args.split('&') if arg.split('=')[0] not in ['elink_title', 'linktoken', 'elink_lan'] ])
url = base_url + '?' + args
laboratories = get_laboratories(self.amrita_username, self.amrita_password)
for lab in laboratories['all_links']:
if lab['sim-url'] == url or lab['iframe-url'] == url or lab['base-url'] == url:
return lab['lab']
return None
def get_check_urls(self, laboratory_id):
return [ laboratory_id ]
def reserve(self, laboratory_id, username, institution, general_configuration_str, particular_configurations, request_payload, user_properties, *args, **kwargs):
if '.co.in/' in laboratory_id:
laboratory_id = laboratory_id.replace('.co.in/', '.edu.in/')
if 'edu.in/' in laboratory_id:
laboratory_id = laboratory_id.replace('http://', 'https://')
response = {
'reservation_id' : laboratory_id,
'load_url' : laboratory_id
}
return response
def load_widget(self, reservation_id, widget_name, **kwargs):
if 'edu.in/' in reservation_id:
reservation_id = reservation_id.replace('http://', 'https://')
return {
'url' : reservation_id
}
def list_widgets(self, laboratory_id, **kwargs):
default_widget = dict( name = 'default', description = 'Default widget' )
return [ default_widget ]
def get_downloads(self, laboratory_id):
return {
'en_ALL': url_for('amrita.amrita_download', laboratory_id=laboratory_id, _external=True),
}
class AmritaTaskQueue(QueueTask):
RLMS_CLASS = RLMS
def populate_cache(rlms):
rlms.get_laboratories()
AMRITA = register("Amrita", ['1.0'], __name__)
AMRITA.add_local_periodic_task('Populating cache', populate_cache, hours = 13)
DEBUG = AMRITA.is_debug() or (os.environ.get('G4L_DEBUG') or '').lower() == 'true' or False
DEBUG_LOW_LEVEL = DEBUG and (os.environ.get('G4L_DEBUG_LOW') or '').lower() == 'true'
if DEBUG:
print("Debug activated")
if DEBUG_LOW_LEVEL:
print("Debug low level activated")
sys.stdout.flush()
amrita_blueprint = Blueprint('amrita', __name__)
class FakeRequestsClass(object):
def fake_get(self, *args, **kwargs):
kwargs['verify'] = False
return requests.get(*args, **kwargs)
def __getattr__(self, name):
if name == 'get':
return self.fake_get
return getattr(requests, name)
@amrita_blueprint.route('/ids')
def amrita_list():
result = get_laboratories(os.environ.get('AMRITA_USERNAME'), os.environ.get('AMRITA_PASSWORD'), force_cached=True)
if result is None:
return jsonify(success=False, message="No lab found at all")
labs = []
for lab in result['laboratories']:
labs.append({
'laboratory_id': lab.laboratory_id,
'name': lab.name,
'description': lab.description,
'home_url': lab.home_url,
})
return jsonify(success=True, labs=labs)
@amrita_blueprint.route('/id/<path:laboratory_id>')
def amrita_download(laboratory_id):
result = get_laboratories(os.environ.get('AMRITA_USERNAME'), os.environ.get('AMRITA_PASSWORD'), force_cached=True)
if result is None:
return "Not found", 404
laboratories = result['laboratories']
link = None
for lab in laboratories:
if lab.laboratory_id == laboratory_id:
link = lab.laboratory_id
if link is None:
laboratory_id = laboratory_id.replace('.co.in', '.edu.in')
for lab in laboratories:
if lab.laboratory_id == laboratory_id:
link = lab.laboratory_id
if link is None:
laboratory_id = laboratory_id.replace('http://', 'https://')
for lab in laboratories:
if lab.laboratory_id == laboratory_id:
link = lab.laboratory_id
if link is None:
laboratory_id = laboratory_id.replace('.edu.in', '.co.in')
for lab in laboratories:
if lab.laboratory_id == laboratory_id:
link = lab.laboratory_id
if not link:
return "Not found", 404
webpage2html.requests = FakeRequestsClass()
try:
generated = webpage2html.generate(index=link, keep_script=True, verbose=False, verify=False)
finally:
webpage2html.requests = requests
return generated.encode()
register_blueprint(amrita_blueprint, url='/amrita')
def main():
with CacheDisabler():
rlms = RLMS("{}")
t0 = time.time()
laboratories = rlms.get_laboratories()
tf = time.time()
print len(laboratories), (tf - t0), "seconds"
print
print laboratories[:10]
print
# print rlms.reserve('http://phet.colorado.edu/en/simulation/beers-law-lab', 'tester', 'foo', '', '', '', '', locale = 'es_ALL')
try:
rlms.reserve('identifier-not-found', 'tester', 'foo', '', '', '', '', locale = 'xx_ALL')
except LabNotFoundError:
print "Captured error successfully"
print rlms.get_base_urls()
# print rlms.get_lab_by_url("https://phet.colorado.edu/en/simulation/acid-base-solutions")
return
for lab in laboratories[:5]:
t0 = time.time()
print rlms.reserve(lab.laboratory_id, 'tester', 'foo', '', '', '', '', locale = lang)
tf = time.time()
print tf - t0, "seconds"
if __name__ == '__main__':
main()