Changeset - d8371901f34c
[Not reviewed]
default
0 1 0
Dennis Fink - 8 years ago 2017-02-13 16:28:20
dennis.fink@c3l.lu
Added new tweets and added lat and lon to tweets
1 file changed with 8 insertions and 3 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
@@ -8,63 +8,68 @@ from flask import request, current_app
 

	
 
import tweepy
 

	
 

	
 
default_json_file = os.path.abspath('default.json')
 
last_state_file = os.path.abspath('laststate.json')
 

	
 
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!',
 
    'Come by and hack something! We\'ve just opened!',
 
    'The ChaosStuff is now open for everyone!',
 
)
 

	
 
possible_closed_tweets = (
 
    'The space is now closed!',
 
    'We\'re closed now! See you soon.',
 
    'Sorry, we are closed now!',
 
    'The ChaosStuff is now closed! Come back another time!',
 
    'Poweroff process finished! We\'re closed!',
 
)
 

	
 

	
 
def post_tweet(tweet):
 
def post_tweet(tweet, spaceapi=None):
 
    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)
 
        if spaceapi is None:
 
            api.update_status(tweet)
 
        else:
 
            api.update_status(tweet, lat=spaceapi['location']['lat'], lon=spaceapi['location']['lon'])
 

	
 

	
 
class Singleton:
 

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

	
 
        if not hasattr(cls, '_instance_dict'):
 
            cls._instance_dict = {}
 

	
 
        if key not in cls._instance_dict:
 
            cls._instance_dict[key] = super().__new__(cls, *args, **kwargs)
 

	
 
        return cls._instance_dict[key]
 

	
 

	
 
class ActiveStatus(Singleton, dict):
 

	
 
    def reload(self):
 

	
 
        with open(default_json_file, encoding='utf-8') as f:
 
            self.update(json.load(f))
 

	
 
        if os.path.exists(last_state_file) and os.path.isfile(last_state_file):
 
@@ -111,49 +116,49 @@ class ActiveStatus(Singleton, dict):
 

	
 
            if people_now_present['value'] > 0:
 
                people_now_present['value'] -= 1
 

	
 
            if 'names' in people_now_present:
 

	
 
                if username in people_now_present['names']:
 
                    people_now_present['names'].remove(username)
 

	
 
                if not people_now_present['names'] or people_now_present['value'] == 0:
 
                    del people_now_present['names']
 

	
 
            self['sensors']['people_now_present'][0] = people_now_present
 
        else:
 
            pass
 

	
 
    def clear_user_present(self):
 
        self['sensors']['people_now_presents'][0]['value'] = 0
 
        if 'names' in self['sensors']['people_now_present'][0]:
 
            del self['sensors']['people_now_present'][0]['names']
 

	
 
    def send_tweet(self, value):
 
        tweet = random.choice(possible_open_tweets) if value else random.choice(possible_closed_tweets)
 
        try:
 
            post_tweet(tweet)
 
            post_tweet(tweet, self)
 
        except Exception as e:
 
            current_app.logger.error('Sending tweet failed! %s' % e,
 
                                     exc_info=True)
 

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

	
 
        if value is not None and value != self['state']['open']:
 
            self['state']['open'] = value
 

	
 
            self.send_tweet(value)
 

	
 
            if not value:
 
                if 'people_now_present' in self['sensors']:
 
                    self.clear_user_present()
 

	
 
                if 'message' in self['state']:
 
                    del self['state']['message']
 

	
 
            if trigger_person is None:
 
                del self['state']['trigger_person']
 
            else:
 
                self['state']['trigger_person'] = trigger_person
 

	
 
            self['state']['lastchange'] = int(time())
0 comments (0 inline, 0 general)