/
script.py
executable file
·82 lines (69 loc) · 1.76 KB
/
script.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
#!/usr/bin/python
print('Content-type: text/html\r\n\r')
import cgi, json
import cgitb; cgitb.enable()
import datetime
from searcher import Searcher
cgitb.enable() # for troubleshooting
#the cgi library gets vars from html
data = cgi.FieldStorage()
mySearcher = Searcher()
html = ""
query = data.getvalue('query')
a = datetime.datetime.now()
results = mySearcher.getSearchResults(query)
b = datetime.datetime.now()
c = b - a
n = 0
# r[1][0] = rank
# r[1][1] = url
# r[1][2] = modified
# r[1][3] = size
# r[1][4] = dict{keyword: freq}
# r[1][5] = array{parents}
# r[1][6] = array{children}
# set up data structure to hold json skeleton
data = {}
# set of result objects
res = []
# handle no results found case
if len(results) <1 :
data['error'] = "No results found for: " + query;
json_data = json.dumps(data)
res.append(json_data);
print json.dumps([dict(result=r) for r in res])
else:
# stats json
data['time_taken'] = str(c.total_seconds()) + " seconds.";
data['total_results'] = str(len(results)) + " (50 shown)" if len(results) > 50 else str(len(results));
json_data = json.dumps(data)
res.append(json_data);
# variable to limit output
i = 0;
for r in results:
# limit output 50 results
if (i > 49):
break;
i+=1;
# unpack result
url = r[0]
rank = r[1][0]
title = r[1][1]
modified = r[1][2]
size = r[1][3]
keywords = r[1][4]
parents = r[1][5]
children = r[1][6]
# create dictionary
data['url'] = url;
data['rank'] = rank;
data['title'] = title;
data['modified'] = modified;
data['size'] = size;
data['keywords'] = keywords;
data['parents'] = parents;
data['children'] = children;
json_data = json.dumps(data)
res.append(json_data);
# output string containing array of json
print json.dumps([dict(result=r) for r in res])