class AbstractParseController(object): def __init__(self): self.logger = LogController() def writeToJsonFile(self, file_name: str, group_name: str): self.logger.info( f"Parser {self.__class__.__name__} started for {group_name}") json = self.makeJson(group_name) filepath = f'{file_name}.json' FileController.writeToFile(filepath, json) def makeJson(self, group_name: str): self.logger.info( f"Parser {self.__class__.__name__} started for {group_name}") return str(self._parse(group_name)) def _get_url_response(self, url: str): try: response = requests.get(url, timeout=30) except requests.Timeout: self.logger.alert(f'Timeout error, url: {url}') return None except requests.HTTPError as err: code = err.response.status_code self.logger.alert(f'Error url: {url}, code: {code}') return None except requests.RequestException: self.logger.alert(f'Downloading error url: {url}') return None else: return response @abstractmethod def _parse(self, group_name: str) -> dict: pass
def getUserGroupId(self, user_id): userInfo = DbManager.getTgUserInfo(user_id) if not userInfo: alert = f'UserController.getUserGroupId failed: user {user_id} info empty' LogController().alert(alert) AlertManager().notify(alert, AlertManager.WARNING_LEVEL) return self.DEFAULT_GROUP_ID return userInfo['group_id']
def getUserUniversityId(self, user_id): userInfo = DbManager.getTgUserInfo(user_id) if not userInfo: alert = f'UserController.getUserUniversityId failed: user {user_id} info empty' LogController().alert(alert) AlertManager().notify(alert, AlertManager.WARNING_LEVEL) return self.DEFAULT_UNIVERSITY_ID return userInfo['university_id']
def install(): """Install crontab from config file.""" crontab = "" LogController().info("Cron installed") for task_name in config['tasks']: task = config['tasks'][task_name] if task['is_active']: crontab += f"# {task['comment']}\n{task['command']}\n" CrontabController.installCrontab(crontab)
def attachLog(self): """Attach logfile to email.""" logfile = LogController().getLogFilename() attach_file = open(logfile, 'rb') payload = MIMEBase('application', 'octate-stream') payload.set_payload(attach_file.read()) encoders.encode_base64(payload) payload.add_header('Content-Decomposition', 'attachment; filename= www', filename=logfile) self.mail.attach(payload)
def getUniversityKeyboardMarkup() -> ReplyKeyboardMarkup: markup = ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=True) universities = DbManager.getUniversities() if not universities: LogController().alert( "getUniversityKeyboardMarkup failed: universities empty") AlertManager().notify( "getUniversityKeyboardMarkup failed: universities empty", AlertManager.WARNING_LEVEL) universities = [['Default']] for university in universities: markup.row(university['university_name']) return markup
def getCurrStatus(self, user_id): userInfo = DbManager.getTgUserInfo(user_id) if not userInfo: alert = f'UserController.getCurrStatus failed: user {user_id} info empty' LogController().alert(alert) AlertManager().notify(alert, AlertManager.WARNING_LEVEL) return self.DEFAULT_STATUS if userInfo['group_id'] != self.DEFAULT_GROUP_ID \ and userInfo['university_id'] != self.DEFAULT_UNIVERSITY_ID: return self.GROUP_CHOSEN elif userInfo['university_id'] != self.DEFAULT_UNIVERSITY_ID: return self.UNIVERSITY_CHOSEN else: return self.DEFAULT_STATUS
from Controllers.MetricsController import MetricsController from Controllers.TelegramViewController import TelegramViewController from Controllers.Translation.TranslationController import TranslationController configImporter = CliArgsController() configImporter.parseArgs() config = configImporter.getConfig() bot = Bot(token=config.BOT_TOKEN) dp = Dispatcher(bot) parser = ParseManager() dbManager = DbManager() alertManager = AlertManager() logger = LogController() userController = UserController() viewController = TelegramViewController() messageGenerator = TranslationController() @dp.message_handler(commands=["start", "changeuniversity"]) async def chooseUniversity(message): """Start message. Configure university. Drops university if user already registered. @param message Telegram message class. """
def startupDb(): while not DbManager.checkDbAvailability(): LogController().alert('Db unavailable. Waiting 5 seconds for connection') DateTimeController.sleep(5) DbManager.upAllMigrations() DbManager.fillGroups()
def __init__(self): self.dbController = DbControllerFactory.getDbController() self.logger = LogController()
def __init__(self) -> None: self.config = CliArgsController().getConfig() self.logger = LogController()
def __init__(self): self.logger = LogController()
def erase(): """Remove all records from crontab.""" LogController().info("Cron erased") CrontabController.removeCrontab()
def disable(): """Comments every record in crontab.""" LogController().info("Cron disabled") CrontabController.disableCrontab()
def enable(): """Uncomment all existing records in crontab.""" LogController().info("Cron enabled") CrontabController.enableCrontab()