-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
106 lines (81 loc) · 2.85 KB
/
server.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
import json
from flask import Flask, render_template, request
from preprocess import load_data
from fetch import fetch_tweet_and_replies
from search import search
from utils import read
app = Flask(__name__)
data = read('data/data.json')
rumours = list(data.keys())
threads = {k: v for rumour in data.values() for k, v in rumour.items()}
groups = read('data/groups.json')
def update_data():
global data, rumours, threads, groups
data = read('data/data.json')
rumours = list(data.keys())
threads = {k: v for rumour in data.values() for k, v in rumour.items()}
groups = read('data/groups.json')
def replies(thread):
# log(thread_id)
# log(len(threads[thread_id].get('replies', dict()).values()))
# return [log(tweet['text']) for tweet in threads[thread_id].get('replies', dict()).values()]
# print('==')
# print(len(thread.get('replies', dict()).values()))
return [{
'text': tweet['text'],
'group': groups.get(tweet.get('id_str', 0), 'unknown')
} for tweet in thread.get('replies', dict()).values()]
@app.route("/")
def index():
return render_template('index.html', rumours=rumours)
@app.route("/fetch")
def fetch_tweet_page():
return render_template('fetch.html')
@app.route("/fetch-tweet", methods=['POST'])
def fetch_tweet():
tweet_id = request.form['tweet_id']
reply_ids = request.form['replies_list']
# print(tweet_id, reply_ids)
fetch_tweet_and_replies(tweet_id, reply_ids)
# print('loading')
load_data()
# print('updating')
update_data()
# print('rendering')
return render_template('success.html')
@app.route("/style.css")
def style():
return render_template('style.css')
@app.route('/text/<text>')
def search_text(text):
print('searching for ', text.replace('%23', '#'))
results = sorted(search(text).items(), key=lambda x: x[1], reverse=True)
results = [{
"rating": rating,
"text": threads[id]['source']['text'],
"replies": replies(threads[id])
# [tweet['text'] for tweet in threads[id].get('replies', dict()).values()]
} for id, rating in results]
return json.dumps(results)
@app.route('/rumour/<rumour>')
def search_rumour(rumour):
results = data[rumour].values()
# print('-----')
# print(len(results))
# print(len(list(results)))
# print(len(list(results)[0]))
# print(list(results)[0]['source']['text'])
# # print(json.dumps(list(results)[0]))
# print('-----')
results = [{
"text": thread['source']['text'],
"replies": replies(thread)
} for thread in results]
# print('dasdansuidnaskd')
return json.dumps(results)
def main():
global app, rumours
print('Active rumours', rumours)
app.run()
if __name__ == "__main__":
main()