2021-11-11 15:18:04 +01:00
|
|
|
import logging.config
|
2021-05-20 01:57:40 +02:00
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.event.event_selection_strategies import select_event_to_publish
|
|
|
|
from mobilizon_reshare.mobilizon.events import get_unpublished_events
|
2021-10-02 18:09:03 +02:00
|
|
|
from mobilizon_reshare.publishers.abstract import EventPublication
|
2021-08-27 23:45:24 +02:00
|
|
|
from mobilizon_reshare.publishers.coordinator import (
|
|
|
|
PublicationFailureNotifiersCoordinator,
|
|
|
|
)
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.publishers.coordinator import PublisherCoordinator
|
2021-11-11 15:18:04 +01:00
|
|
|
from mobilizon_reshare.storage.query.model_creation import (
|
|
|
|
create_event_publication_models,
|
|
|
|
)
|
|
|
|
from mobilizon_reshare.storage.query.read_query import get_published_events
|
|
|
|
from mobilizon_reshare.storage.query.save_query import (
|
2021-08-05 00:29:50 +02:00
|
|
|
create_unpublished_events,
|
|
|
|
save_publication_report,
|
|
|
|
)
|
2021-05-13 13:32:44 +02:00
|
|
|
|
2021-05-20 01:57:40 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-05-13 13:32:44 +02:00
|
|
|
|
2021-10-25 13:43:38 +02:00
|
|
|
async def start():
|
2021-11-11 15:18:04 +01:00
|
|
|
"""
|
|
|
|
STUB
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2021-08-27 23:45:24 +02:00
|
|
|
# TODO: the logic to get published and unpublished events is probably redundant.
|
|
|
|
# We need a simpler way to bring together events from mobilizon, unpublished events from the db
|
|
|
|
# and published events from the DB
|
2021-07-05 23:07:12 +02:00
|
|
|
|
|
|
|
# Load past events
|
|
|
|
published_events = list(await get_published_events())
|
|
|
|
|
|
|
|
# Pull unpublished events from Mobilizon
|
2021-05-30 21:47:36 +02:00
|
|
|
unpublished_events = get_unpublished_events(published_events)
|
2021-07-12 22:17:49 +02:00
|
|
|
# Store in the DB only the ones we didn't know about
|
2021-11-11 15:18:04 +01:00
|
|
|
db_unpublished_events = await create_unpublished_events(unpublished_events)
|
2021-08-05 00:29:50 +02:00
|
|
|
event = select_event_to_publish(
|
|
|
|
published_events,
|
|
|
|
# We must load unpublished events from DB since it contains
|
|
|
|
# merged state between Mobilizon and previous WAITING events.
|
2021-11-11 15:18:04 +01:00
|
|
|
db_unpublished_events,
|
2021-08-05 00:29:50 +02:00
|
|
|
)
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
if event:
|
2021-11-20 15:53:38 +01:00
|
|
|
logger.info(f"Event to publish found: {event.name}")
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
models = await create_event_publication_models(event)
|
|
|
|
publications = list(EventPublication.from_orm(m, event) for m in models)
|
|
|
|
reports = PublisherCoordinator(publications).run()
|
2021-10-02 18:09:03 +02:00
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
await save_publication_report(reports, models)
|
2021-10-16 01:25:45 +02:00
|
|
|
for report in reports.reports:
|
2021-11-11 15:18:04 +01:00
|
|
|
if not report.succesful:
|
2021-11-11 16:20:50 +01:00
|
|
|
PublicationFailureNotifiersCoordinator(report,).notify_failure()
|
2021-11-11 15:18:04 +01:00
|
|
|
else:
|
2021-11-20 15:53:38 +01:00
|
|
|
logger.info("No event to publish found")
|