moved everything to a new module called backend

This commit is contained in:
2019-10-23 15:00:33 +02:00
parent 310d5f4820
commit 6b4f7c8118
52 changed files with 2 additions and 380 deletions

View File

@@ -0,0 +1,2 @@
# Copyright (c) 2019. Tobias Kurze, KIT

View File

@@ -0,0 +1,72 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2019. Tobias Kurze
import hashlib
import logging
from datetime import datetime
from json import dumps
from pprint import pprint
from sqlalchemy import and_
from backend import db
from backend.models.recorder_model import RecorderModel, RecorderCommand
from backend.recorder_adapters import get_defined_recorder_adapters
def calculate_md5_checksum(string_to_md5_sum: str):
return hashlib.md5(string_to_md5_sum.encode('utf-8')).hexdigest()
def create_recorder_commands_for_recorder_adapter(command_definitions: dict, recorder_model: RecorderModel):
existing_recorder_commands = RecorderCommand.query.filter(
and_(RecorderCommand.name.in_(command_definitions.keys())),
RecorderCommand.recorder_model == recorder_model)
existing_commands = set()
for existing_command in existing_recorder_commands:
existing_commands.add(existing_command.name)
args = command_definitions.get(existing_command.name)
if dumps(existing_command.parameters) != dumps(args):
logging.warning(
"The function definition {} collides with an existing definition of the same name "
"but different parameters!".format(
existing_command.name))
existing_command.last_time_modified = datetime.utcnow()
existing_command.parameters = args
db.session.commit()
for c_d in set(command_definitions.keys()) - existing_commands:
# print(c_d)
args = command_definitions.get(c_d)
# create new recorder command(s)
r_c = RecorderCommand(name=c_d, parameters=args, recorder_model=recorder_model)
db.session.add(r_c)
db.session.commit()
def update_recorder_models_database():
r_as = get_defined_recorder_adapters()
for r_a in r_as:
r_m = RecorderModel.get_by_adapter_id(r_a["id"])
model_checksum = calculate_md5_checksum(dumps(r_a["commands"]))
if r_m is None:
r_m = RecorderModel(record_adapter_id=r_a["id"], model_name=r_a["name"], checksum=model_checksum,
requires_user=r_a.get('requires_user', None),
requires_password=r_a.get('requires_password', None))
db.session.add(r_m)
db.session.flush()
db.session.refresh(r_m)
else:
if not model_checksum == r_m.checksum:
r_m.model_name = r_a["name"]
r_m.model_name = r_a["name"]
r_m.checksum = model_checksum
create_recorder_commands_for_recorder_adapter(r_a["commands"], r_m)
db.session.commit()
if __name__ == '__main__':
db.create_all()
update_recorder_models_database()

View File

@@ -0,0 +1,60 @@
from pprint import pprint
import re
import requests
from bs4 import BeautifulSoup
def scrape_rooms():
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
room_url = "https://campus.kit.edu/live-stud/campus/all/roomgroup.asp?roomgroupcolumn1=H%F6r%2D%2FLehrsaal&tguid=0x1A35C3A1490748388EBEBA3943EFCDD5"
page = requests.get(room_url, headers=headers)
# soup = BeautifulSoup(page.content, 'html5lib')
soup = BeautifulSoup(page.content, 'html.parser')
# pprint(page.content)
# pprint(soup.prettify())
idx = 0
rooms = []
re_string = r"^(\d\d.\d\d)?\s(.*)"
re_exp = re.compile(re_string)
for tr in soup.find_all('tr'):
idx += 1
if idx == 1: # skip first row
continue
a_name = tr.find_all('a')[0].string
a_building = tr.find_all('a')[3].string
match = re_exp.match(a_name)
if match is not None:
building_number, name = re_exp.match(a_name).groups()
else:
name = a_name
building_number = None
match = re_exp.match(a_building)
if match is not None:
building_number, building_name = re_exp.match(a_building).groups()
else:
building_name = a_name
building_number = None
room = {'name': name,
'room_number': tr.find_all('a')[1].string if tr.find_all('a')[0].string != "None" else tr.find_all('a')[
1].string,
'building_name': building_name,
'building_number': building_number}
rooms.append(room)
return rooms
if __name__ == '__main__':
scrape_rooms()