mirror of
https://github.com/pds-nest/nest.git
synced 2024-11-21 20:44:18 +00:00
Aggiunti try/except in punti in cui l'interfaccia con l'API si poteva rompere
This commit is contained in:
parent
7e8df1cf16
commit
e47bb4f187
2 changed files with 22 additions and 15 deletions
|
@ -2,6 +2,7 @@ from datetime import datetime, timedelta
|
||||||
from nest_backend.database import *
|
from nest_backend.database import *
|
||||||
from authentication import authenticate
|
from authentication import authenticate
|
||||||
import smtplib
|
import smtplib
|
||||||
|
import tweepy as tw
|
||||||
|
|
||||||
def is_repo_alert_triggered(repository_id):
|
def is_repo_alert_triggered(repository_id):
|
||||||
repo = Repository.query.filter_by(id=repository_id).first()
|
repo = Repository.query.filter_by(id=repository_id).first()
|
||||||
|
@ -35,7 +36,7 @@ def is_repo_alert_triggered(repository_id):
|
||||||
ext.session.commit()
|
ext.session.commit()
|
||||||
print("alert triggered")
|
print("alert triggered")
|
||||||
alerts_triggered.append(alert)
|
alerts_triggered.append(alert)
|
||||||
send_notification_email(alert)
|
#send_notification_email(alert)
|
||||||
send_notification_tweet(alert)
|
send_notification_tweet(alert)
|
||||||
|
|
||||||
|
|
||||||
|
@ -47,14 +48,11 @@ def send_notification_email(alert):
|
||||||
conditions_string = conditions_string[:-1]
|
conditions_string = conditions_string[:-1]
|
||||||
smtpObj = None
|
smtpObj = None
|
||||||
try:
|
try:
|
||||||
smtpObj = smtplib.SMTP('localhost')
|
with smtplib.SMTP(host='localhost') as smtpObj:
|
||||||
smtpObj.sendmail("alert@nest.com", owner_repo.email, "Alert triggered")
|
smtpObj.sendmail("alert@nest.com", owner_repo.email, "Alert triggered")
|
||||||
print("Successfully sent email")
|
print("Successfully sent email")
|
||||||
except smtplib.SMTPException:
|
except smtplib.SMTPException:
|
||||||
print("Error: unable to send email")
|
print("Error: unable to send email")
|
||||||
finally:
|
|
||||||
if smtpObj is not None:
|
|
||||||
smtpObj.close()
|
|
||||||
|
|
||||||
def send_notification_tweet(alert):
|
def send_notification_tweet(alert):
|
||||||
api = authenticate()
|
api = authenticate()
|
||||||
|
@ -63,5 +61,8 @@ def send_notification_tweet(alert):
|
||||||
conditions_string += condition.condition.content + ','
|
conditions_string += condition.condition.content + ','
|
||||||
conditions_string = conditions_string[:-1]
|
conditions_string = conditions_string[:-1]
|
||||||
print(conditions_string)
|
print(conditions_string)
|
||||||
api.update_status(f"L'alert {alert.name} è stato attivato! C'è stato un incremento di popolarità negli argomenti di ricerca {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")
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,12 @@ def search_repo_conditions(repository_id):
|
||||||
|
|
||||||
print(f"Searching tweets from repo: {repo.name}")
|
print(f"Searching tweets from repo: {repo.name}")
|
||||||
evaluation_mode = repo.evaluation_mode
|
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()
|
conditions_type = dict()
|
||||||
|
|
||||||
# Dividing condition into condition types
|
# Dividing condition into condition types
|
||||||
|
@ -54,8 +60,8 @@ def search_repo_conditions(repository_id):
|
||||||
for condition_content in conditions_type[ConditionType.coordinates]:
|
for condition_content in conditions_type[ConditionType.coordinates]:
|
||||||
coordinates_tweet = condition_content.content.split()
|
coordinates_tweet = condition_content.content.split()
|
||||||
coordinates_string = coordinates_tweet[2] + "," + coordinates_tweet[3] + "," + str(float(coordinates_tweet[1])/1000) + "km"
|
coordinates_string = coordinates_tweet[2] + "," + coordinates_tweet[3] + "," + str(float(coordinates_tweet[1])/1000) + "km"
|
||||||
print(coordinates_string)
|
print(f"Le coordinate di questa condizione sono: {coordinates_string}")
|
||||||
for tweet in tw.Cursor(method=api.search, q="", geocode=coordinates_string).items(10):
|
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():
|
if not Tweet.query.filter_by(snowflake=str(tweet.id)).all():
|
||||||
image_url_list = ''
|
image_url_list = ''
|
||||||
if 'media' in tweet.entities.keys():
|
if 'media' in tweet.entities.keys():
|
||||||
|
@ -92,17 +98,17 @@ def search_repo_conditions(repository_id):
|
||||||
queryString += ("since:" + condition_content.content[2:] + " " + queryConjunction + " ")
|
queryString += ("since:" + condition_content.content[2:] + " " + queryConjunction + " ")
|
||||||
# End of query string
|
# End of query string
|
||||||
queryString = queryString[:-len(queryConjunction) - 1]
|
queryString = queryString[:-len(queryConjunction) - 1]
|
||||||
print(queryString)
|
print(f"La stringa di query finale e':{queryString}")
|
||||||
|
|
||||||
if evaluation_mode == ConditionMode.all_or:
|
if evaluation_mode == ConditionMode.all_or:
|
||||||
if queryString != "":
|
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)
|
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:
|
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)
|
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:
|
for tweet in tweetsFound:
|
||||||
if not Tweet.query.filter_by(snowflake=str(tweet.id)).all():
|
if not Tweet.query.filter_by(snowflake=str(tweet.id)).all():
|
||||||
image_url_list = ''
|
image_url_list = ''
|
||||||
|
|
Loading…
Reference in a new issue