Changeset - 8b4a6570b373
[Not reviewed]
default
0 1 0
Dennis Fink - 9 years ago 2016-09-19 17:36:46
dennis.fink@c3l.lu
Add random tweets
1 file changed with 29 insertions and 13 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
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!')
 
                tweet = random.choice(possible_open_tweets)
 
                else:
 
                    api.update_status('The space is now closed!')
 
                tweet = random.choice(possible_closed_tweets)
 
            send_tweet(tweet)
 

	
 
            if not value:
 
                if 'people_now_present' in self['sensors']:
0 comments (0 inline, 0 general)