2021-11-20 15:40:10 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-10-19 07:35:18 +02:00
|
|
|
from tweepy import OAuthHandler, API, TweepyException
|
2021-10-17 14:05:16 +02:00
|
|
|
from tweepy.models import Status
|
|
|
|
|
2022-12-11 14:15:04 +01:00
|
|
|
from mobilizon_reshare.dataclasses import MobilizonEvent
|
2021-10-17 14:05:16 +02:00
|
|
|
from mobilizon_reshare.publishers.abstract import (
|
|
|
|
AbstractPlatform,
|
|
|
|
AbstractEventFormatter,
|
|
|
|
)
|
|
|
|
from mobilizon_reshare.publishers.exceptions import (
|
|
|
|
InvalidCredentials,
|
|
|
|
PublisherError,
|
2021-11-27 23:31:44 +01:00
|
|
|
InvalidMessage,
|
2021-10-17 14:05:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TwitterFormatter(AbstractEventFormatter):
|
|
|
|
_conf = ("publisher", "twitter")
|
2021-10-17 14:09:24 +02:00
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
def _validate_event(self, event: MobilizonEvent) -> None:
|
2022-03-02 08:59:49 +01:00
|
|
|
pass # pragma: no cover
|
2021-10-17 14:05:16 +02:00
|
|
|
|
2021-11-27 23:31:44 +01:00
|
|
|
def _validate_message(self, message) -> None:
|
2021-10-19 07:35:18 +02:00
|
|
|
# TODO this is not precise. It should count the characters according to Twitter's logic but
|
|
|
|
# Tweepy doesn't seem to support the validation client side
|
|
|
|
if len(message.encode("utf-8")) > 280:
|
2021-11-27 23:31:44 +01:00
|
|
|
self._log_error("Message is too long", raise_error=InvalidMessage)
|
2021-10-17 14:05:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TwitterPlatform(AbstractPlatform):
|
|
|
|
"""
|
|
|
|
Twitter publisher class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_conf = ("publisher", "twitter")
|
2021-10-24 21:32:28 +02:00
|
|
|
name = "twitter"
|
2021-10-17 14:05:16 +02:00
|
|
|
|
|
|
|
def _get_api(self):
|
|
|
|
|
|
|
|
api_key = self.conf.api_key
|
|
|
|
api_key_secret = self.conf.api_key_secret
|
|
|
|
access_token = self.conf.access_token
|
|
|
|
access_secret = self.conf.access_secret
|
|
|
|
auth = OAuthHandler(api_key, api_key_secret)
|
|
|
|
auth.set_access_token(access_token, access_secret)
|
|
|
|
return API(auth)
|
|
|
|
|
2021-11-20 15:40:10 +01:00
|
|
|
def _send(self, message: str, event: Optional[MobilizonEvent] = None) -> Status:
|
2021-10-19 07:35:18 +02:00
|
|
|
try:
|
|
|
|
return self._get_api().update_status(message)
|
|
|
|
except TweepyException as e:
|
2021-11-27 23:31:44 +01:00
|
|
|
self._log_error(e.args[0], raise_error=PublisherError)
|
2021-10-17 14:05:16 +02:00
|
|
|
|
|
|
|
def validate_credentials(self):
|
|
|
|
if not self._get_api().verify_credentials():
|
|
|
|
self._log_error(
|
|
|
|
"Invalid Twitter credentials. Authentication Failed",
|
|
|
|
raise_error=InvalidCredentials,
|
|
|
|
)
|
|
|
|
|
|
|
|
def _validate_response(self, res: Status) -> dict:
|
2022-03-02 08:59:49 +01:00
|
|
|
pass # pragma: no cover
|
2021-10-17 14:05:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TwitterPublisher(TwitterPlatform):
|
|
|
|
|
|
|
|
_conf = ("publisher", "twitter")
|
|
|
|
|
|
|
|
|
|
|
|
class TwitterNotifier(TwitterPlatform):
|
|
|
|
|
|
|
|
_conf = ("notifier", "twitter")
|