added event parsing

This commit is contained in:
Simone Robutti 2021-05-15 14:16:02 +02:00
parent 62db19ab90
commit 8de9874023
3 changed files with 43 additions and 27 deletions

View File

@ -1,8 +1,8 @@
from dataclasses import dataclass, asdict
from datetime import datetime
from enum import Enum
from typing import Optional
import arrow
from jinja2 import Template
@ -19,14 +19,14 @@ class MobilizonEvent:
name: str
description: str
begin_datetime: datetime
end_datetime: datetime
last_accessed: datetime
begin_datetime: arrow.Arrow
end_datetime: arrow.Arrow
last_accessed: arrow.Arrow
mobilizon_link: str
mobilizon_id: str
thumbnail_link: Optional[str] = None
location: Optional[str] = None
publication_time: Optional[datetime] = None
publication_time: Optional[arrow.Arrow] = None
publication_status: PublicationStatus = PublicationStatus.WAITING
def __post_init__(self):

View File

@ -1,9 +1,37 @@
from typing import List
from typing import List, Optional
from mobilizon_bots.event.event import MobilizonEvent
from mobilizon_bots.event.event import MobilizonEvent, PublicationStatus
import requests
import arrow
def parse_location(data):
# TODO define a better logic (or a customizable strategy) to get the location
return (data.get("physicalAddress", {}) or {}).get("locality") or data.get(
"onlineAddress"
)
def parse_picture(data):
return (data.get("picture", {}) or {}).get("url")
def parse_event(data):
return MobilizonEvent(
name=data["title"],
description=data["description"],
begin_datetime=arrow.get(data["beginsOn"]),
end_datetime=arrow.get(data["endsOn"]),
last_accessed=arrow.now(),
mobilizon_link=data["url"],
mobilizon_id=data["uuid"],
thumbnail_link=parse_picture(data),
location=parse_location(data),
publication_time=None,
publication_status=PublicationStatus.WAITING,
)
query_future_events = """{{
group(preferredUsername: "{group}") {{
organizedEvents(page:{page}, afterDatetime:"{afterDatetime}"){{
@ -16,13 +44,7 @@ query_future_events = """{{
showStartTime,
showEndTime,
}},
attributedTo {{
avatar {{
url,
}}
name,
preferredUsername,
}},
uuid,
description,
onlineAddress,
physicalAddress {{
@ -30,11 +52,6 @@ query_future_events = """{{
description,
region
}},
tags {{
title,
id,
slug
}},
picture {{
url
}},
@ -44,14 +61,18 @@ query_future_events = """{{
}}"""
def get_mobilizon_future_events() -> List[MobilizonEvent]:
def get_mobilizon_future_events(
page: int = 1, from_date: Optional[arrow.Arrow] = None
) -> List[MobilizonEvent]:
url = "https://apero.bzh/api"
query = query_future_events.format(
group="test", page=1, afterDatetime=arrow.now().isoformat()
group="test", page=page, afterDatetime=from_date or arrow.now().isoformat()
)
r = requests.post(url, json={"query": query})
return r.json()
return list(
map(parse_event, r.json()["data"]["group"]["organizedEvents"]["elements"])
)
get_mobilizon_future_events()

View File

@ -1,5 +0,0 @@
from mobilizon_bots import __version__
def test_version():
assert __version__ == "0.1.0"