diff --git a/services/reading-list-updater/hive/reading_list_updater/service.py b/services/reading-list-updater/hive/reading_list_updater/service.py index f5b766b..697dd9a 100644 --- a/services/reading-list-updater/hive/reading_list_updater/service.py +++ b/services/reading-list-updater/hive/reading_list_updater/service.py @@ -1,11 +1,13 @@ import logging from dataclasses import dataclass +from functools import cached_property from typing import Callable, Optional from pika import BasicProperties from pika.spec import Basic +from hive.mediawiki import HiveWiki from hive.messaging import Channel, blocking_connection from .entry import ReadingListEntry @@ -17,9 +19,27 @@ @dataclass class Service: email_queue: str = "readinglist.emails.received" - updates_queue: str = "readinglist.update.requests" + append_request_queue: str = "readinglist.append.requests" on_channel_open: Optional[Callable[[Channel], None]] = None + @cached_property + def wiki(self): + return HiveWiki() + + def on_append_request( + self, + channel: Channel, + method: Basic.Deliver, + properties: BasicProperties, + body: bytes, + ): + content_type = properties.content_type + if content_type != "application/json": + raise ValueError(content_type) + entry = ReadingListEntry.from_json_bytes(body) + wikitext = entry.as_wikitext() + self.wiki.page("Reading list").append(f"* {wikitext}") + def on_email_received( self, channel: Channel, @@ -33,7 +53,7 @@ def on_email_received( entry = ReadingListEntry.from_email_bytes(body) channel.publish_request( message=entry.as_dict(), - routing_key=self.updates_queue, + routing_key=self.append_request_queue, ) def run(self): @@ -44,4 +64,9 @@ def run(self): on_message_callback=self.on_email_received, dead_letter=True, ) + channel.consume_requests( + queue=self.append_request_queue, + on_message_callback=self.on_append_request, + dead_letter=True, + ) channel.start_consuming() diff --git a/services/reading-list-updater/requirements.txt b/services/reading-list-updater/requirements.txt index 636e467..0846e45 100644 --- a/services/reading-list-updater/requirements.txt +++ b/services/reading-list-updater/requirements.txt @@ -1,5 +1,6 @@ -r ../../requirements.txt -e ../../libs/common +-e ../../libs/config -e ../../libs/mediawiki -e ../../libs/messaging -e ../../libs/service