-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
75 lines (58 loc) · 1.61 KB
/
Copy pathmain.py
File metadata and controls
75 lines (58 loc) · 1.61 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
from flask.cli import prepare_import
from flask.helpers import url_for
import spacy
# from spacy.lang.en import English, tokenizer_exceptions
# from spacy.lang.ar.examples import sentences
from data.TwitterData import Listener, api
from data.DBHelper import DBHelper
from tweepy.streaming import Stream
import tweepy
from flask import Flask, render_template, stream_with_context, Response, request, redirect
from flask_cors import CORS
import json
app = Flask(__name__)
CORS(app)
listener = Listener()
stream = Stream(auth=api.auth, listener=listener)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/', methods=['POST'])
def form_post():
text = request.form['keyword']
print(text)
processed_text = text.upper()
streamData(processed_text)
return redirect(url_for('/',))
@app.route('/dataStream')
def streamData():
print('starting...')
try:
# stream.filter(track=[keyword])
stream.sample()
return '200'
except tweepy.TweepError as e:
print(e)
print(e.reason)
return '500'
@app.route('/stopStream')
def stop():
# def gen():
print('stopping...')
try:
stream.disconnect()
return '200'
except tweepy.TweepError as e:
print(e)
print(e.reason)
return '500'
@app.route('/data')
def fetchLatest():
print('fetching latest tweets...')
db = DBHelper()
db.__connect__()
result = db.fetch('SELECT * FROM Tweets order by id desc LIMIT 10;')
print('Fetched 👍')
return json.dumps(result, default=str)
if __name__ == "__main__":
app.run(debug=True)