import json import os import logging from multiprocessing.pool import ThreadPool from pprint import pprint import requests from requests.auth import HTTPBasicAuth from ics import Calendar from backend.config import Config from backend.recorder_adapters.epiphan_base import EpiphanV1 from backend.recorder_adapters.extron_smp import SMP logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.DEBUG) logger.addHandler(stream_handler) base_url = "https://opencast.bibliothek.kit.edu" session = requests.session() session.auth = HTTPBasicAuth(Config.OPENCAST_USER, Config.OPENCAST_PW) config = {'service_urls': {}} recorders = None def get_service_url(service_type: str): if service_type in config['service_urls']: return config['service_urls'][service_type] params = {'serviceType': service_type} url = base_url + "/services/available.json" res = session.get(url, params=params) if res.ok: service = res.json()["services"]["service"] config["service_urls"][service_type] = service["host"] + \ service["path"] return service["host"] + service["path"] return None def get_calender(rec_id): params = {'agentid': rec_id} url = get_service_url('org.opencastproject.scheduler') + "/calendars" print(url) res = session.get(url, params=params) if res.ok: return Calendar(res.text) def get_capture_agents(): url = get_service_url("org.opencastproject.capture.admin") + "/agents.json" res = session.get(url) if res.ok: return res.json()["agents"]["agent"] def get_recorder_details(): """Temporary implementation using initial_recorders.json. Should be replaced by DB layer later!""" global recorders if recorders is None: f = os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'models', 'initial_recorders.json')) with open(f, 'r') as json_file: recorders = json.load(json_file)['recorders'] return recorders def get_recorder_by_name(name: str): for r in get_recorder_details(): if r["name"] == name: return r return None def notify_users_of_problem(msg: str): pass def check_capture_agent_state(a: dict): logger.debug("Checking Agent {}".format(a['name'])) c = get_calender(a['name']) is_recording_in_calendar = len(list(c.timeline.now())) >= 1 if is_recording_in_calendar: if a['state'] == "capturing": print( "{} is in capturing state, so there should be an entry in the calendar of the recorder, right? -> {}".format( a['name'], is_recording_in_calendar )) rec = get_recorder_by_name(a['name']) print(rec) if "SMP" in rec["type"]: print("using SMP adapter") rec = SMP(rec['ip'], rec['password']) else: rec = EpiphanV1(rec['ip'], rec["username"], rec["password"]) if rec.is_recording(): print("OK – recorder is recording :)") else: logger.info(rec.get_recording_status()) logger.error("FATAL - recorder {} must be recording!!!!".format(a['name'])) else: print("FATAL: {} is not in capturing state...but should be!!".format(a['name'])) else: rec = get_recorder_by_name(a['name']) if "SMP" in rec["type"]: logger.debug("using SMP adapter") rec = SMP(rec['ip'], rec['password']) else: rec = EpiphanV1(rec['ip'], rec["username"], rec["password"]) if rec.is_recording(): logger.error("FATAL - recorder must not be recording!!!!") else: logger.info("OK – recorder is not recording :)") agents = get_capture_agents() logger.info("Got {} capture agents that will be checked...".format(len(agents))) pool = ThreadPool(5) pool.map(check_capture_agent_state, agents) exit() c = get_calender('CS 30.46 Chemie Neuer Hoersaal') print(c.events) print(list(c.timeline)) # chronological order print(list(c.timeline.now()))