Skip to content

Commit 5cf70a6

Browse files
committed
added mastodon support
1 parent 0f3e8bf commit 5cf70a6

5 files changed

Lines changed: 146 additions & 46 deletions

File tree

‎MatrixClient.py‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,3 @@ def send_message(self, message):
2828

2929
def __del__(self):
3030
asyncio.get_event_loop().run_until_complete(self.client.close())
31-
32-
33-
def data_valid():
34-
required_config_data = [
35-
config.MATRIX_SERVER,
36-
config.MATRIX_USER_NAME,
37-
config.MATRIX_PASSWORD,
38-
config.MATRIX_ROOM_ID
39-
]
40-
41-
for config_data in required_config_data:
42-
if config_data == '':
43-
return False
44-
45-
return True

‎config.py‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,12 @@
44
log = logging.getLogger(__name__)
55

66

7-
def setup_logging(_log_level: str) -> None:
8-
"""Configure logging."""
9-
numeric_log_level = getattr(logging, _log_level.upper(), None)
10-
if not numeric_log_level:
11-
raise Exception("Invalid log level: {}".format(_log_level))
12-
logging.basicConfig(level=numeric_log_level)
13-
14-
157
WIKI_SITE = "en.wikipedia.org"
168
WIKI_API_PATH = '/w/'
179
WIKI_VIEW_PATH = '/wiki/'
1810

1911
# log-level: https://docs.python.org/3/library/logging.html#logging-levels
20-
LOG_LEVEL = "INFO"
12+
LOG_LEVEL = logging.ERROR
2113
MAX_ENTRIES = 25
2214
IGNORE_MINOR_CHANGES = False
2315
IGNORE_BOTS = False
@@ -34,6 +26,10 @@ def setup_logging(_log_level: str) -> None:
3426
MATRIX_SERVER = '' # https://matrix-client.matrix.org
3527
MATRIX_ROOM_ID = '' # !aqsASgGGPgMEVHeMdW:cybre.space
3628

29+
MASTODON_DRY_RUN = False
30+
MASTODON_ACCESS_TOKEN = ''
31+
MASTODON_API_BASE_URL = '' # https://botsin.space
32+
3733
USER_NAME_BLACK_LIST = set()
3834

3935

@@ -42,7 +38,7 @@ def setup_logging(_log_level: str) -> None:
4238
except ImportError as ex:
4339
pass
4440

45-
setup_logging(LOG_LEVEL)
41+
logging.basicConfig(level=LOG_LEVEL)
4642

4743
# loop over all local vars and overwrite with found environ vars
4844
for name in list(vars().keys()):

‎helper.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import config
2+
import logging
3+
4+
log = logging.getLogger(__name__)
5+
6+
7+
def any_dry_run():
8+
return config.TWITTER_DRY_RUN or config.MATRIX_DRY_RUN or config.MASTODON_DRY_RUN
9+
10+
11+
def data_valid(data_list):
12+
for config_data in data_list:
13+
if config_data == '':
14+
return False
15+
16+
return True
17+
18+
19+
def can_twitter():
20+
required_config_data = [
21+
config.TWITTER_TOKEN,
22+
config.TWITTER_TOKEN_SECRET,
23+
config.TWITTER_API_KEY,
24+
config.TWITTER_API_SECRET,
25+
]
26+
27+
return data_valid(required_config_data)
28+
29+
30+
def can_mastodon():
31+
required_config_data = [
32+
config.MASTODON_API_BASE_URL,
33+
config.MASTODON_ACCESS_TOKEN
34+
]
35+
36+
return data_valid(required_config_data)
37+
38+
39+
def can_matrix():
40+
required_config_data = [
41+
config.MATRIX_SERVER,
42+
config.MATRIX_USER_NAME,
43+
config.MATRIX_PASSWORD,
44+
config.MATRIX_ROOM_ID
45+
]
46+
47+
return data_valid(required_config_data)

‎messager.py‎

Lines changed: 92 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,84 @@
11
#!/usr/bin/env python3
22
import logging
33
from twython import Twython
4+
from MatrixClient import MatrixClient
5+
from mastodon import Mastodon
46
import config
7+
import helper
58
from readwiki import WikiChange
69
import readwiki
710
import CanMessage
8-
import MatrixClient
911

1012
log = logging.getLogger(__name__)
1113

1214

