From a5bbd9462b117e0e2606d97a028be1b4eaeb2501 Mon Sep 17 00:00:00 2001 From: mcbloch Date: Thu, 6 Oct 2022 23:07:26 +0200 Subject: [PATCH] Make a lil post on mattermost when we find a new valid report --- src/db.py | 11 +++++++++-- src/mattermost.py | 5 +++-- src/web.py | 17 +++++++++++++++-- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/db.py b/src/db.py index 4434e37..910dfec 100644 --- a/src/db.py +++ b/src/db.py @@ -54,10 +54,17 @@ def get_discovered_files() -> List[str]: return discovered_files -def add_valid_file(filename, metadata): +def add_valid_file(filename, metadata) -> bool: + """ + Returns: Boolean that indicates if the file is newly identified as valid. + """ db = _load_db() valid_files = db.get("valid_files", {}) + new_file = False + if filename not in valid_files: + new_file = True valid_files[filename] = metadata db["valid_files"] = valid_files _save_db(db) - return valid_files + + return new_file diff --git a/src/mattermost.py b/src/mattermost.py index 5e7b3e6..6dc1b7a 100644 --- a/src/mattermost.py +++ b/src/mattermost.py @@ -12,7 +12,7 @@ from colored import style from mattermostdriver import Driver from tabulate import tabulate -from MattermostObjects import MMUser, MMPost, MMChannelPosts +from mattermost_objects import MMUser, MMPost, MMChannelPosts from utils import humanize_date_difference, timer pp = pp.PrettyPrinter(indent=2) @@ -81,6 +81,7 @@ def merge_dict(a: dict, b: dict) -> dict: class MMApi(Driver): def __init__(self, user: User = users["flynn"]): + print(f"Initializing MMApi client for user {user}") Driver.__init__( self, merge_dict( @@ -109,7 +110,7 @@ class MMApi(Driver): print(f"{style.BOLD}[{log_level.value}]{style.RESET} {text}") def get_channel_id(self, channel_name): - resp = self.channels.get_channel_by_name(self.team_id, channel_name, since) + resp = self.channels.get_channel_by_name(self.team_id, channel_name) id = resp["id"] self.log(f"Fetching channel id for {channel_name}: {id}") return id diff --git a/src/web.py b/src/web.py index c723d5c..dd852c6 100644 --- a/src/web.py +++ b/src/web.py @@ -8,7 +8,8 @@ import requests import db import dir_utils -from mattermost import MMApi +from mattermost import MMApi, ChannelApi +import mattermost def find_codimd_files_on_mattermost(): @@ -54,6 +55,16 @@ def download_files(): print(f"Downloading url {url}") f.write(read_note(url)) +def filename_to_url(filename): + return f"https://codimd.zeus.gent/{filename[5:-3]}" + +def report_newly_found_file(filename): + channel = ChannelApi("bestuur-dev", mattermost.users["flynn"]) + + channel.create_post( + f"I found a new report: {filename_to_url(filename)}! Making work of putting it on gitlab :)" + ) + def validate_downloaded_files(): path = "data" @@ -62,7 +73,9 @@ def validate_downloaded_files(): for filename in dir_list: metadata = dir_utils.find_metadata("data/" + filename) if metadata is not None: - db.add_valid_file(filename, metadata) + is_new_file = db.add_valid_file(filename, metadata) + if is_new_file: + report_newly_found_file(filename) return db._load_db()