Files
kontor/kontor-scripts/read_queue.py
T
tpeetz 031d71406c
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 5s
change queue names
2026-08-14 22:45:34 +02:00

77 lines
2.8 KiB
Python

import stomp
import time
import msgspec
import sys
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
from typing import Optional
from api import Server, get_api_config
from log import get_logger
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument("--config", "-c", default="kontor-api")
parser.add_argument("--server", "-s")
parser.add_argument("--messages", "-m", default="127.0.0.1")
parser.add_argument("--port", "-p", default="61616")
args = parser.parse_args()
class Link(msgspec.Struct):
url: str
class AddLinkListener(stomp.ConnectionListener):
def __init__(self, log, conn):
self.log = log
self.conn = conn
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="media.link.add.processed")
self.conn.send(body=json_bytes, destination="media.link.update_title")
class UpdateTitleListener(stomp.ConnectionListener):
def __init__(self, log, conn):
self.log = log
self.conn = conn
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="media.link.update_title.processed")
if __name__ == '__main__':
logger = get_logger(args.verbose, __file__)
logger.info("kontor.read_queue started")
#APICONFIG = get_api_config(logger, args.config)
#first_server: Optional[Server] = APICONFIG.get_server("inky")
#if not first_server:
# sys.exit(2)
#data = first_server.request(log=logger, table="media_file")
host = [(args.messages, args.port)]
conn_add = stomp.Connection(host_and_ports=host)
conn_add.set_listener('', AddLinkListener(logger, conn_add))
conn_add.connect(username='artemis', passcode='artemis', wait=True)
conn_add.subscribe(destination='media.link', id=1, ack='auto', headers={})
conn_update = stomp.Connection(host_and_ports=host)
conn_update.set_listener('', UpdateTitleListener(logger, conn_update))
conn_update.connect(username='artemis', passcode='artemis', wait=True)
conn_update.subscribe(destination='media.link.update_title', id=1, ack='auto', headers={})
time.sleep(5)
conn_add.disconnect()
conn_update.disconnect()
logger.info("kontor.read_queue finished")