Small refactors
This commit is contained in:
parent
d8f1538d68
commit
544b6db885
1 changed files with 6 additions and 3 deletions
9
mmcli.py
9
mmcli.py
|
@ -205,7 +205,7 @@ def cat(mm_api: mattermost.MMApi, parsed):
|
||||||
backlog[0] = None
|
backlog[0] = None
|
||||||
|
|
||||||
if parsed.follow:
|
if parsed.follow:
|
||||||
def simple_websocket_callback(mmws, event_data):
|
def simple_websocket_callback(_mmws, event_data):
|
||||||
if event_data.get("event") == "posted":
|
if event_data.get("event") == "posted":
|
||||||
post = json.loads(event_data["data"]["post"])
|
post = json.loads(event_data["data"]["post"])
|
||||||
if post["channel_id"] != channel["id"]:
|
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"
|
ws_url = http_to_ws(mm_api._url) + "/v4/websocket"
|
||||||
mmws = MMws(initial_websocket_callback, mm_api.access_token, ws_url)
|
mmws = MMws(initial_websocket_callback, mm_api.access_token, ws_url)
|
||||||
|
|
||||||
if parsed.follow:
|
|
||||||
thread = threading.Thread(target=print_initial_messages)
|
thread = threading.Thread(target=print_initial_messages)
|
||||||
thread.setDaemon(True)
|
thread.setDaemon(True)
|
||||||
thread.start()
|
thread.start()
|
||||||
|
@ -270,6 +269,10 @@ def send(mm_api: mattermost.MMApi, parsed):
|
||||||
print(sent)
|
print(sent)
|
||||||
|
|
||||||
|
|
||||||
|
def tsv_escape(text):
|
||||||
|
return text.replace("\\", "\\\\").replace("\t", r"\t").replace("\n", r"\n")
|
||||||
|
|
||||||
|
|
||||||
def str_for_post(attribute, post, parsed):
|
def str_for_post(attribute, post, parsed):
|
||||||
obj = {
|
obj = {
|
||||||
k: v
|
k: v
|
||||||
|
@ -280,7 +283,7 @@ def str_for_post(attribute, post, parsed):
|
||||||
if parsed.format == "json":
|
if parsed.format == "json":
|
||||||
return json.dumps(obj)
|
return json.dumps(obj)
|
||||||
if parsed.format == "tsv":
|
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}"
|
return f"{obj['id']}\t{obj['create_at']}\t{obj.get('username') or obj['user_id']}\t{msg}"
|
||||||
assert False
|
assert False
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue