1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-21 20:44:18 +00:00

🐛 Fix various bugs

This commit is contained in:
Steffo 2021-05-30 17:35:49 +02:00
parent d32537ba9b
commit 8d591befb9
Signed by: steffo
GPG key ID: 6965406171929D01
4 changed files with 14 additions and 24 deletions

View file

@ -30,16 +30,16 @@ def is_repo_alert_triggered(repository_id):
conditions_tweet.update([tweet.tweet for tweet in condition.tweets]) conditions_tweet.update([tweet.tweet for tweet in condition.tweets])
alert_tweets = alert_tweets.intersection(conditions_tweet) alert_tweets = alert_tweets.intersection(conditions_tweet)
end_time = datetime.now() end_time = datetime.now()
window_size_hours = timedelta(hours = alert.window_size) window_size_hours = timedelta(hours=alert.window_size)
last_notification_time = min([notification.ora for notification in alert.notifications] if len(alert.notifications)>0 else [end_time - window_size_hours]) 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) 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)] alert_tweets = [tweet for tweet in alert_tweets if (end_time > tweet.insert_time > start_time)]
print(f"I tweet corrispondenti sono:{len(alert_tweets)}") print(f"I tweet corrispondenti sono: {len(alert_tweets)}")
if len(alert_tweets) >= alert.limit: if len(alert_tweets) >= alert.limit:
alert_notification = Notification(ora=str(datetime.now()), alert_id=alert.id) alert_notification = Notification(ora=str(datetime.now()), alert_id=alert.id)
ext.session.add(alert_notification) ext.session.add(alert_notification)
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)
@ -62,11 +62,6 @@ def send_notification_email(alert):
def send_notification_tweet(alert): def send_notification_tweet(alert):
api = authenticate() api = authenticate()
conditions_string = ''
for condition in alert.conditions:
conditions_string += condition.condition.content + ','
conditions_string = conditions_string[:-1]
print(conditions_string)
try: try:
api.update_status(MESSAGE.format(alert_name=alert.name, now=datetime.now().isoformat())) api.update_status(MESSAGE.format(alert_name=alert.name, now=datetime.now().isoformat()))
except tw.errors.Forbidden: except tw.errors.Forbidden:
@ -74,6 +69,7 @@ def send_notification_tweet(alert):
__all__ = ( __all__ = (
"MESSAGE",
"is_repo_alert_triggered", "is_repo_alert_triggered",
"send_notification_email", "send_notification_email",
"send_notification_tweet", "send_notification_tweet",

View file

@ -3,10 +3,10 @@ import os
def authenticate(): def authenticate():
c_k = os.getenv('C_K') c_k = os.environ['C_K']
c_s = os.getenv('C_S') c_s = os.environ['C_S']
a_t = os.getenv('A_T') a_t = os.environ['A_T']
a_t_s = os.getenv('A_T_S') a_t_s = os.environ['A_T_S']
auth = tw.OAuthHandler(c_k, c_s) auth = tw.OAuthHandler(c_k, c_s)
auth.set_access_token(a_t, a_t_s) auth.set_access_token(a_t, a_t_s)

View file

@ -11,13 +11,11 @@ def search_repo_conditions(repository_id):
repo = Repository.query.filter_by(id=repository_id).first() repo = Repository.query.filter_by(id=repository_id).first()
if repo is None: if repo is None:
print("Non esiste una repository con questo id")
return False return False
conditions = [use for use in repo.conditions] conditions = [use for use in repo.conditions]
if len(conditions) == 0: if len(conditions) == 0:
return False return False
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 = [tweet.tweet for tweet in repo.tweets]
@ -40,8 +38,6 @@ def search_repo_conditions(repository_id):
tweetsFound = [] tweetsFound = []
for types in conditions_type.keys():
print(types, ":", conditions_type[types])
coordinates_string = "" coordinates_string = ""
# Adding to the query string the hashtag conditions # Adding to the query string the hashtag conditions
if ConditionType.hashtag in conditions_type.keys(): if ConditionType.hashtag in conditions_type.keys():
@ -60,7 +56,6 @@ 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(f"Le coordinate di questa condizione sono: {coordinates_string}")
for tweet in tw.Cursor(method=api.search, q="", geocode=coordinates_string).items(100): 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 = ''
@ -88,27 +83,26 @@ def search_repo_conditions(repository_id):
# Adding to the query string the user condition # Adding to the query string the user condition
if ConditionType.user in conditions_type.keys(): if ConditionType.user in conditions_type.keys():
for condition_content in conditions_type[ConditionType.user]: for condition_content in conditions_type[ConditionType.user]:
queryString += ("from:" + condition_content.content + " " + queryConjunction + " ") queryString += f"from:{condition_content.content} {queryConjunction} "
# Adding to the query string the time condition # Adding to the query string the time condition
if ConditionType.time in conditions_type.keys(): if ConditionType.time in conditions_type.keys():
for condition_content in conditions_type[ConditionType.time]: for condition_content in conditions_type[ConditionType.time]:
if condition_content.content[0] == '<': if condition_content.content[0] == '<':
queryString += ("until:" + condition_content.content[2:] + " " + queryConjunction + " ") queryString += f"until:{condition_content.content[2:]} {queryConjunction} "
elif condition_content.content[0] == '>': elif condition_content.content[0] == '>':
queryString += ("since:" + condition_content.content[2:] + " " + queryConjunction + " ") queryString += f"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(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(100): 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(f"{tweet.user.name}: {tweet.text} @ {tweet.geo or '<nowhere>'}")
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(100): 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 + ' : ' + (tweet.geo if tweet.geo is not None else '')) print(f"{tweet.user.name}: {tweet.text} @ {tweet.geo or '<nowhere>'}")
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 = ''