From 647e5f722f3ed14b073bdb9272c433743607c965 Mon Sep 17 00:00:00 2001 From: "g.minoccari" Date: Fri, 7 May 2021 10:15:10 +0200 Subject: [PATCH] Rinominata cartella e aggiunti metodi per effettuare richieste all'api di twitter a partire dalle condizioni associate a una repository --- .../nest_backend/tweet-explorer/__main__.py | 14 --- .../nest_backend/tweet_explorer/__main__.py | 86 +++++++++++++++++++ 2 files changed, 86 insertions(+), 14 deletions(-) delete mode 100644 code/backend/nest_backend/tweet-explorer/__main__.py create mode 100644 code/backend/nest_backend/tweet_explorer/__main__.py diff --git a/code/backend/nest_backend/tweet-explorer/__main__.py b/code/backend/nest_backend/tweet-explorer/__main__.py deleted file mode 100644 index 5dd4e9d..0000000 --- a/code/backend/nest_backend/tweet-explorer/__main__.py +++ /dev/null @@ -1,14 +0,0 @@ -from nest_backend.app import app -from nest_backend.database import * - -Base.init_app(app=app) - - -def start_exploring(): - pass # Codice qui - - -if __name__ == "__main__": - with app.app_context(): - Base.create_all(app=app) - start_exploring() diff --git a/code/backend/nest_backend/tweet_explorer/__main__.py b/code/backend/nest_backend/tweet_explorer/__main__.py new file mode 100644 index 0000000..f9e426d --- /dev/null +++ b/code/backend/nest_backend/tweet_explorer/__main__.py @@ -0,0 +1,86 @@ +from nest_backend.app import app +from nest_backend.database import * +import tweepy as tw +import nltk +from nltk.corpus import stopwords + + + + +Base.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) + return api + +def start_exploring(): + api = authenticate() + most_popular_hashtags = dict() + most_popular_words = dict() + geocode="34.0999996,-118.333332,1000km" + for tweet in tw.Cursor(method = api.search, q="Draghi OR Oscar", lang="it").items(50): + print(tweet.user.name + ' : ' + tweet.text) + for hashtag in tweet.entities['hashtags']: + if hashtag['text'] in most_popular_hashtags.keys(): + most_popular_hashtags[hashtag['text']] += 1 + else: + most_popular_hashtags[hashtag['text']] = 1 + + stop_words = set(stopwords.words('italian')) + stop_words.add("RT") + + word_tokens = nltk.word_tokenize(tweet.text) + + filtered_sentence = [w for w in word_tokens if not w.lower() in stop_words and w.isalpha()] + for word in filtered_sentence: + if word in most_popular_words.keys(): + most_popular_words[word] += 1 + else: + most_popular_words[word] = 1 + + print(dict(sorted(most_popular_hashtags.items(), key=lambda item: item[1]))) + print(dict(sorted(most_popular_words.items(), key=lambda item: item[1]))) + + +def search_repo_conditions(repository_id): + repo = Repository.query.filter_by(id=repository_id).first() + conditions = [use.condition for use in repo.uses] + conditions_type = dict() + for condition in conditions: + # print(condition.id) + if condition.type not in conditions_type.keys(): + conditions_type[condition.type]=[condition.content] + else: + conditions_type[condition.type].append(condition.content) + + for types in conditions_type.keys(): + print(types, ":", conditions_type[types]) + +def search_condtions(conditions): + + + + + + + + + + + + + + +if __name__ == "__main__": + search_repo_conditions() + #print(stopwords.words('italian')) + with app.app_context(): + Base.create_all(app=app) + #start_exploring()