2021-05-20 01:57:40 +02:00
|
|
|
import logging.config
|
2021-10-02 18:09:03 +02:00
|
|
|
from functools import partial
|
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
|
|
|
|
from mobilizon_reshare.models.publication import PublicationStatus
|
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
|
|
|
|
from mobilizon_reshare.storage.query import (
|
2021-08-05 00:29:50 +02:00
|
|
|
get_published_events,
|
|
|
|
get_unpublished_events as get_db_unpublished_events,
|
|
|
|
create_unpublished_events,
|
|
|
|
save_publication_report,
|
|
|
|
publications_with_status,
|
|
|
|
)
|
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-08-04 18:53:58 +02:00
|
|
|
async def main():
|
2021-05-13 13:32:44 +02:00
|
|
|
"""
|
|
|
|
STUB
|
|
|
|
:return:
|
|
|
|
"""
|
2021-07-12 22:17:49 +02:00
|
|
|
|
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-08-27 23:45:24 +02:00
|
|
|
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.
|
|
|
|
list(await get_db_unpublished_events()),
|
|
|
|
)
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
if event:
|
2021-10-02 18:09:03 +02:00
|
|
|
logger.debug(f"Event to publish found: {event.name}")
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
waiting_publications_models = await publications_with_status(
|
|
|
|
status=PublicationStatus.WAITING, event_mobilizon_id=event.mobilizon_id,
|
2021-08-27 23:45:24 +02:00
|
|
|
)
|
2021-10-02 18:09:03 +02:00
|
|
|
waiting_publications = list(
|
|
|
|
map(
|
|
|
|
partial(EventPublication.from_orm, event=event),
|
|
|
|
waiting_publications_models.values(),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
reports = PublisherCoordinator(waiting_publications).run()
|
|
|
|
|
|
|
|
await save_publication_report(reports, waiting_publications_models)
|
2021-10-16 01:25:45 +02:00
|
|
|
for report in reports.reports:
|
2021-10-02 18:09:03 +02:00
|
|
|
PublicationFailureNotifiersCoordinator(report).notify_failure()
|
2021-08-27 23:45:24 +02:00
|
|
|
|
2021-10-02 18:09:03 +02:00
|
|
|
return 0 if reports.successful else 1
|
2021-07-12 22:17:49 +02:00
|
|
|
else:
|
2021-08-05 00:29:50 +02:00
|
|
|
return 0
|