-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (111 loc) · 4.81 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from typing import List, Dict
import logging
import re
import time
import asyncio
from mastoBot.configManager import ConfigAccessor
from mastoBot.mastoBot import MastoBot, handleMastodonExceptions
from mastoBot.mastoBot import AccountDict, StatusDict
class MyBot(MastoBot):
@handleMastodonExceptions
def processMention(self, mention: Dict):
api_status: StatusDict = self.getStatus(mention.get("status"))
api_account: AccountDict = self.getAccount(mention.get("account"))
content: str = api_status.get("content")
# Check for report tag
report_pattern = r"(.*?)(?<!\S)\$report\b\s*(.*)</p>"
report_match = re.search(report_pattern, content)
# If report message
if report_match:
before_report = report_match.group(1).strip()
report_message = report_match.group(2).strip()
logging.info(f"⛔ \t Report message received: {report_message}")
template_data = {
"creator": api_account.get("acct"),
"reported_post_id": mention.get("status"),
"reported_post_url": api_status.get("url"),
"report_message": report_message,
}
try:
output = self.getTemplate("report.txt", template_data)
self._api.status_post(status=output, visibility="direct")
except Exception as e:
logging.critical("❗ \t Error posting status message")
raise e
else:
# Check boost and favourite configs
shouldReblog: bool = self.shouldReblog(mention.get("status"))
shouldFavourite: bool = self.shouldFavorite(mention.get("status"))
altTextTestPassed: bool = self.altTextTestPassed(mention.get("status"), "boosts")
# Check boost
if shouldReblog:
try:
self.reblogStatus(mention.get("status"))
except Exception as e:
logging.warning(f"❗ \t Status could not be boosted")
logging.error(e)
elif not altTextTestPassed:
template_data = {"account": api_account.get("acct")}
try:
output = self.getTemplate("missing_alt_text.txt", template_data)
self._api.status_post(status=output, visibility="direct")
except Exception as e:
logging.critical("❗ \t Error sending missing-alt-text message")
raise e
# Check favourite
if shouldFavourite:
try:
self.favoriteStatus(mention.get("status"))
except Exception as e:
logging.warning(f"❗ \t Status could not be favourited")
logging.error(e)
logging.info(f"📬 \t Mention processed: {mention.get('id')}")
self.dismissNotification(mention.get("id"))
@handleMastodonExceptions
def processReblog(self, reblog: Dict):
self.dismissNotification(reblog.get("id"))
@handleMastodonExceptions
def processFavourite(self, favourite: Dict):
self.dismissNotification(favourite.get("id"))
@handleMastodonExceptions
def processFollow(self, follow: Dict):
# Get latest account from the Mastodon API
api_account: AccountDict = self.getAccount(follow.get("account"))
account = api_account.get("acct")
template_data = {"account": account}
# Generate the welcoming message from the template
try:
output = self.getTemplate("new_follow.txt", template_data)
self._api.status_post(status=output, visibility="direct")
except Exception as e:
logging.critical("❗ \t Error posting Status")
raise e
logging.info(f"📭 \t Follow processed: {follow.get('id')}")
self.dismissNotification(follow.get("id"))
@handleMastodonExceptions
def processPoll(self, poll: Dict):
self.dismissNotification(poll.get("id"))
@handleMastodonExceptions
def processFollowRequest(self, follow_request: Dict):
self.dismissNotification(follow_request.get("id"))
@handleMastodonExceptions
def processUpdate(self, update: Dict) -> None:
self.dismissNotification(update.get("id"))
if __name__ == "__main__":
config = ConfigAccessor("config.yml")
credentials = ConfigAccessor("credentials.yml")
bot = MyBot(credentials=credentials, config=config)
async def bot_loop():
await bot.run()
async def timer():
while True:
logging.info('tick')
await asyncio.sleep(5)
async def main():
await asyncio.gather(bot_loop(), timer())
while True:
try:
asyncio.run(main())
except:
time.sleep(10)
pass