mirror of
https://github.com/Tech-Workers-Coalition-Italia/mobilizon-reshare.git
synced 2025-02-27 08:57:45 +01:00
* fixed parsing bug * implemented events and publications endpoints split endpoints by entity removed credentials * add pagination (#179) * added pagination * integrated pagination with tortoise * added test for publications * removed converter file * moved publications to dataclasses module * implemented import pattern on dataclasses to prevent circular imports * removed redundant fetch * removed unused query * split build_publications * split failed_publications * removed redundant query functions * split publication retrieve * split all read functions * removed redundant write function * fixed lock
99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
from typing import Optional
|
|
|
|
import facebook
|
|
import pkg_resources
|
|
from facebook import GraphAPIError
|
|
|
|
from mobilizon_reshare.dataclasses import MobilizonEvent
|
|
from mobilizon_reshare.formatting.description import html_to_plaintext
|
|
from mobilizon_reshare.publishers.abstract import (
|
|
AbstractPlatform,
|
|
AbstractEventFormatter,
|
|
)
|
|
from mobilizon_reshare.publishers.exceptions import (
|
|
InvalidCredentials,
|
|
InvalidEvent,
|
|
InvalidMessage,
|
|
PublisherError,
|
|
)
|
|
|
|
|
|
class FacebookFormatter(AbstractEventFormatter):
|
|
|
|
_conf = ("publisher", "facebook")
|
|
default_template_path = pkg_resources.resource_filename(
|
|
"mobilizon_reshare.publishers.templates", "facebook.tmpl.j2"
|
|
)
|
|
|
|
default_recap_template_path = pkg_resources.resource_filename(
|
|
"mobilizon_reshare.publishers.templates", "facebook_recap.tmpl.j2"
|
|
)
|
|
|
|
default_recap_header_template_path = pkg_resources.resource_filename(
|
|
"mobilizon_reshare.publishers.templates", "facebook_recap_header.tmpl.j2"
|
|
)
|
|
|
|
def _validate_event(self, event: MobilizonEvent) -> None:
|
|
text = event.description
|
|
if not (text and text.strip()):
|
|
self._log_error("No text was found", raise_error=InvalidEvent)
|
|
|
|
def _validate_message(self, message) -> None:
|
|
if len(message) >= 63200:
|
|
self._log_error("Message is too long", raise_error=InvalidMessage)
|
|
|
|
def _preprocess_event(self, event: MobilizonEvent):
|
|
event.description = html_to_plaintext(event.description)
|
|
event.name = html_to_plaintext(event.name)
|
|
return event
|
|
|
|
|
|
class FacebookPlatform(AbstractPlatform):
|
|
"""
|
|
Facebook publisher class.
|
|
"""
|
|
|
|
name = "facebook"
|
|
|
|
def _get_api(self) -> facebook.GraphAPI:
|
|
return facebook.GraphAPI(access_token=self.conf["page_access_token"])
|
|
|
|
def _send(self, message: str, event: Optional[MobilizonEvent] = None):
|
|
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,
|
|
)
|
|
|
|
def validate_credentials(self):
|
|
|
|
try:
|
|
self._log_debug("Validating Facebook credentials")
|
|
self._get_api().get_object(id="me", field="name")
|
|
except GraphAPIError:
|
|
self._log_error(
|
|
"Invalid Facebook credentials. Authentication Failed",
|
|
raise_error=InvalidCredentials,
|
|
)
|
|
|
|
self._log_debug("Facebook credentials are valid")
|
|
|
|
def _validate_response(self, response):
|
|
pass
|
|
|
|
|
|
class FacebookPublisher(FacebookPlatform):
|
|
|
|
_conf = ("publisher", "facebook")
|
|
|
|
|
|
class FacebookNotifier(FacebookPlatform):
|
|
|
|
_conf = ("notifier", "facebook")
|