-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
133 lines (116 loc) · 4.79 KB
/
app.py
File metadata and controls
133 lines (116 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import json
import os
import requests
import tweepy
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from datetime import date, timedelta
TWEETS_TO_SEARCH = 100
PREVIOUS_RESULT_NUM_TO_DISPLAY = 15
PREVIOUS_RESULT_TO_TEMP_STORE = 50
# Returns tweets that are BELOW the threshold
SENTIMENT_THRESHOLD = -0.8
DURATION = 0
app = Flask(__name__) #, static_url_path='/templates/build')
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
analyzer = SentimentIntensityAnalyzer()
auth = tweepy.AppAuthHandler(os.environ['CONSUMER_KEY'], os.environ['CONSUMER_SECRET'])
api = tweepy.API(auth, wait_on_rate_limit=True)
from models import TweetO
def get_negative_tweets(search, threshold=SENTIMENT_THRESHOLD, duration=DURATION):
results = []
tweets = []
# Search tweets based on given parameter for strongly opinionated ones
for tweet in tweepy.Cursor(api.search,
lang="en",
result_type="recent",
q=search,
tweet_mode='extended',
until=(date.today() - timedelta(days=duration-1)).isoformat()
).items(TWEETS_TO_SEARCH):
full_text = tweet.full_text[:tweet.full_text.find(':')+2] + tweet.retweeted_status.full_text if hasattr(tweet, 'retweeted_status') else tweet.full_text
print(tweet.retweeted)
print(tweet.full_text)
# print(tweet.retweeted_status)
# print(tweet.retweeted_status.extended_tweet)
score = analyzer.polarity_scores(full_text)
if score["compound"] < threshold:
results.append((tweet.id_str, full_text, score, tweet.retweet_count, tweet.retweeted))
# Transform results into Tweet objects
for tweet in results:
try:
result = TweetO(
url = "https://twitter.com/twitter/statuses/" + tweet[0],
text = tweet[1],
sentiment = tweet[2],
retweets = tweet[3]
)
print(json.dumps(tweet[2]), type(json.dumps(tweet[2])))
print('hi')
tweets.append((result, tweet[4]))
except Exception as e:
print(e)
print('well hello')
print('help me')
return tweets
def get_previous_results():
previous_results = []
tweets = TweetO.query.order_by(TweetO.id.desc()).limit(PREVIOUS_RESULT_NUM_TO_DISPLAY).all()
for tweet in tweets:
previous_results.append((tweet.url, tweet.text, tweet.sentiment, tweet.compound))
return previous_results
show_previous_tweets = False
previous_results = []
prev_threshold = -0.8
prev_duration = 0
@app.route('/', methods=['GET', 'POST'])
def index():
global show_previous_tweets
global previous_results
global prev_threshold
global prev_duration
tweets = []
errors = []
results = []
search = ""
if request.method == "POST":
show_previous_tweets = False
try:
search = request.form['search']
threshold = float(request.form["threshold"])
duration = int(request.form["duration"])
tweets = get_negative_tweets(search, threshold, duration)
prev_threshold = threshold
prev_duration = duration
except Exception as e:
print(e)
errors.append(
"Unable to get search. Please make sure it's valid and try again."
)
if tweets:
try:
for tweet, retweeted in tweets:
db.session.add(tweet)
results.append((tweet.url, tweet.text, tweet.compound, tweet.retweets))
print(tweet.sentiment, type(tweet.sentiment))
db.session.commit()
except Exception as e:
print(e)
errors.append("Unable to add item to database.")
previous_results.extend(results)
# if len(previous_results) > PREVIOUS_RESULT_TO_TEMP_STORE:
# previous_results = previous_results[:PREVIOUS_RESULT_TO_TEMP_STORE]
prev_to_show = get_previous_results() if not previous_results else previous_results[-PREVIOUS_RESULT_NUM_TO_DISPLAY:]
# print(previous_results)
return render_template('index.html', errors=errors, results=results, previous_results=prev_to_show,
keyword=search, show_prev=show_previous_tweets, thresh=prev_threshold, dur=prev_duration)
@app.route('/button', methods=['GET'])
def button():
global show_previous_tweets
show_previous_tweets = not show_previous_tweets
return redirect("/", code=302)
if __name__ == '__main__':
app.run()