Initial commit

parents
#!/usr/bin/env python3
import asyncio
import json
import threading
from datetime import datetime
import requests
import websockets
HOST = 'localhost:7000'
CONTACT_ID = '1'
URL = 'http://%s/v1/messages/%s' % (HOST, CONTACT_ID)
URL_WS = 'ws://%s/v1/ws' % HOST
PROMPT = '$> '
def main():
load_history()
threading.Thread(target=get_message_stdin).start()
loop = asyncio.get_event_loop()
loop.create_task(get_message_websocket())
loop.run_forever()
loop.close()
async def get_message_websocket():
async with websockets.connect(URL_WS) as ws:
message = await ws.recv()
m = json.loads(message)
if m['name'] == 'org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent':
print() # line-break
print_message(m['data'])
print(PROMPT, end='', flush=True)
if not asyncio.get_event_loop().is_closed():
asyncio.get_event_loop().create_task(get_message_websocket())
def get_message_stdin():
while True:
body = input(PROMPT)
if len(body) > 0:
print_message(requests.post(URL, data={'message': body}).json())
def print_message(message):
prefix = '>' if message['local'] else '<'
time = get_timestamp(datetime.fromtimestamp(message['timestamp'] / 1000))
print(f"{time} {prefix} {message['body']}")
def get_timestamp(date):
return date.strftime('%Y-%m-%dT%H:%M:%S')
def load_history():
history = requests.get(URL).json()
for message in history:
print_message(message)
if __name__ == "__main__":
main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment