#!/usr/bin/python3 # -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup import re from datetime import datetime from datetime import timedelta import logging from logging.handlers import RotatingFileHandler from os import path from time import sleep def get_settings(file): settings = {} try: with open(file) as f: for line in f: try: arr = line.split(":") arr = [line.strip() for line in arr] settings[arr[0]] = arr[1] except IndexError: continue except FileNotFoundError as e: print(e) exit(1) return settings def find_string(expr): pattern = re.compile(r"{}".format(expr)) string = soup.find(text=pattern).__dict__ return string def get_date(string): match = re.findall(r"\d{4}-\d{2}-\d{2}", string) return match def get_ok(string): pattern = re.compile("glyphicon-ok") match = re.search(pattern, str(string)) if not match: return False else: return True def unsent_checks(): string = find_string("Неотправленные чеки")["next_element"].getText() res = get_date(string) try: date = datetime.strptime(res[0], '%Y-%m-%d').date() except (AttributeError, IndexError): date = None if date: return string else: return None def ok_check(string): string = find_string(string) res = get_ok(string["previous_element"]) if not res: logger.error(string["next_element"].getText()) def check_date(string, days): string = find_string(string)["next_element"].getText() date = get_date(string)[1] date = datetime.strptime(date, '%Y-%m-%d').date() - timedelta(days) if datetime.now().date() > date: logger.warning(string) settings_vars = get_settings("{}/settings.yaml".format(path.dirname(path.abspath(__file__)))) log_format = "%(asctime)s - %(levelname)s: %(message)s" logger = logging.getLogger("log") logger.setLevel("INFO") handler = RotatingFileHandler("{}/{}".format(path.dirname(path.abspath(__file__)), settings_vars["log_filename"]), maxBytes=1024, backupCount=5) logger.addHandler(handler) formatter = logging.Formatter("%(asctime)s - %(levelname)s: %(message)s") handler.setFormatter(formatter) logging.getLogger("urllib3").setLevel(logging.WARNING) try: cert_warn_days = int(settings_vars["cert_warn_days"]) except ValueError: cert_warn_days = None logger.error("check 'cert_warn_days' in settings.yaml") exit(1) text = requests.get("http://{}:{}".format(settings_vars["utm_address"], settings_vars["utm_port"])).text soup = BeautifulSoup(text, 'html.parser') unsent_error_counter = 0 while True: unsent_string = unsent_checks() if unsent_string: unsent_error_counter += 1 if unsent_error_counter > settings_vars["unsent_error_limit"]: logger.error(unsent_string) unsent_error_counter = 0 ok_check("Продуктивный контур") ok_check("Статус лицензии") check_date("Сертификат RSA", cert_warn_days) check_date("Сертификат ГОСТ", cert_warn_days) logger.info("OK") sleep(60)It was written by me but now I really can't understand why "soup" is available in "def find_string(expr)" Can anybody explain ? By the way may be there is some weakness in my code ?
Now I understand )))