Skip to content

Commit

Permalink
implementing #9, extra static text for twitter messages
Browse files Browse the repository at this point in the history
  • Loading branch information
iannesbitt committed Jan 8, 2021
1 parent 19ac6ba commit 825c683
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
29 changes: 24 additions & 5 deletions rsudp/c_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ class Tweeter(rs.ConsumerThread):
:param str consumer_secret: Twitter calls this the "consumer key secret"
:param str access_token: Twitter calls this the "consumer access token"
:param str access_secret: Twitter calls this the "consumer access secret"
:param bool send_images: whether or not to send images. if False, only alerts will be sent.
:param bool tweet_images: whether or not to send images. if False, only alerts will be sent.
:type extra_text: bool or str
:param extra_text: approximately 180 characters to post .
:param queue.Queue q: queue of data and messages sent by :class:`rsudp.c_consumer.Consumer`
'''
def __init__(self, consumer_key, consumer_secret, access_token, access_token_secret,
q=False, tweet_images=False,
q=False, tweet_images=False, extra_text=False,
):
"""
Initialize the process
Expand All @@ -78,6 +80,8 @@ def __init__(self, consumer_key, consumer_secret, access_token, access_token_sec
self.access_token = access_token
self.access_token_secret = access_token_secret

self._resolve_extra_text(extra_text)

if q:
self.queue = q
else:
Expand All @@ -97,7 +101,22 @@ def __init__(self, consumer_key, consumer_secret, access_token, access_token_sec
self.message1 = '(#RaspberryShake station %s.%s%s) Image of event detected at' % (rs.net, rs.stn, self.region)

printM('Starting.', self.sender)



def _resolve_extra_text(self, extra_text):
if ((extra_text == '') or (extra_text == None) or (extra_text == False)):
self.extra_text = ''
else:
extra_text = str(extra_text)
len_ex_txt = len(extra_text)

if len_extra_text > 143:
printW('extra_text parameter is longer than allowable (%s chars) and will be truncated. Please keep extra_text at or below 143 characters.' % len_ex_txt, sender=self.sender)
extra_text = extra_text[:143]

self.extra_text = ' %s' % (extra_text)


def auth(self):
self.twitter = Twython(
self.consumer_key,
Expand Down Expand Up @@ -127,7 +146,7 @@ def _when_alarm(self, d):
'''
event_time = helpers.fsec(helpers.get_msg_time(d))
self.last_event_str = '%s' % (event_time.strftime(self.fmt)[:22])
message = '%s %s UTC - %s' % (self.message0, self.last_event_str, self.livelink)
message = '%s %s UTC%s - %s' % (self.message0, self.last_event_str, self.extra_text, self.livelink)
response = None
try:
printM('Sending tweet...', sender=self.sender)
Expand Down Expand Up @@ -166,7 +185,7 @@ def _when_img(self, d):
if self.tweet_images:
imgpath = helpers.get_msg_path(d)
imgtime = helpers.fsec(helpers.get_msg_time(d))
message = '%s %s UTC' % (self.message1, imgtime.strftime(self.fmt)[:22])
message = '%s %s UTC%s' % (self.message1, imgtime.strftime(self.fmt)[:22], self.extra_text)
response = None
if os.path.exists(imgpath):
with open(imgpath, 'rb') as image:
Expand Down
3 changes: 2 additions & 1 deletion rsudp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,12 @@ def run(settings, debug):
access_token = settings['tweets']['access_token']
access_token_secret = settings['tweets']['access_secret']
tweet_images = settings['tweets']['tweet_images']
extra_text = settings['tweets']['extra_text']

q = mk_q()
tweet = Tweeter(q=q, consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_token_secret,
tweet_images=tweet_images)
tweet_images=tweet_images, extra_text=extra_text)
mk_p(tweet)

if settings['telegram']['enabled']:
Expand Down
3 changes: 2 additions & 1 deletion rsudp/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ def default_settings(output_dir='%s/rsudp' % os.path.expanduser('~').replace('\\
"api_key": "n/a",
"api_secret": "n/a",
"access_token": "n/a",
"access_secret": "n/a"},
"access_secret": "n/a",
"extra_text": ""},
"telegram": {
"enabled": false,
"send_images": true,
Expand Down

0 comments on commit 825c683

Please sign in to comment.