1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-24 22:14:18 +00:00

Aggiunti try/except in punti in cui l'interfaccia con l'API si poteva rompere

This commit is contained in:
g.minoccari 2021-05-29 15:30:38 +02:00
parent 7e8df1cf16
commit e47bb4f187
2 changed files with 22 additions and 15 deletions

View file

@ -2,6 +2,7 @@ from datetime import datetime, timedelta
from nest_backend.database import *
from authentication import authenticate
import smtplib
import tweepy as tw
def is_repo_alert_triggered(repository_id):
repo = Repository.query.filter_by(id=repository_id).first()
@ -35,7 +36,7 @@ def is_repo_alert_triggered(repository_id):
ext.session.commit()
print("alert triggered")
alerts_triggered.append(alert)
send_notification_email(alert)
#send_notification_email(alert)
send_notification_tweet(alert)
@ -47,14 +48,11 @@ def send_notification_email(alert):
conditions_string = conditions_string[:-1]
smtpObj = None
try:
smtpObj = smtplib.SMTP('localhost')
with smtplib.SMTP(host='localhost') as smtpObj:
smtpObj.sendmail("alert@nest.com", owner_repo.email, "Alert triggered")
print("Successfully sent email")
except smtplib.SMTPException:
print("Error: unable to send email")
finally:
if smtpObj is not None:
smtpObj.close()
def send_notification_tweet(alert):
api = authenticate()
@ -63,5 +61,8 @@ def send_notification_tweet(alert):
conditions_string += condition.condition.content + ','
conditions_string = conditions_string[:-1]
print(conditions_string)
try:
api.update_status(f"L'alert {alert.name} è stato attivato! C'è stato un incremento di popolarità negli argomenti di ricerca {conditions_string}")
except tw.errors.Forbidden:
print("Il tweet e' gia' stato pubblicato")

View file

@ -19,6 +19,12 @@ def search_repo_conditions(repository_id):
print(f"Searching tweets from repo: {repo.name}")
evaluation_mode = repo.evaluation_mode
# tweets_repo = [tweet.tweet for tweet in repo.tweets]
# tweets_repo.sort(key=lambda x: x.snowflake)
#
# latest_tweet_id = int(tweets_repo[-1].snowflake) if len(tweets_repo) > 0 else 0
conditions_type = dict()
# Dividing condition into condition types
@ -54,8 +60,8 @@ def search_repo_conditions(repository_id):
for condition_content in conditions_type[ConditionType.coordinates]:
coordinates_tweet = condition_content.content.split()
coordinates_string = coordinates_tweet[2] + "," + coordinates_tweet[3] + "," + str(float(coordinates_tweet[1])/1000) + "km"
print(coordinates_string)
for tweet in tw.Cursor(method=api.search, q="", geocode=coordinates_string).items(10):
print(f"Le coordinate di questa condizione sono: {coordinates_string}")
for tweet in tw.Cursor(method=api.search, q="", geocode=coordinates_string).items(100):
if not Tweet.query.filter_by(snowflake=str(tweet.id)).all():
image_url_list = ''
if 'media' in tweet.entities.keys():
@ -92,17 +98,17 @@ def search_repo_conditions(repository_id):
queryString += ("since:" + condition_content.content[2:] + " " + queryConjunction + " ")
# End of query string
queryString = queryString[:-len(queryConjunction) - 1]
print(queryString)
print(f"La stringa di query finale e':{queryString}")
if evaluation_mode == ConditionMode.all_or:
if queryString != "":
for tweet in tw.Cursor(method=api.search, q=queryString).items(10):
for tweet in tw.Cursor(method=api.search, q=queryString).items(100):
tweetsFound.append(tweet)
print(tweet.user.name + ' : ' + tweet.text + ' : ' + tweet.geo if tweet.geo is not None else '')
print(tweet.user.name + ' : ' + tweet.text + ' : ' + (tweet.geo if tweet.geo is not None else ''))
elif evaluation_mode == ConditionMode.all_and:
for tweet in tw.Cursor(method=api.search, q=queryString, geocode=coordinates_string).items(10):
for tweet in tw.Cursor(method=api.search, q=queryString, geocode=coordinates_string).items(100):
tweetsFound.append(tweet)
print(tweet.user.name + ' : ' + tweet.text + ' : ' + str(tweet.geo))
print(tweet.user.name + ' : ' + tweet.text + ' : ' + (tweet.geo if tweet.geo is not None else ''))
for tweet in tweetsFound:
if not Tweet.query.filter_by(snowflake=str(tweet.id)).all():
image_url_list = ''