diff --git a/read_mattermost.py b/read_mattermost.py index 94825cd..0ae8ace 100755 --- a/read_mattermost.py +++ b/read_mattermost.py @@ -14,8 +14,6 @@ SERVER = "mattermost.zeus.gent" TEAM_NAME = "zeus" CHAN_NAME = "pannenkoeken" EMOJI_NAME = "pancakes" -SINCE = datetime.datetime.strptime("2020-11-01 00:00", "%Y-%m-%d %H:%M") \ - .astimezone(datetime.timezone.utc) TAGGERS = [ # Board "flynn", @@ -36,30 +34,25 @@ USER = os.getenv("MM_USERNAME") PASSWORD = os.getenv("MM_PASSWORD") +try: + since_arg_i = sys.argv.index("--since") +except ValueError: + SINCE = datetime.datetime.now(tz=datetime.timezone.utc) + print(f"Warning: no start time provided, using now. Use `--since {SINCE.isoformat(timespec='seconds')}` to pin the start time.", file=sys.stderr) +if since_arg_i: + SINCE = datetime.datetime.fromisoformat(sys.argv[since_arg_i + 1]) + + +if sys.stdout.isatty(): + print("To use this data, redirect stdout to a file and use table.py on it.", file=sys.stderr) + + def first(iterable, default=None): for x in iterable: return x return default -def get_posts_for_channel(mmapi, channel_id, since, **kwargs): - after = None - while True: - data_page = mmapi._get("/v4/channels/"+channel_id+"/posts", params=( - { "after": after } - if after else - { "since": to_mm_timestamp(since) } - ), **kwargs) - - order = list(reversed(data_page["order"])) - for post_id in order: - yield data_page["posts"][post_id] - - if not order: - return - after = order[-1] - - ################################## # Log in mm = mattermost.MMApi(f"https://{SERVER}/api") @@ -160,6 +153,24 @@ def retract_if_appropriate(reaction): print(f"{awardee} {post['id']} verification removed by {awarder}", flush=True) +def get_posts_for_channel(mmapi, channel_id, since, **kwargs): + after = None + while True: + data_page = mmapi._get("/v4/channels/"+channel_id+"/posts", params=( + { "after": after } + if after else + { "since": to_mm_timestamp(since) } + ), **kwargs) + + order = list(reversed(data_page["order"])) + for post_id in order: + yield data_page["posts"][post_id] + + if not order: + return + after = order[-1] + + def handle_backlog(since): for post in get_posts_for_channel(mm, channel, since): for reaction in post.get("metadata", {}).get("reactions", []): @@ -186,9 +197,6 @@ live = "--live" in sys.argv[1:] # Note: skipping this step and updating an existing file would be dangerous: you would miss revocations that happened while not listening. handle_backlog(SINCE) -if sys.stdout.isatty(): - print("To use this data, redirect stdout to a file and use make_table.py on it.", file=sys.stderr) - if live: print("Now watching for live posts.", file=sys.stderr) handle_live()