Changeset - 856635b4eef4
[Not reviewed]
notifyEmail
0 1 0
Dennis Fink - 3 years ago 2022-06-26 11:29:38
dennis.fink@c3l.lu
Apply consistent style and use values from config in post_email
1 file changed with 14 insertions and 14 deletions:
0 comments (0 inline, 0 general)
spaceapi/utils.py
Show inline comments
 
import email
 
import json
 
import os.path
 
import random
 
import smtplib
 
import email
 
from functools import wraps
 
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!")
 
@@ -65,34 +65,38 @@ def post_tweet(tweet, spaceapi=None):
 
def post_toot(toot):
 
    if "MASTODON_USERCRED_FILE" in current_app.config:
 
        api = mastodon.Mastodon(
 
            client_id="c3l_spaceapi_clientcred.secret",
 
            access_token=current_app.config["MASTODON_USERCRED_FILE"],
 
            api_base_url="https://chaos.social",
 
        )
 
        api.status_post(toot, visibility="unlisted")
 

	
 

	
 
def post_email(subject, body):
 
    if "EMAIL_PASS" in current_app.config:
 
        smtpConn = smtplib.SMTP("EMAIL_HOST", port="EMAIL_PORT")
 
        sslContext = ssl.create_default_context(Purpose.SERVER_AUTH)
 
        smtpConn.starttls(sslContext)
 
        smtpConn.login("EMAIL_USER", "EMAIL_PASS")
 
        smtp_conn = smtplib.SMTP(
 
            current_app.config["EMAIL_HOST"], port=current_app.config["EMAIL_PORT"]
 
        )
 
        ssl_context = ssl.create_default_context(Purpose.SERVER_AUTH)
 
        smtp_conn.starttls(ssl_context)
 
        smtp_conn.login(
 
            current_app.config["EMAIL_USER"], current_app.config["EMAIL_PASS"]
 
        )
 
        msg = email.message.EmailMessage(email.policy.SMTP)
 
        msg['To'] = "EMAIL_ANNOUNCE_ADDRESS"
 
        msg['From'] = "EMAIL_USER"
 
        msg['Subject'] = subject
 
        msg["To"] = current_app.config["EMAIL_ANNOUNCE_ADDRESS"]
 
        msg["From"] = current_app.config["EMAIL_USER"]
 
        msg["Subject"] = subject
 
        msg.set_content(body)
 
        smtpConn.send_message(msg)
 
        smtp_conn.send_message(msg)
 

	
 

	
 
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)
 

	
 
@@ -184,29 +188,25 @@ class ActiveStatusv14(Singleton, dict):
 
        )
 
        try:
 
            post_tweet(tweet, self)
 
        except Exception as e:
 
            current_app.logger.error("Sending tweet failed! %s" % e, exc_info=True)
 

	
 
        try:
 
            post_toot(tweet)
 
        except Exception as e:
 
            current_app.logger.error("Sending toot failed! %s" % e, exc_info=True)
 

	
 
    def send_email(self, value):
 
        subject = (
 
            standard_open_message
 
            if value
 
            else standard_close_message
 
        )
 
        subject = standard_open_message if value else standard_close_message
 
        try:
 
            post_email(message)
 
        except Exception as e:
 
            current_app.logger.error("Sending email 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)
 
            self.send_email(value)
0 comments (0 inline, 0 general)