53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
import logging
|
|
|
|
import stomp
|
|
import logging.config
|
|
import time
|
|
import yaml
|
|
import msgspec
|
|
from pathlib import Path
|
|
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
|
from platformdirs import PlatformDirs
|
|
from log import get_logger
|
|
|
|
|
|
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
|
|
parser.add_argument('--verbose', '-v', action='count', default=0)
|
|
parser.add_argument("--server", "-s", default="127.0.0.1")
|
|
parser.add_argument("--port", "-p", default="61616")
|
|
args = parser.parse_args()
|
|
|
|
|
|
class Link(msgspec.Struct):
|
|
url: str
|
|
|
|
class MyListener(stomp.ConnectionListener):
|
|
def __init__(self, log, conn):
|
|
self.log = log
|
|
self.conn = conn
|
|
pass
|
|
|
|
def on_error(self, frame):
|
|
self.log.info("received an error %s", frame.body)
|
|
|
|
def on_message(self, frame):
|
|
self.log.info("received a message %s", frame.body)
|
|
link = msgspec.json.decode(frame.body, type=Link)
|
|
self.log.info("found link: %s", link.url)
|
|
json_bytes = msgspec.json.encode(link)
|
|
self.conn.send(body=json_bytes, destination="add_link_accepted")
|
|
self.conn.send(body=json_bytes, destination="update_title")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
logger = get_logger(args.verbose, __file__)
|
|
logger.info("kontor.read_queue started")
|
|
host = [(args.server, args.port)]
|
|
conn = stomp.Connection(host_and_ports=host)
|
|
conn.set_listener('', MyListener(logger, conn))
|
|
conn.connect(username='artemis', passcode='artemis', wait=True)
|
|
conn.subscribe(destination='add_link', id=1, ack='auto', headers={})
|
|
time.sleep(5)
|
|
conn.disconnect()
|
|
logger.info("kontor.read_queue finished")
|