Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessager.py
More file actions
Latest commit
214 lines (159 loc) · 5.48 KB
/
Copy pathmessager.py
File metadata and controls
214 lines (159 loc) · 5.48 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/usr/bin/env python3
importlogging
fromrequestsimportReadTimeout
fromtwythonimportTwython
fromclients.MatrixClientimportMatrixClient
fromclients.DiscordClientimportDiscordClient
frommastodonimportMastodon
importconfig
importhelper
fromreadwikiimportReadWiki
importCanMessage
log=logging.getLogger(__name__)
defget_bot_twitter() ->TwythonorNone:
ifnothelper.can_twitter():
returnNone
log.debug("set up a connection to twitter")
twitter_bot=None
try:
twitter_bot=Twython(
config.TWITTER_API_KEY,
config.TWITTER_API_SECRET,
config.TWITTER_TOKEN,
config.TWITTER_TOKEN_SECRET
)
exceptExceptionasex:
log.error("ERROR LOGIN TWITTER: "+str(ex))
twitter_bot=None
returntwitter_bot
defget_bot_matrix() ->MatrixClient() orNone:
ifnothelper.can_matrix():
returnNone
log.debug("set up a connection to matrix")
matrix_bot=None
try:
matrix_bot=MatrixClient()
matrix_bot.login()
exceptExceptionasex:
log.error("ERROR LOGIN MATRIX: "+str(ex))
matrix_bot=None
returnmatrix_bot
defget_bot_mastodon() ->Mastodon() orNone:
ifnothelper.can_mastodon():
returnNone
log.debug("set up a connection to mastodon")
mastodon_bot=None
try:
mastodon_bot=Mastodon(
access_token=config.MASTODON_ACCESS_TOKEN,
api_base_url=config.MASTODON_API_BASE_URL
)
exceptExceptionasex:
log.error("ERROR LOGIN MASTODON: "+str(ex))
mastodon_bot=None
returnmastodon_bot
defget_bot_discord() ->DiscordClient() orNone:
ifnothelper.can_discord():
returnNone
log.debug("initialise discord client")
returnDiscordClient()
defrun(last_revid):
"""
Sends messages less than 'max_changes' changes newer than 'last_revid'.
Returns the new 'last_revid'.
"""
log.debug("last rev id: %s", last_revid)
twitter_bot=get_bot_twitter() ifconfig.TWITTER_ACTIVEelseNone
matrix_bot=get_bot_matrix() ifconfig.MATRIX_ACTIVEelseNone
mastodon_bot=get_bot_mastodon() ifconfig.MASTODON_ACTIVEelseNone
discord_bot=get_bot_discord() ifconfig.DISCORD_ACTIVEelseNone
changes=list()
changes_overflow=False
new_messages_count=0
try:
read_wiki=ReadWiki()
forchangeinread_wiki.get_changes():
ifchange.revId<=last_revid:
break
new_messages_count+=1
changes.append(change)
ifnew_messages_count>=config.MAX_ENTRIES:
changes_overflow=True
break
exceptReadTimeoutasex:
log.debug("Read TimeOut: %s", ex)
log.debug("got the following changes: %s", changes)
changes.reverse()
ifchanges_overflow:
log.debug("detected a overflow in changes")
missing_message= \
"Limit of {} changes reached. Please see https://{}{}Special:RecentChanges for more information."\
.format(config.MAX_ENTRIES, config.WIKI_SITE, config.WIKI_VIEW_PATH)
missing_link=ReadWiki.WikiChange()
missing_link.revId=changes[-1].revId
missing_link.set_message(missing_message)
changes.insert(0, missing_link)
forchangeinchanges:
message=change.get_message()
send_tweet(twitter_bot, message)
send_toot(mastodon_bot, message)
send_matrix(matrix_bot, message)
send_discord(discord_bot, message)
ifnotchanges:
log.debug("no changes, rev id: %s", last_revid)
returnlast_revid
log.debug("new rev id: %s", changes[-1].revId)
returnchanges[-1].revId
defsend_tweet(twitter, message):
iftwitterisNone:
return
logging.debug("Send tweet: %s", message)
ifnotconfig.TWITTER_DRY_RUN:
try:
twitter.update_status(status=message)
exceptExceptionasex:
log.error('TWITTER SEND ERROR: '+str(ex))
else:
log.warning('TWITTER DRY RUN!')
defsend_toot(_mastodon, message):
if_mastodonisNone:
return
logging.debug("Send toot: %s", message)
ifnotconfig.MASTODON_DRY_RUN:
try:
_mastodon.toot(message)
exceptExceptionasex:
log.error('MASTODON SEND ERROR: '+str(ex))
else:
log.warning('MASTODON DRY RUN!')
defsend_matrix(_matrix, message):
if_matrixisNone:
return
logging.debug("Send matrix: %s", message)
ifnotconfig.MATRIX_DRY_RUN:
try:
_matrix.send_message(message)
exceptExceptionasex:
log.error('MATRIX SEND ERROR: '+str(ex))
else:
log.warning('MATRIX DRY RUN!')
defsend_discord(_discord, message):
if_discordisNone:
return
logging.debug("Send discord: %s", message)
ifnotconfig.DISCORD_DRY_RUN:
try:
_discord.send_message(message)
exceptExceptionasex:
log.error('DISCORD SEND ERROR: '+str(ex))
else:
log.warning('DISCORD DRY RUN!')
if__name__=='__main__':
can_message=CanMessage.CanMessage()
highest_ref_id=run(can_message.last_revid)
log.info("finished, got %s as the latest revid", highest_ref_id)
ifhelper.any_dry_run():
log.warning("DRY_RUN, don't save %s revid", highest_ref_id)
else:
can_message.set_last_revid(highest_ref_id)
can_message.save()