2021-11-20 15:40:10 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import facebook
|
2022-02-14 21:10:27 +01:00
|
|
|
from facebook import GraphAPIError
|
2021-11-20 15:40:10 +01:00
|
|
|
|
2022-12-11 14:15:04 +01:00
|
|
|
from mobilizon_reshare.dataclasses import MobilizonEvent
|
2022-02-16 13:51:00 +01:00
|
|
|
from mobilizon_reshare.formatting.description import html_to_plaintext
|
2021-11-20 15:40:10 +01:00
|
|
|
from mobilizon_reshare.publishers.abstract import (
|
|
|
|
AbstractPlatform,
|
|
|
|
AbstractEventFormatter,
|
|
|
|
)
|
|
|
|
from mobilizon_reshare.publishers.exceptions import (
|
|
|
|
InvalidCredentials,
|
|
|
|
InvalidEvent,
|
2022-03-02 08:59:49 +01:00
|
|
|
InvalidMessage,
|
|
|
|
PublisherError,
|
2021-11-20 15:40:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class FacebookFormatter(AbstractEventFormatter):
|
|
|
|
_conf = ("publisher", "facebook")
|
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
def _validate_event(self, event: MobilizonEvent) -> None:
|
2021-11-20 15:40:10 +01:00
|
|
|
text = event.description
|
|
|
|
if not (text and text.strip()):
|
|
|
|
self._log_error("No text was found", raise_error=InvalidEvent)
|
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
def _validate_message(self, message) -> None:
|
2022-03-02 08:59:49 +01:00
|
|
|
if len(message) >= 63200:
|
|
|
|
self._log_error("Message is too long", raise_error=InvalidMessage)
|
2021-11-20 15:40:10 +01:00
|
|
|
|
2022-02-16 13:51:00 +01:00
|
|
|
def _preprocess_event(self, event: MobilizonEvent):
|
|
|
|
event.description = html_to_plaintext(event.description)
|
|
|
|
event.name = html_to_plaintext(event.name)
|
|
|
|
return event
|
|
|
|
|
2021-11-20 15:40:10 +01:00
|
|
|
|
|
|
|
class FacebookPlatform(AbstractPlatform):
|
|
|
|
"""
|
|
|
|
Facebook publisher class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = "facebook"
|
|
|
|
|
2022-03-02 08:59:49 +01:00
|
|
|
def _get_api(self) -> facebook.GraphAPI:
|
2022-03-06 10:41:02 +01:00
|
|
|
return facebook.GraphAPI(access_token=self.conf["page_access_token"])
|
2021-11-20 15:40:10 +01:00
|
|
|
|
|
|
|
def _send(self, message: str, event: Optional[MobilizonEvent] = None):
|
2022-03-02 08:59:49 +01:00
|
|
|
try:
|
|
|
|
self._get_api().put_object(
|
|
|
|
parent_object="me",
|
|
|
|
connection_name="feed",
|
|
|
|
message=message,
|
|
|
|
link=event.mobilizon_link if event else None,
|
|
|
|
)
|
|
|
|
except GraphAPIError:
|
|
|
|
self._log_error(
|
|
|
|
"Facebook send failed", raise_error=PublisherError,
|
|
|
|
)
|
2021-11-20 15:40:10 +01:00
|
|
|
|
|
|
|
def validate_credentials(self):
|
|
|
|
|
|
|
|
try:
|
2022-02-14 21:10:27 +01:00
|
|
|
self._log_debug("Validating Facebook credentials")
|
2021-11-20 15:40:10 +01:00
|
|
|
self._get_api().get_object(id="me", field="name")
|
2022-02-14 21:10:27 +01:00
|
|
|
except GraphAPIError:
|
2021-11-20 15:40:10 +01:00
|
|
|
self._log_error(
|
|
|
|
"Invalid Facebook credentials. Authentication Failed",
|
|
|
|
raise_error=InvalidCredentials,
|
|
|
|
)
|
|
|
|
|
2022-03-02 08:59:49 +01:00
|
|
|
self._log_debug("Facebook credentials are valid")
|
2022-02-14 21:10:27 +01:00
|
|
|
|
2021-11-20 15:40:10 +01:00
|
|
|
def _validate_response(self, response):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FacebookPublisher(FacebookPlatform):
|
|
|
|
|
|
|
|
_conf = ("publisher", "facebook")
|
|
|
|
|
|
|
|
|
|
|
|
class FacebookNotifier(FacebookPlatform):
|
|
|
|
|
|
|
|
_conf = ("notifier", "facebook")
|