ticketfrei/active_bots/mastodonbot.py

115 lines
4.8 KiB
Python
Raw Normal View History

2018-03-28 15:36:35 +00:00
#!/usr/bin/env python3
from bot import Bot
import logging
2019-05-17 18:42:13 +00:00
import mastodon
2018-03-28 15:36:35 +00:00
import re
from report import Report
logger = logging.getLogger(__name__)
class MastodonBot(Bot):
def crawl(self, user):
"""
Crawl mentions from Mastodon.
:return: list of statuses
"""
mentions = []
try:
2019-05-17 18:42:13 +00:00
m = mastodon.Mastodon(*user.get_masto_credentials())
except TypeError:
# No Mastodon Credentials in database.
return mentions
2018-03-28 15:36:35 +00:00
try:
notifications = m.notifications()
except mastodon.MastodonNetworkError:
2020-06-16 17:05:07 +00:00
logger.error("Mastodon Network Error.")
return mentions
2020-07-26 23:14:59 +00:00
except mastodon.MastodonAPIError:
try:
logger.error("Mastodon API Error: " + m.instance()['urls']['streaming_api'] + ", city: " + str(user.uid))
except mastodon.MastodonServerError:
logger.error("Mastodon Server Error 500, can't get instance.")
return mentions
except mastodon.MastodonInternalServerError:
try:
logger.error("Mastodon Error: 500. Server: " + m.instance()['urls']['streaming_api'])
except mastodon.MastodonServerError:
logger.error("Mastodon Server Error 500, can't get instance.")
2020-07-26 23:14:59 +00:00
except mastodon.MastodonVersionError:
logger.error("Mastodon Server Error 500, server version too low.")
return mentions
except mastodon.MastodonBadGatewayError:
try:
logger.error("Mastodon Error: 502. Server: " + m.instance()['urls']['streaming_api'])
except mastodon.MastodonServerError:
logger.error("Mastodon Server Error 502, can't get instance.")
2018-03-28 15:36:35 +00:00
return mentions
except mastodon.MastodonServiceUnavailableError:
try:
logger.error("Mastodon Error: 503. Server: " + m.instance()['urls']['streaming_api'])
except mastodon.MastodonServerError:
logger.error("Mastodon Server Error 503, can't get instance.")
return mentions
except mastodon.MastodonGatewayTimeoutError:
try:
logger.error("Mastodon Error: 504. Server: " + m.instance()['urls']['streaming_api'])
except mastodon.MastodonServerError:
logger.error("Mastodon Server Error 504, can't get instance.")
return mentions
except mastodon.MastodonServerError:
try:
logger.error("Unknown Mastodon Server Error. Server: " + m.instance()['urls']['streaming_api'], exc_info=True)
except mastodon.MastodonServerError:
logger.error("Unknown Mastodon Server Error.", exc_info=True)
return mentions
2018-03-28 15:36:35 +00:00
for status in notifications:
2020-07-26 23:14:59 +00:00
try:
if (status['type'] == 'mention' and
2018-09-24 18:54:57 +00:00
not user.toot_is_seen(status['status']['uri'])):
2020-07-26 23:14:59 +00:00
# save state
user.toot_witness(status['status']['uri'])
# add mention to mentions
text = re.sub(r'<[^>]*>', '', status['status']['content'])
text = re.sub(
"(?<=^|(?<=[^a-zA-Z0-9-_.]))@([A-Za-z]+[A-Za-z0-9-_]+)",
"", text)
if status['status']['visibility'] == 'public':
mentions.append(Report(status['account']['acct'],
self,
text,
status['status']['id'],
status['status']['created_at']))
else:
mentions.append(Report(status['account']['acct'],
'mastodonPrivate',
text,
status['status']['id'],
status['status']['created_at']))
except TypeError:
pass
2018-03-28 15:36:35 +00:00
return mentions
def post(self, user, report):
try:
2019-05-17 18:42:13 +00:00
m = mastodon.Mastodon(*user.get_masto_credentials())
except TypeError:
return # no mastodon account for this user.
2018-03-28 15:36:35 +00:00
if report.source == self:
try:
m.status_reblog(report.id)
except Exception:
logger.error('Error boosting: ' + report.id, exc_info=True)
2018-03-28 15:36:35 +00:00
else:
2018-04-15 09:42:34 +00:00
text = report.text
if len(text) > 500:
text = text[:500 - 4] + u' ...'
try:
2018-04-15 09:42:34 +00:00
m.toot(text)
except Exception:
2018-09-13 15:33:33 +00:00
logger.error('Error tooting: ' + user.get_city() + ': ' +
report.id, exc_info=True)