def init_db(): try: print("sqlite: DB Create mode - use db file if exisiting.") Base.metadata.create_all(localdb_engine) except Exception as e: handle_exception(e) print(e)
def get_url_id(url): row = Url(url) try: session.add(row) session.commit() return row.id except Exception as e: session.rollback() handle_exception(e, risk='normal') row = session.query(Url).filter(Url.name == url).first() return row.id
def get_exam_id(exam): row = Exam(exam) try: session.add(row) session.commit() return row.id except Exception as e: session.rollback() handle_exception(e, "normal") row = session.query(Exam).filter(Exam.name == exam).first() return row.id
async def list_inp(request): """ --- description: This end-point takes a list of USNs along with source URL and reval(=true or false). tags: - INPUT parameters: - in: query name: url description: Link of VTU Index page (spcifically the page where usn is given). type: string required: true - in: query description: Indicate whether reval or not required: true name: reval type: boolean - in: body name: inp description: String of comma separated usns or usn ranges. type: string required: true responses: "200": description: successful operation. Returns queue containing a list of queued requests(objects). schema: type: object properties: queue: type: array items: object "405": description: invalid HTTP Method """ try: data = await request.json() except Exception as e: handle_exception(e, risk="ok") data = await request.post() if len(data['usns']) < 10: return web.json_response( {'queue': [progress.to_json() for progress in REQUEST_QUEUE]}) url = request.rel_url.query.get('url') reval = request.rel_url.query.get('reval') in ['True', 'true'] progress = Progress(inp=data['usns'], reval=reval) progress.url_id = get_url_id(url) localdb.add(progress) localdb.commit() REQUEST_QUEUE.append(progress) return web.json_response( {'queue': [progress.to_json() for progress in REQUEST_QUEUE]})
def check_usn(usn, url_id, exam_id, force=False): row = Usn(usn, url_id, exam_id) try: session.add(row) session.commit() return row except Exception as e: session.rollback() handle_exception(e, risk='normal') row = session.query(Usn).filter(Usn.usn == usn, Usn.url_id == url_id).first() if row.status == 0 or row.status == 5 or force: print("redoing:" + usn) return row print("skipped :" + usn) return None
async def single_usn(request): """ --- description: (Primarily for testing and helper for internal UI) This end-point takes in a single USN along with source URL.Response time for this route may be more than 5 seconds.. tags: - INPUT parameters: - in: query name: url description: Link of VTU Index page (spcifically the page where usn is given). type: string required: true - in: query name: usn description: 10 lettered string. type: string required: true - in: query description: Flag to indicate reval result or main result required: true name: reval type: boolean responses: "200": description: successful operation. Returns a json with result of usn. "405": description: invalid HTTP Method """ try: usn = request.rel_url.query.get('usn').lower() url = request.rel_url.query.get('url') reval = request.rel_url.query.get('reval') in ["True", "true"] force = True # try: todo #design # force = request.rel_url.query.get('force') in ['true','True'] # except: # force = True indexpage_url = "/".join(url.split('/')[-2:]) resultpage_url = indexpage_url.replace('index.php', 'resultpage.php') print(request.rel_url) if len(usn) != 10: return web.json_response({'data': ["invalid usn"]}) url_id = get_url_id(url) i = 0 while True: try: exam = await get_exam_name(indexpage_url) except Exception as e: handle_exception(e) exam = 0 if exam != 0: if len(exam) < 35 or force: break else: # todo #desgin return web.json_response({ 'data': [ "is this the right exam name displayed on site? Yes No'" + exam + "' if not contact admin." ] }) elif i > 3: return web.json_response({'data': ["VTU site is down"]}) i += 1 exam = exam.replace('/', '_') exam_id = get_exam_id(exam) usn_obj = check_usn(usn, url_id, exam_id, force=True) data = await async_executer(usn_obj, {}, indexpage_url, resultpage_url, localdb=localdb, save=False, reval=reval) print(data) sent = await send_student_to_db(data, reval) if not sent: print("something went wrong while sending to databse") usn_obj.status = 5 localdb.commit() else: usn_obj.status = 1 localdb.commit() return web.json_response({'data': data}) except Exception as e: return web.json_response({'data': ['ERROR:' + str(e)]})