2021-07-12 22:17:49 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2021-05-30 21:47:36 +02:00
|
|
|
from http import HTTPStatus
|
|
|
|
from typing import List, Optional
|
2021-08-29 14:51:52 +02:00
|
|
|
from uuid import UUID
|
2021-05-30 21:47:36 +02:00
|
|
|
|
|
|
|
import arrow
|
|
|
|
import requests
|
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.config.config import get_settings
|
|
|
|
from mobilizon_reshare.event.event import MobilizonEvent, EventPublicationStatus
|
2021-05-30 21:47:36 +02:00
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-05-30 21:47:36 +02:00
|
|
|
|
|
|
|
class MobilizonRequestFailed(Exception):
|
|
|
|
# TODO move to an error module
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def parse_location(data):
|
2021-09-27 09:20:01 +02:00
|
|
|
if "physicalAddress" in data and data["physicalAddress"]:
|
|
|
|
addr = data["physicalAddress"]
|
|
|
|
return f"{addr['description']}, {addr['locality']}, {addr['region']}"
|
|
|
|
elif "onlineAddress" in data and data["onlineAddress"]:
|
|
|
|
return data["onlineAddress"]
|
|
|
|
else:
|
|
|
|
return None
|
2021-05-30 21:47:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_picture(data):
|
|
|
|
return (data.get("picture", {}) or {}).get("url")
|
|
|
|
|
|
|
|
|
|
|
|
def parse_event(data):
|
|
|
|
return MobilizonEvent(
|
|
|
|
name=data["title"],
|
|
|
|
description=data.get("description", None),
|
|
|
|
begin_datetime=arrow.get(data["beginsOn"]) if "beginsOn" in data else None,
|
|
|
|
end_datetime=arrow.get(data["endsOn"]) if "endsOn" in data else None,
|
|
|
|
mobilizon_link=data.get("url", None),
|
2021-08-29 14:51:52 +02:00
|
|
|
mobilizon_id=UUID(data["uuid"]),
|
2021-05-30 21:47:36 +02:00
|
|
|
thumbnail_link=parse_picture(data),
|
|
|
|
location=parse_location(data),
|
|
|
|
publication_time=None,
|
2021-08-05 00:29:50 +02:00
|
|
|
status=EventPublicationStatus.WAITING,
|
2022-02-09 00:54:56 +01:00
|
|
|
last_update_time=arrow.get(data["updatedAt"]) if "updatedAt" in data else None,
|
2021-05-30 21:47:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
query_future_events = """{{
|
|
|
|
group(preferredUsername: "{group}") {{
|
|
|
|
organizedEvents(page:{page}, afterDatetime:"{afterDatetime}"){{
|
|
|
|
elements {{
|
|
|
|
title,
|
|
|
|
url,
|
2022-02-09 00:54:56 +01:00
|
|
|
updatedAt,
|
2021-05-30 21:47:36 +02:00
|
|
|
beginsOn,
|
|
|
|
endsOn,
|
|
|
|
options {{
|
|
|
|
showStartTime,
|
|
|
|
showEndTime,
|
|
|
|
}},
|
|
|
|
uuid,
|
|
|
|
description,
|
|
|
|
onlineAddress,
|
|
|
|
physicalAddress {{
|
|
|
|
locality,
|
|
|
|
description,
|
|
|
|
region
|
|
|
|
}},
|
|
|
|
picture {{
|
|
|
|
url
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
}}"""
|
|
|
|
|
|
|
|
|
|
|
|
def get_mobilizon_future_events(
|
|
|
|
page: int = 1, from_date: Optional[arrow.Arrow] = None
|
|
|
|
) -> List[MobilizonEvent]:
|
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
url = get_settings()["source"]["mobilizon"]["url"]
|
2021-05-30 21:47:36 +02:00
|
|
|
query = query_future_events.format(
|
2021-07-12 22:17:49 +02:00
|
|
|
group=get_settings()["source"]["mobilizon"]["group"],
|
2021-05-30 21:47:36 +02:00
|
|
|
page=page,
|
|
|
|
afterDatetime=from_date or arrow.now().isoformat(),
|
|
|
|
)
|
|
|
|
r = requests.post(url, json={"query": query})
|
|
|
|
if r.status_code != HTTPStatus.OK:
|
|
|
|
raise MobilizonRequestFailed(
|
|
|
|
f"Request for events failed with code:{r.status_code}"
|
|
|
|
)
|
|
|
|
|
2021-07-12 22:17:49 +02:00
|
|
|
response_json = r.json()
|
|
|
|
logger.debug(f"Response:\n{json.dumps(response_json, indent=4)}")
|
|
|
|
if "errors" in response_json:
|
|
|
|
raise MobilizonRequestFailed(
|
|
|
|
f"Request for events failed because of the following errors: "
|
|
|
|
f"{json.dumps(response_json['errors'],indent=4)}"
|
|
|
|
)
|
2021-05-30 21:47:36 +02:00
|
|
|
return list(
|
2021-07-12 22:17:49 +02:00
|
|
|
map(parse_event, response_json["data"]["group"]["organizedEvents"]["elements"])
|
2021-05-30 21:47:36 +02:00
|
|
|
)
|