Reorder code, add --since argument
This commit is contained in:
parent
6758e49bd8
commit
16ee5fd08d
1 changed files with 31 additions and 23 deletions
|
@ -14,8 +14,6 @@ SERVER = "mattermost.zeus.gent"
|
||||||
TEAM_NAME = "zeus"
|
TEAM_NAME = "zeus"
|
||||||
CHAN_NAME = "pannenkoeken"
|
CHAN_NAME = "pannenkoeken"
|
||||||
EMOJI_NAME = "pancakes"
|
EMOJI_NAME = "pancakes"
|
||||||
SINCE = datetime.datetime.strptime("2020-11-01 00:00", "%Y-%m-%d %H:%M") \
|
|
||||||
.astimezone(datetime.timezone.utc)
|
|
||||||
TAGGERS = [
|
TAGGERS = [
|
||||||
# Board
|
# Board
|
||||||
"flynn",
|
"flynn",
|
||||||
|
@ -36,30 +34,25 @@ USER = os.getenv("MM_USERNAME")
|
||||||
PASSWORD = os.getenv("MM_PASSWORD")
|
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):
|
def first(iterable, default=None):
|
||||||
for x in iterable:
|
for x in iterable:
|
||||||
return x
|
return x
|
||||||
return default
|
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
|
# Log in
|
||||||
mm = mattermost.MMApi(f"https://{SERVER}/api")
|
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)
|
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):
|
def handle_backlog(since):
|
||||||
for post in get_posts_for_channel(mm, channel, since):
|
for post in get_posts_for_channel(mm, channel, since):
|
||||||
for reaction in post.get("metadata", {}).get("reactions", []):
|
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.
|
# Note: skipping this step and updating an existing file would be dangerous: you would miss revocations that happened while not listening.
|
||||||
handle_backlog(SINCE)
|
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:
|
if live:
|
||||||
print("Now watching for live posts.", file=sys.stderr)
|
print("Now watching for live posts.", file=sys.stderr)
|
||||||
handle_live()
|
handle_live()
|
||||||
|
|
Loading…
Reference in a new issue