11#!/usr/bin/env python3
22import logging
33from twython import Twython
4+ from MatrixClient import MatrixClient
5+ from mastodon import Mastodon
46import config
7+ import helper
58from readwiki import WikiChange
69import readwiki
710import CanMessage
8- import MatrixClient
911
1012log = 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+
1372def 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
77126def 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
86141def 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
90156def 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 )
0 commit comments