-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwisdomofreddit.py
217 lines (158 loc) · 6.45 KB
/
wisdomofreddit.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
"""
wisdomofreddit is a search app for high quality reddit comments
"""
from whoosh import index
from whoosh import scoring
from whoosh.qparser import QueryParser
from flask import Flask
from flask import render_template
from flask import request
from flask import redirect, url_for
from flask import _request_ctx_stack
import os,csv,re,random,sqlite3
from ast import literal_eval
app = Flask(__name__)
def open_index(index_dir,index_name):
"Open a given index"
ix = index.open_dir(index_dir,indexname=index_name)
return ix
def get_random_query():
"Return a random query"
query = "Kasparov"
random_query_file = os.path.join(os.path.dirname(__file__),'random_queries.txt')
with open(random_query_file,'r') as fp:
lines = fp.readlines()
query = random.choice(lines)
query = query.strip()
return query
def search_comments(query):
"Ask whoosh to return the top 20 matches"
all_results = []
try:
index_dir = os.path.join(os.path.dirname(__file__),'indexdir')
ix = open_index(index_dir,index_name='wor')
if os.path.exists(r'/tmp/'):
fp = open('/tmp/query.log','a')
fp.write(str(query)+"\n")
fp.close()
with ix.searcher() as searcher:
parser = QueryParser("comment",ix.schema)
results = searcher.search(parser.parse(query),limit=25)
for result in results:
all_results.append(result)
if os.path.exists(r'/tmp/'):
fp = open('/tmp/results.log','a')
fp.write(str(all_results)[:1]+"\n")
fp.close()
except Exception,e:
if os.path.exists(r'/tmp/'):
fp = open('/tmp/error.log','a')
fp.write(str(e))
fp.close()
return all_results
@app.route("/")
def index_page():
"The search page"
return render_template('index.html')
@app.route("/search")
def search():
"Return relevant comments"
query = request.args.get('query')
if query.strip() == "":
return return_random_prompt()
else:
results = search_comments(query)
return render_template('results.html', query=query, results=results)
@app.route("/random")
def return_random_prompt():
"Return a random comment"
query = get_random_query()
results = []
while results == []:
results = search_comments(query)
return render_template('random.html', results=results)
@app.route("/about")
def about():
"The about page"
return render_template('about.html')
@app.route("/why")
def why():
"The why page"
return render_template('why.html')
@app.route("/uses")
def use_cases():
"The uses page"
return render_template('uses.html')
@app.route("/pro-tips")
def pro_tips():
"The pro-tips page"
return render_template('pro-tips.html')
@app.route("/examples")
def examples():
"The examples page"
return render_template('examples.html')
@app.route("/blog")
def blog():
"The blog"
return render_template('blog.html')
@app.route("/blogposts/real-scary-creepy-paranormal-stories")
def paranormal():
"Paranormal stories"
return render_template('blogposts/real-scary-creepy-paranormal-stories.html')
@app.route("/blogposts/mundane-coincidence-stories")
def mundane_coincidences():
"Mundane coincidences"
return render_template('blogposts/mundane-coincidence-stories.html')
@app.route('/api-tutorial-main')
def api_tutorial_main():
"Api Tutorial main page- If method is post redirect to api tutorial redirect with name and comments"
return render_template('api-tutorial-main.html')
@app.route('/api-tutorial-redirect',methods=['GET','POST'])
def api_tutorial_redirect():
"Api Tutorial Redirect page- Saves the name and comments and displays all the name and comments"
db_file = os.path.join(os.path.dirname(__file__),'tmp','wisdomofreddit.db') #Create a variabe as db_file to create the DB file in the temp directory
connection_obj = sqlite3.connect(db_file) #Connect to the db
cursor_obj = connection_obj.cursor()
if request.method == 'POST':
user_name = request.form['submitname']
user_comments = request.form['submitcomments']
value = [user_name,user_comments]
cursor_obj.execute("INSERT INTO comments VALUES (?,?)",value) #Insert values into the table. FYI comments table has already been created in the DB
connection_obj.commit() #Save the changes
results = cursor_obj.execute("SELECT * FROM comments") #Hold all the name and comments in a variable
return render_template('api-tutorial-redirect.html', results=results.fetchall())
@app.route('/trial-page-bug')
def trial_page_bug():
"Trial page for practicing bug reports"
return render_template('trial_page_bug.html')
@app.route('/trial-redirect-page-bug',methods=['GET','POST'])
def trial_redirect_page_bug():
"Trial redirects page specifies there is no validation for login"
return render_template('trial_redirect_page_bug.html')
@app.route('/retrieve-wrong-data')
def retrieve_wrong_data():
"Trial page for practicing bug reports redirect with name and comments"
return render_template('retrieve_wrong_data.html')
@app.route('/retrieve-wrong-data-redirect-page',methods=['GET','POST'])
def retrieve_wrong_data_redirect_page():
"Trial redirects page specifies that retieved username and comments are not correct"
db_file = os.path.join(os.path.dirname(__file__),'tmp','wisdomofreddit.db') #Create a variabe as db_file to create the DB file in the temp directory
connection_obj = sqlite3.connect(db_file) #Connect to the db
cursor_obj = connection_obj.cursor()
results = cursor_obj.execute("SELECT * FROM comments") #Hold all the name and comments in a variable
return render_template('retrieve_wrong_data_redirect_page.html', results=results.fetchall())
@app.route('/no-response')
def no_response():
"Trial page for practicing bug reports of no response"
return render_template('no_response.html')
@app.route('/no-response-page-bug',methods=['GET','POST'])
def no_response_page_bug():
"Trial page with no response after clicking the button"
return render_template('no_response_page_bug.html')
@app.route('/click-button-bug')
def click_button_bug():
"Trial page specifies no response after clicking the login button"
return render_template('click_button_bug.html')
#---START
if __name__=='__main__':
app.run(host='0.0.0.0',port=6464)