1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-28 23:44:19 +00:00
pds-2021-g2-nest/nest_crawler/alert_trigger.py

77 lines
3.1 KiB
Python
Raw Normal View History

2021-05-27 09:21:51 +00:00
from datetime import datetime, timedelta
from nest_backend.database import *
2021-05-28 13:40:39 +00:00
from .authentication import authenticate
2021-05-27 09:21:51 +00:00
import smtplib
2021-05-30 15:07:19 +00:00
import os
import tweepy as tw
2021-05-30 15:07:19 +00:00
MESSAGE = "{alert_name}: la soglia di allerta è stata superata alle {now}!"
2021-05-27 09:21:51 +00:00
2021-05-28 13:40:39 +00:00
2021-05-27 09:21:51 +00:00
def is_repo_alert_triggered(repository_id):
repo = Repository.query.filter_by(id=repository_id).first()
if repo is None:
print("Non esiste una repository con questo id")
return False
alerts = [alert for alert in repo.alerts]
alerts_triggered = []
for alert in alerts:
evaluation_mode = alert.evaluation_mode
conditions = [condition.condition for condition in alert.conditions]
repo_tweets = [tweet.tweet for tweet in repo.tweets]
alert_tweets = set(repo_tweets)
if evaluation_mode == ConditionMode.all_and:
for condition in conditions:
alert_tweets.intersection(set([tweet.tweet for tweet in condition.tweets]))
elif evaluation_mode == ConditionMode.all_or:
conditions_tweet = set()
for condition in conditions:
conditions_tweet.update([tweet.tweet for tweet in condition.tweets])
alert_tweets = alert_tweets.intersection(conditions_tweet)
end_time = datetime.now()
2021-05-30 15:35:49 +00:00
window_size_hours = timedelta(hours=alert.window_size)
2021-05-27 09:21:51 +00:00
last_notification_time = min([notification.ora for notification in alert.notifications] if len(alert.notifications)>0 else [end_time - window_size_hours])
start_time = max(end_time - window_size_hours, last_notification_time)
alert_tweets = [tweet for tweet in alert_tweets if (end_time > tweet.insert_time > start_time)]
2021-05-30 15:35:49 +00:00
print(f"I tweet corrispondenti sono: {len(alert_tweets)}")
2021-05-27 09:21:51 +00:00
if len(alert_tweets) >= alert.limit:
alert_notification = Notification(ora=str(datetime.now()), alert_id=alert.id)
ext.session.add(alert_notification)
ext.session.commit()
2021-05-30 15:35:49 +00:00
print("Alert triggered!")
2021-05-27 09:21:51 +00:00
alerts_triggered.append(alert)
2021-05-30 15:07:19 +00:00
send_notification_email(alert)
2021-05-27 09:21:51 +00:00
send_notification_tweet(alert)
def send_notification_email(alert):
try:
2021-05-30 15:20:16 +00:00
with smtplib.SMTP(host=os.environ["SMTP_HOST"], port=587) as smtpObj:
2021-05-30 15:07:19 +00:00
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.ehlo()
smtpObj.login(os.environ["SMTP_USERNAME"], os.environ["SMTP_PASSWORD"])
smtpObj.sendmail(os.environ["SMTP_FROM_EMAIL"],
alert.repository.owner.email,
2021-05-30 15:26:22 +00:00
MESSAGE.format(alert_name=alert.name, now=datetime.now().isoformat()).encode("utf8"))
print("Successfully sent email")
2021-05-27 09:21:51 +00:00
except smtplib.SMTPException:
print("Error: unable to send email")
2021-05-28 13:40:39 +00:00
2021-05-27 09:21:51 +00:00
def send_notification_tweet(alert):
api = authenticate()
try:
2021-05-30 15:23:29 +00:00
api.update_status(MESSAGE.format(alert_name=alert.name, now=datetime.now().isoformat()))
except tw.errors.Forbidden:
print("Il tweet e' gia' stato pubblicato")
2021-05-27 09:21:51 +00:00
2021-05-28 13:40:39 +00:00
__all__ = (
2021-05-30 15:35:49 +00:00
"MESSAGE",
2021-05-28 13:40:39 +00:00
"is_repo_alert_triggered",
"send_notification_email",
"send_notification_tweet",
)