15+
def get_bot_twitter() -> Twython or None:
16+
if not helper.can_twitter():
17+
return None
18+
19+
log.debug("set up a connection to twitter")
20+
21+
twitter_bot = None
22+
try:
23+
twitter_bot = Twython(
24+
config.TWITTER_API_KEY,
25+
config.TWITTER_API_SECRET,
26+
config.TWITTER_TOKEN,
27+
config.TWITTER_TOKEN_SECRET
28+
)
29+
except Exception as ex:
30+
log.error("ERROR LOGIN TWITTER: " + str(ex))
31+
twitter_bot = None
32+
33+
return twitter_bot
34+
35+
36+
def get_bot_matrix() -> MatrixClient() or None:
37+
if not helper.can_matrix():
38+
return None
39+
40+
log.debug("set up a connection to matrix")
41+
42+
matrix_bot = None
43+
try:
44+
matrix_bot = MatrixClient()
45+
matrix_bot.login()
46+
except Exception as ex:
47+
log.error("ERROR LOGIN MATRIX: " + str(ex))
48+
matrix_bot = None
49+
50+
return matrix_bot
51+
52+
53+
def get_bot_mastodon() -> Mastodon() or None:
54+
if not helper.can_mastodon():
55+
return None
56+
57+
log.debug("set up a connection to mastodon")
58+
59+
mastodon_bot = None
60+
try:
61+
mastodon_bot = Mastodon(
62+
access_token=config.MASTODON_ACCESS_TOKEN,
63+
api_base_url=config.MASTODON_API_BASE_URL
64+
)
65+
except Exception as ex:
66+
log.error("ERROR LOGIN MASTODON: " + str(ex))
67+
mastodon_bot = None
68+
69+
return mastodon_bot
70+
71+
1372
def run(last_revid):
1473
"""
1574
Tweets less than 'max_changes' changes newer than 'last_revid'.
1675
Returns the new 'last_revid'.
1776
"""
1877
log.debug("last rev id: %s", last_revid)
1978

20-
log.debug("set up a connection to the twitter api")
21-
twitter = Twython(
22-
config.TWITTER_API_KEY,
23-
config.TWITTER_API_SECRET,
24-
config.TWITTER_TOKEN,
25-
config.TWITTER_TOKEN_SECRET
26-
)
27-
28-
matrix_bot = None
29-
if MatrixClient.data_valid():
30-
log.debug("set up a connection to matrix")
31-
matrix_bot = MatrixClient.MatrixClient()
32-
matrix_bot.login()
79+
twitter_bot = get_bot_twitter()
80+
matrix_bot = get_bot_matrix()
81+
mastodon_bot = get_bot_mastodon()
3382

3483
changes = list()
3584
changes_overflow = False
@@ -62,8 +111,8 @@ def run(last_revid):
62111

63112
for change in changes:
64113
message = change.get_message()
65-
tweet(twitter, message)
66-
toot(None, message)
114+
tweet(twitter_bot, message)
115+
toot(mastodon_bot, message)
67116
matrix(matrix_bot, message)
68117

69118
if not changes:
@@ -75,24 +124,46 @@ def run(last_revid):
75124

76125

77126
def tweet(twitter, message):
78-
logging.debug("Tweeting: %s", message)
127+
if twitter is None:
128+
return
129+
130+
logging.debug("Send tweet: %s", message)
79131

80132
if not config.TWITTER_DRY_RUN:
81-
twitter.update_status(status=message)
133+
try:
134+
twitter.update_status(status=message)
135+
except Exception as ex:
136+
log.error('TWITTER SEND ERROR: ' + str(ex))
82137
else:
83138
log.warning('TWITTER DRY RUN!')
84139

85140

86141
def toot(_mastodon, message):
87-
log.warning('MASTODON NOT IMPLEMENTED')
142+
if _mastodon is None:
143+
return
144+
145+
logging.debug("Send toot: %s", message)
146+
147+
if not config.MASTODON_DRY_RUN:
148+
try:
149+
_mastodon.toot(message)
150+
except Exception as ex:
151+
log.error('MASTODON SEND ERROR: ' + str(ex))
152+
else:
153+
log.warning('MASTODON DRY RUN!')
88154

89155

90156
def matrix(_matrix, message):
91157
if _matrix is None:
92158
return
93159

160+
logging.debug("Send matrix: %s", message)
161+
94162
if not config.MATRIX_DRY_RUN:
95-
_matrix.send_message(message)
163+
try:
164+
_matrix.send_message(message)
165+
except Exception as ex:
166+
log.error('MATRIX SEND ERROR: ' + str(ex))
96167
else:
97168
log.warning('MATRIX DRY RUN!')
98169

@@ -103,7 +174,7 @@ def matrix(_matrix, message):
103174
highest_ref_id = run(can_message.last_revid)
104175
log.info("finished, got %s as the latest revid", highest_ref_id)
105176

106-
if config.TWITTER_DRY_RUN or config.MATRIX_DRY_RUN:
177+
if helper.any_dry_run():
107178
log.info("DRY_RUN, don't save revid")
108179
else:
109180
can_message.set_last_revid(highest_ref_id)

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
mwclient
22
twython
33
matrix-nio
4+
Mastodon.py

0 commit comments

Comments
 (0)