-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
291 lines (247 loc) · 10.1 KB
/
app.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import numpy as np
import pickle
import hashlib
import json
import re
import tweepy
import requests
import traceback
import GetOldTweets3 as got
from datetime import datetime, timedelta
import pandas as pd
import nltk
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')
import Methods as m
import Stanford
from nltk.tokenize import word_tokenize
# import get_Old_Tweets_re as got
stop_words = m.read_stopwords()
from werkzeug.wsgi import ClosingIterator
from flask_sqlalchemy import SQLAlchemy
from bson import json_util
from flask import Flask, request, jsonify, render_template, abort, Response
from flask_pymongo import PyMongo
from flask_cors import CORS, cross_origin
from datetime import datetime
from sqlalchemy_serializer import SerializerMixin
mongo = PyMongo()
app = Flask(__name__)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///db.sqlite3'
app.config['CORS_HEADERS'] = 'Content-Type'
db = SQLAlchemy(app)
CORS(app)
consumer_key = ''
consumer_secret_key = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
max_tweets = 1000
query_Id = 0
class AfterThisResponse:
def __init__(self, app=None):
self.callbacks = []
if app:
self.init_app(app)
def __call__(self, callback):
self.callbacks.append(callback)
return callback
def init_app(self, app):
# install extensioe
app.after_this_response = self
# install middleware
app.wsgi_app = AfterThisResponseMiddleware(app.wsgi_app, self)
def flush(self):
try:
for fn in self.callbacks:
try:
fn()
except Exception:
traceback.print_exc()
finally:
self.callbacks = []
class AfterThisResponseMiddleware:
def __init__(self, application, after_this_response_ext):
self.application = application
self.after_this_response_ext = after_this_response_ext
def __call__(self, environ, start_response):
iterator = self.application(environ, start_response)
try:
return ClosingIterator(iterator, [self.after_this_response_ext.flush])
except Exception:
traceback.print_exc()
return iterator
class Tweet(db.Model, SerializerMixin):
id = db.Column(db.Integer, primary_key=True)
queryId = db.Column(db.Integer)
tweet = db.Column(db.Text)
in_reply_to_status_id = db.Column(db.Text)
screen_name = db.Column(db.Text)
location = db.Column(db.Text)
description = db.Column(db.Text)
url = db.Column(db.Text)
lang = db.Column(db.Text)
status = db.Column(db.Text)
has_extended_profile = db.Column(db.Boolean)
name = db.Column(db.Text)
created_at = db.Column(db.Text)
verified = db.Column(db.Boolean)
followers_count = db.Column(db.Integer)
friends_count = db.Column(db.Integer)
statuses_count = db.Column(db.Integer)
listed_count = db.Column(db.Integer)
favourites_count = db.Column(db.Integer)
favorites = db.Column(db.Integer)
geo = db.Column(db.Text)
tweetid = db.Column(db.Text)
retweets = db.Column(db.Integer)
record_created_at = db.Column(db.DateTime, default = datetime.now)
AfterThisResponse(app)
@app.route('/')
@cross_origin()
def home():
return render_template('index.html')
@app.route('/query', methods=['POST'])
@cross_origin()
def query():
global query_Id
global news_text
data = request.get_json(force=True)
if not data or not 'queryId' in data:
abort(400)
query_Id = data['queryId']
news_text = data['query']
# keyword_list = data['keyword_list']
# print('keyword_list <<<<', keyword_list)
print('data <<<<', data)
# background process
@app.after_this_response
def post_process():
# Extract Keywoords here using 'tweet'
# ----------------------------------
global news_text
ordering = []
nonordering = []
keystring = []
s = ''
data_set = []
tokenized_text = word_tokenize(news_text)
# tokenized_text = word_tokenize(user_text)
news_text = str(m.text_cleaning(news_text, stop_words))
#----getting keywords ----
candidate_keywords = Stanford.extract_candidate_keywords(news_text)
candidate_keywords = [m.lower() for m in candidate_keywords]
keywords = list(dict.fromkeys(candidate_keywords))#remove duplicates
print("keywords", keywords)
#----getting keywords---
#---Ordering the keywords---
for word in tokenized_text:
for kword in keywords:
if word.lower() == kword:
ordering.append(kword)
keywords.remove(kword)
ordering = ordering + keywords
for x in range(len(ordering)):
s = s + ordering[x] + " "
if len(s.split()) == 3:
keystring.append(s)
s = ''
elif x == len(ordering)-1:
keystring.append(s)
print("keystring", keystring)
# keystring = keystring + keywords
keyword_list = keystring
# news_text = tweet_text #'Regime special guard forces attacked citizens who were protesting peacefully. The crowd is calling the regime agents "dishonorable" Sunday January 12 #abc https://docs.google.com/document/d/1o_fvrKrCr61eDqXN6ak4UEqpBTpK0FPransm7FgwTOg/edit '
print("news >>>", news_text)
print("keyword_list >>>", keyword_list)
# tokenized_text = word_tokenize(news_text)
querySearchTerm = ''
endDate = datetime.today().strftime('%Y-%m-%d')
startDate = (datetime.today() - timedelta(days = 2000)).strftime('%Y-%m-%d')
global counter
tweetLimit = 5
count = 1
try:
for elem in keyword_list:
querySearchTerm = elem
print("querySearchTerm", querySearchTerm)
tweetCriteria = got.manager.TweetCriteria().setQuerySearch(querySearchTerm) \
.setSince(startDate) \
.setUntil(endDate) \
.setMaxTweets(tweetLimit)
for index, x in enumerate(got.manager.TweetManager.getTweets(tweetCriteria)):
datestring = str(x.date)
# user_data_tweepy = get_data_tweepy(x.username, x.id)
consumer_key = ''
consumer_secret_key = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
user_data_tweepy = api.get_user(x.username)
status = api.get_status(x.id)
# user.in_reply_to_status_id_str = status.in_reply_to_status_id_str
# tweet = api.get_status(1232781683885314056)
# print(tweet.in_reply_to_status_id_str)
tweet = Tweet()
tweet.queryId = query_Id
tweet.tweetid = str(x.id)
tweet.retweets = str(x.retweets)
tweet.tweet = x.text
tweet.in_reply_to_status_id_str = status.in_reply_to_status_id_str
tweet.screen_name = str(user_data_tweepy.screen_name)
tweet.location = str(user_data_tweepy.location)
tweet.description = str(user_data_tweepy.description)
tweet.url = str(user_data_tweepy.url)
tweet.lang = str(user_data_tweepy.lang)
tweet.status = x.text
tweet.has_extended_profile = user_data_tweepy.default_profile_image
tweet.name = str(user_data_tweepy.name)
tweet.created_at = str(user_data_tweepy.created_at)
tweet.verified = user_data_tweepy.verified
tweet.followers_count = user_data_tweepy.followers_count
tweet.friends_count = user_data_tweepy.friends_count
tweet.statuses_count = user_data_tweepy.statuses_count
tweet.listed_count = user_data_tweepy.listed_count
tweet.favourites_count = user_data_tweepy.favourites_count
tweet.favorites = str(x.favorites)
tweet.geo = str(x.geo)
db.session.add(tweet)
db.session.commit()
print("Tweet " + str(count) + " collected")
count += 1
# user_data_tweepy = get_data_tweepy(x.username)
# type = datestring + ' | ' + str(x.id) + ' | ' + str(x.username) + ' | ' + x.text + ' | ' + str(x.retweets) + \
# ' | ' + str(x.favorites) + ' | '+ str(x.geo) + user_data_tweepy
# print(type)
# data_set.append(type)
print("Done for ", elem)
except Exception:
traceback.print_exc()
print("Something went wrong")
finally:
r = requests.post('https://strainer-rest-api.herokuapp.com/setdata/'+str(query_Id))
print(r)
# r = requests.post('https://strainer-forecast.herokuapp.com/setdata/'+str(query_Id))
# print(r)
# write_file(data_set)
return jsonify({'queryId': data['queryId'],'message': 'Data collection initiated'}), 201
@app.route('/get/<id>', methods=['GET'])
@cross_origin()
def get(id):
try:
queryId = int(id)
except:
return jsonify({'message': 'Not a valid Id'}), 400
tweets = Tweet.query.filter_by(queryId=id).all()
if tweets == []:
return jsonify({'queryId': queryId, 'message': 'Nothing is available for Id ' + id + ', rechek and try again!'}), 400
json_results = [t.to_dict() for t in tweets]
return jsonify(json_results), 200
if __name__ == "__main__":
app.run(debug=True)