Small refactors

This commit is contained in:
Midgard 2021-06-30 13:06:04 +02:00
parent d8f1538d68
commit 544b6db885
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -205,7 +205,7 @@ def cat(mm_api: mattermost.MMApi, parsed):
backlog[0] = None
if parsed.follow:
def simple_websocket_callback(mmws, event_data):
def simple_websocket_callback(_mmws, event_data):
if event_data.get("event") == "posted":
post = json.loads(event_data["data"]["post"])
if post["channel_id"] != channel["id"]:
@ -228,7 +228,6 @@ def cat(mm_api: mattermost.MMApi, parsed):
ws_url = http_to_ws(mm_api._url) + "/v4/websocket"
mmws = MMws(initial_websocket_callback, mm_api.access_token, ws_url)
if parsed.follow:
thread = threading.Thread(target=print_initial_messages)
thread.setDaemon(True)
thread.start()
@ -270,6 +269,10 @@ def send(mm_api: mattermost.MMApi, parsed):
print(sent)
def tsv_escape(text):
return text.replace("\\", "\\\\").replace("\t", r"\t").replace("\n", r"\n")
def str_for_post(attribute, post, parsed):
obj = {
k: v
@ -280,7 +283,7 @@ def str_for_post(attribute, post, parsed):
if parsed.format == "json":
return json.dumps(obj)
if parsed.format == "tsv":
msg = obj.get("message", "").replace("\\", "\\\\").replace("\t", r"\t").replace("\n", r"\n")
msg = tsv_escape(obj.get("message", ""))
return f"{obj['id']}\t{obj['create_at']}\t{obj.get('username') or obj['user_id']}\t{msg}"
assert False