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 31 insertions and 15 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
 

	
 
import tweepy
 

	
 

	
 
@@ -12,12 +13,37 @@ last_state_file = os.path.abspath('lasts
 

	
 
if not os.path.exists(default_json_file):
 
    raise RuntimeError('default.json does not exists!')
 
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:
 

	
 
    def __new__(cls, *args, **kwargs):
 
        key = str(hash(cls))
 

	
 
@@ -95,27 +121,17 @@ class ActiveStatus(Singleton, dict):
 

	
 
    def set_new_state(self, value=None, trigger_person=None, message=None):
 

	
 
        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']:
 
                    self['sensors']['people_now_present'][0]['value'] = 0
 
                    if 'names' in self['sensors']['people_now_present'][0]:
 
                        del self['sensors']['people_now_present'][0]['names']
0 comments (0 inline, 0 general)