Changeset - 53e747f585b6
[Not reviewed]
default
0 1 0
Dennis Fink - 3 years ago 2022-07-19 19:31:13
dennis.fink@c3l.lu
Add more message with time
1 file changed with 8 insertions and 2 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
import email
 
import email.policy
 
import json
 
import os.path
 
import random
 
import smtplib
 
import ssl
 
from datetime import datetime
 
from functools import partial
 
from time import time
 

	
 
import mastodon
 
import tweepy
 
from flask import current_app, request
 

	
 
default_json_file_v14 = os.path.abspath("default_v14.json")
 
last_state_file_v14 = os.path.abspath("laststate_v14.json")
 

	
 
if not os.path.exists(default_json_file_v14):
 
    raise RuntimeError("default_v14.json does not exists!")
 
elif not os.path.isfile(default_json_file_v14):
 
    raise RuntimeError("default_v14.json is not a file!")
 
@@ -23,34 +23,39 @@ elif not os.path.isfile(default_json_fil
 
standard_open_message = "The space is now open!"
 
standard_close_message = "The space is now closed!"
 

	
 
possible_open_messages = (
 
    standard_open_message,
 
    "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!",
 
    "Let the Chaos begin! We're open!",
 
    "What do we hack now? Come and see, we've just opened!",
 
    "TUWAT! Come and hack. We are open!",
 
    "It's {time:%H:%M} and we are open! Come and hack!",
 
    "We just opened our doors at {time:%H:%M}!",
 
    "Bootup process finished at {time:%H:%M}! Doors opened!",
 
)
 

	
 
possible_closed_messages = (
 
    standard_close_message,
 
    "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!",
 
    "Singularity reached! The space is closed!",
 
    "Dream of electric sheeps! We are closed!",
 
    "We closed our doors at {time:%H:%M}. See you soon.",
 
    "Poweroff process finished at {time:%H:%M}. We're closed!",
 
)
 

	
 

	
 
RADIATON_SUBKEYS = frozenset(("alpha", "beta", "gamma", "beta_gamma"))
 

	
 

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

	
 
        if not hasattr(cls, "_instance_dict"):
 
            cls._instance_dict = {}
 
@@ -135,24 +140,25 @@ class ActiveStatusv14(Singleton, dict):
 

	
 
    def clear_user_present(self):
 
        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"]
 

	
 
    def notify(self):
 
        message = (
 
            random.choice(possible_open_messages)
 
            if self["state"]["open"]
 
            else random.choice(possible_closed_messages)
 
        )
 
        message = message.format(time=datetime.now())
 
        self.send_tweet(message)
 
        self.send_toot(message)
 

	
 
        subject = (
 
            standard_open_message if self["state"]["open"] else standard_close_message
 
        )
 
        self.send_email(subject, message)
 

	
 
    def send_tweet(self, message):
 
        if "TWITTER_CONSUMER_KEY" in current_app.config:
 
            try:
 
                auth = tweepy.OAuthHandler(
 
@@ -215,25 +221,25 @@ class ActiveStatusv14(Singleton, dict):
 
                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:
 
                if "trigger_person" in self["state"]:
 
                    del self["state"]["trigger_person"]
 
            else:
 
                self["state"]["trigger_person"] = trigger_person
 

	
 
            self["state"]["lastchange"] = int(time())
 
            self["state"]["lastchange"] = int(datetime.now().timestamp())
 

	
 
        if message is not None and message:
 
            self["state"]["message"] = message
 

	
 
    def set_sensor_value(self, data, key):
 
        try:
 
            subkey = get_identification_key(data)
 
        except ValueError:
 
            raise
 

	
 
        try:
 
            index = fuzzy_list_find(self["sensors"][key], subkey, data[subkey])
0 comments (0 inline, 0 general)