2021-05-14 15:49:04 +00:00
|
|
|
from nest_backend.app import app
|
|
|
|
from nest_backend.database import *
|
|
|
|
import tweepy as tw
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
ext.init_app(app=app)
|
|
|
|
|
|
|
|
|
|
|
|
def authenticate():
|
|
|
|
c_k = "GEhtSyP9e98mzFeiOCSW0lvQX"
|
|
|
|
c_s = "438cmYrl5xqaX2W7I2Bf5A9nF1pN5VtM9f77WYQnAXg1BwKJ27"
|
|
|
|
a_t = "1380217745732689921-8gCfr8Zx9YHKvo4OVP3HAr3kfMRkgz"
|
|
|
|
a_t_s = "jGOlgTs1i1itGMxDxAqFEDnv7QAui772n9hGxeSIKcwzS"
|
|
|
|
|
|
|
|
auth = tw.OAuthHandler(c_k, c_s)
|
|
|
|
auth.set_access_token(a_t, a_t_s)
|
|
|
|
api = tw.API(auth, wait_on_rate_limit=True)
|
2021-05-17 08:17:00 +00:00
|
|
|
# client = tw.Client(b_t, c_k, c_s, a_t, a_t_s, wait_on_rate_limit=True);
|
2021-05-14 15:49:04 +00:00
|
|
|
return api
|
|
|
|
|
|
|
|
|
|
|
|
def search_repo_conditions(repository_id):
|
|
|
|
api = authenticate()
|
2021-05-17 08:17:00 +00:00
|
|
|
geocode = "44.3591600,11.7132000,20km"
|
2021-05-14 15:49:04 +00:00
|
|
|
|
|
|
|
repo = Repository.query.filter_by(id=repository_id).first()
|
2021-05-17 08:17:00 +00:00
|
|
|
if repo is None:
|
2021-05-14 15:49:04 +00:00
|
|
|
print("Non esiste una repository con questo id")
|
|
|
|
return False
|
|
|
|
conditions = [use for use in repo.conditions]
|
|
|
|
evaluation_mode = repo.evaluation_mode
|
|
|
|
conditions_type = dict()
|
2021-05-17 08:17:00 +00:00
|
|
|
# Dividing condition into condition types
|
2021-05-14 15:49:04 +00:00
|
|
|
for condition in conditions:
|
|
|
|
# print(condition.id)
|
|
|
|
if condition.type not in conditions_type.keys():
|
2021-05-18 13:16:47 +00:00
|
|
|
conditions_type[condition.type] = [condition]
|
2021-05-14 15:49:04 +00:00
|
|
|
else:
|
2021-05-18 13:16:47 +00:00
|
|
|
conditions_type[condition.type].append(condition)
|
2021-05-14 15:49:04 +00:00
|
|
|
|
|
|
|
queryString = ""
|
|
|
|
|
|
|
|
queryConjunction = "OR" if (evaluation_mode == ConditionMode.all_or) else ""
|
|
|
|
|
|
|
|
tweetsFound = []
|
|
|
|
|
|
|
|
for types in conditions_type.keys():
|
|
|
|
print(types, ":", conditions_type[types])
|
|
|
|
coordinates_string = ""
|
|
|
|
if ConditionType.hashtag in conditions_type.keys():
|
|
|
|
for condition_content in conditions_type[ConditionType.hashtag]:
|
2021-05-18 13:16:47 +00:00
|
|
|
queryString += ("#" + condition_content.content + " " + queryConjunction + " ")
|
2021-05-14 15:49:04 +00:00
|
|
|
if ConditionType.coordinates in conditions_type.keys():
|
|
|
|
if evaluation_mode == ConditionMode.all_and:
|
2021-05-17 08:17:00 +00:00
|
|
|
if len(conditions_type[ConditionType.coordinates]) == 1:
|
2021-05-14 15:49:04 +00:00
|
|
|
coordinates_tweet = conditions_type[ConditionType.coordinates][0].split()
|
2021-05-17 08:17:00 +00:00
|
|
|
coordinates_string = coordinates_tweet[2] + "," + coordinates_tweet[3] + "," + str(float(coordinates_tweet[1])/1000) + "km"
|
2021-05-14 15:49:04 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
elif evaluation_mode == ConditionMode.all_or:
|
|
|
|
for condition_content in conditions_type[ConditionType.coordinates]:
|
2021-05-18 13:16:47 +00:00
|
|
|
coordinates_tweet = condition_content.content.split()
|
2021-05-17 08:17:00 +00:00
|
|
|
coordinates_string = coordinates_tweet[2] + "," + coordinates_tweet[3] + "," + str(float(coordinates_tweet[1])/1000) + "km"
|
2021-05-14 15:49:04 +00:00
|
|
|
print(coordinates_string)
|
2021-05-18 13:16:47 +00:00
|
|
|
for tweet in tw.Cursor(method=api.search, q="", geocode=coordinates_string).items(10):
|
|
|
|
if not Tweet.query.filter_by(snowflake=str(tweet.id)).all():
|
|
|
|
tweetDB = Tweet(snowflake=tweet.id, content=tweet.text,
|
|
|
|
location=tweet.geo['coordinates'] if tweet.geo is not None else None,
|
|
|
|
place=tweet.place.full_name if tweet.place is not None else None,
|
|
|
|
insert_time=str(datetime.now()),
|
|
|
|
poster=tweet.author.screen_name)
|
|
|
|
ext.session.add(tweetDB)
|
|
|
|
ext.session.commit()
|
|
|
|
if not Contains.query.filter_by(snowflake=str(tweet.id), cid=condition_content.id).all():
|
|
|
|
condition_associated = Contains(cid=condition_content.id, snowflake=tweet.id)
|
|
|
|
ext.session.add(condition_associated)
|
|
|
|
ext.session.commit()
|
|
|
|
if not Composed.query.filter_by(snowflake=str(tweet.id), rid=repository_id).all():
|
|
|
|
composed = Composed(rid=repository_id, snowflake=tweet.id)
|
|
|
|
ext.session.add(composed)
|
|
|
|
ext.session.commit()
|
2021-05-14 15:49:04 +00:00
|
|
|
if ConditionType.user in conditions_type.keys():
|
|
|
|
for condition_content in conditions_type[ConditionType.user]:
|
2021-05-18 13:16:47 +00:00
|
|
|
queryString += ("from:" + condition_content.content + " " + queryConjunction + " ")
|
2021-05-14 15:49:04 +00:00
|
|
|
if ConditionType.time in conditions_type.keys():
|
|
|
|
for condition_content in conditions_type[ConditionType.time]:
|
2021-05-18 13:16:47 +00:00
|
|
|
if condition_content.content[0] == '<':
|
|
|
|
queryString += ("until:" + condition_content.content[2:] + " " + queryConjunction + " ")
|
|
|
|
elif condition_content.content[0] == '>':
|
|
|
|
queryString += ("since:" + condition_content.content[2:] + " " + queryConjunction + " ")
|
2021-05-17 08:17:00 +00:00
|
|
|
queryString = queryString[:-len(queryConjunction) - 1]
|
2021-05-14 15:49:04 +00:00
|
|
|
print(queryString)
|
2021-05-17 08:17:00 +00:00
|
|
|
if evaluation_mode == ConditionMode.all_or:
|
2021-05-18 13:16:47 +00:00
|
|
|
if queryString != "":
|
|
|
|
for tweet in tw.Cursor(method=api.search, q=queryString).items(10):
|
|
|
|
tweetsFound.append(tweet)
|
|
|
|
print(tweet.user.name + ' : ' + tweet.text + ' : ' + tweet.geo)
|
2021-05-17 08:17:00 +00:00
|
|
|
elif evaluation_mode == ConditionMode.all_and:
|
2021-05-14 15:49:04 +00:00
|
|
|
for tweet in tw.Cursor(method=api.search, q=queryString, geocode=coordinates_string).items(10):
|
|
|
|
tweetsFound.append(tweet)
|
2021-05-18 13:16:47 +00:00
|
|
|
print(tweet.user.name + ' : ' + tweet.text + ' : ' + str(tweet.geo))
|
2021-05-14 15:49:04 +00:00
|
|
|
for tweet in tweetsFound:
|
|
|
|
if not Tweet.query.filter_by(snowflake=str(tweet.id)).all():
|
2021-05-17 08:17:00 +00:00
|
|
|
tweetDB = Tweet(snowflake=tweet.id, content=tweet.text,
|
2021-05-18 13:16:47 +00:00
|
|
|
location=tweet.geo['coordinates'] if tweet.geo is not None else None,
|
|
|
|
place=tweet.place.full_name if tweet.place is not None else None,
|
|
|
|
insert_time=str(datetime.now()),
|
|
|
|
poster=tweet.author.screen_name)
|
2021-05-14 15:49:04 +00:00
|
|
|
ext.session.add(tweetDB)
|
2021-05-18 13:16:47 +00:00
|
|
|
ext.session.commit()
|
|
|
|
if evaluation_mode == ConditionMode.all_or:
|
|
|
|
if ConditionType.hashtag in conditions_type.keys():
|
|
|
|
for condition_content in conditions_type[ConditionType.hashtag]:
|
|
|
|
if condition_content.content in [hashtag['text'] for hashtag in tweet.entities['hashtags']]:
|
|
|
|
condition_associated = Contains(cid=condition_content.id, snowflake=tweet.id)
|
|
|
|
ext.session.add(condition_associated)
|
|
|
|
ext.session.commit()
|
|
|
|
if ConditionType.user in conditions_type.keys():
|
|
|
|
for condition_content in conditions_type[ConditionType.user]:
|
|
|
|
if condition_content.content == tweet.author.screen_name:
|
|
|
|
condition_associated = Contains(cid=condition_content.id, snowflake=tweet.id)
|
|
|
|
ext.session.add(condition_associated)
|
|
|
|
ext.session.commit()
|
|
|
|
|
|
|
|
if ConditionType.time in conditions_type.keys():
|
|
|
|
for condition_content in conditions_type[ConditionType.time]:
|
|
|
|
condition_date_time = datetime.fromisoformat(condition_content.content[2:])
|
|
|
|
if condition_content.content[0] == '<':
|
|
|
|
if tweet.created_at < condition_date_time:
|
|
|
|
condition_associated = Contains(cid=condition_content.id, snowflake=tweet.id)
|
|
|
|
ext.session.add(condition_associated)
|
|
|
|
ext.session.commit()
|
|
|
|
elif condition_content.content[0] == '>':
|
|
|
|
if tweet.created_at > condition_date_time:
|
|
|
|
condition_associated = Contains(cid=condition_content.id, snowflake=tweet.id)
|
|
|
|
ext.session.add(condition_associated)
|
|
|
|
ext.session.commit()
|
|
|
|
elif evaluation_mode == ConditionMode.all_and:
|
|
|
|
for condition in conditions:
|
|
|
|
if Contains.query.filter_by(snowflake=str(tweet.id), cid=condition.id).all():
|
|
|
|
condition_associated = Contains(cid=condition.id, snowflake=tweet.id)
|
|
|
|
ext.session.add(condition_associated)
|
|
|
|
ext.session.commit()
|
2021-05-14 15:49:04 +00:00
|
|
|
if not Composed.query.filter_by(snowflake=str(tweet.id), rid=repository_id).all():
|
|
|
|
composed = Composed(rid=repository_id, snowflake=tweet.id)
|
|
|
|
ext.session.add(composed)
|
|
|
|
ext.session.commit()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
search_repo_conditions(16)
|
|
|
|
with app.app_context():
|
|
|
|
ext.create_all(app=app)
|