diff --git a/spaceapi/utils.py b/spaceapi/utils.py --- a/spaceapi/utils.py +++ b/spaceapi/utils.py @@ -1,6 +1,7 @@ import json import os.path from time import time +import random from flask import request, current_app @@ -15,6 +16,31 @@ if not os.path.exists(default_json_file) elif not os.path.isfile(default_json_file): raise RuntimeError('default.json is not a file!') +possible_open_tweets = { + 'The space is now open!', + 'The space is open! Come in and hack something!', + 'Yes, we\'re open! Come in and create something!', +} + +possible_closed_tweets = { + 'The space is now closed!', + 'We\'re closed now! See you soon.', +} + + +def send_tweet(tweet): + if 'TWITTER_CONSUMER_KEY' in current_app.config: + auth = tweepy.OAuthHandler( + current_app.config['TWITTER_CONSUMER_KEY'], + current_app.config['TWITTER_CONSUMER_SECRET'] + ) + auth.set_access_token( + current_app.config['TWITTER_ACCESS_TOKEN_KEY'], + current_app.config['TWITTER_ACCESS_TOKEN_SECRET'] + ) + api = tweepy.API(auth) + api.update_status(tweet) + class Singleton: @@ -98,21 +124,11 @@ class ActiveStatus(Singleton, dict): if value is not None: self['state']['open'] = value - if 'TWITTER_CONSUMER_KEY' in current_app.config: - auth = tweepy.OAuthHandler( - current_app.config['TWITTER_CONSUMER_KEY'], - current_app.config['TWITTER_CONSUMER_SECRET'] - ) - auth.set_access_token( - current_app.config['TWITTER_ACCESS_TOKEN_KEY'], - current_app.config['TWITTER_ACCESS_TOKEN_SECRET'] - ) - api = tweepy.API(auth) - - if value: - api.update_status('The space is now open!') - else: - api.update_status('The space is now closed!') + if value: + tweet = random.choice(possible_open_tweets) + else: + tweet = random.choice(possible_closed_tweets) + send_tweet(tweet) if not value: if 'people_now_present' in self['sensors']: