-
Notifications
You must be signed in to change notification settings - Fork 12
/
data_handler.py
executable file
·91 lines (80 loc) · 2.61 KB
/
data_handler.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
#!/usr/bin/env python
import json
import cgi
import cgitb
import dataset
import sys
import scrape
import codecs
import string
import config
cgitb.enable()
data = cgi.FieldStorage()
def sanitize(s):
s = s.replace("'", "")
s = s.replace('"', "")
s = s.replace("/", "")
s = s.replace("\\", "")
s = s.replace(";", "")
s = s.replace("=", "")
s = s.replace("*", "")
s = s.replace("%", "")
return s
if 'get' in data and data['get'].value == "number_of_pages":
db = dataset.connect("sqlite:///leyes.db")
table = db['proyectos']
rows = table.all()
i = 0
for row in rows:
i += 1
result = {"number_of_pages": i}
print "Content-Type: application/json\n"
print json.dumps(result)
elif 'start' in data:
start = sanitize(data['start'].value)
end = sanitize(data['end'].value)
db = dataset.connect("sqlite:///leyes.db")
# We will limit to show only 20 results per page
query = "SELECT * FROM proyectos ORDER BY codigo DESC LIMIT %s OFFSET %s" % (20, start)
res = db.query(query)
out = ""
for i in res:
out += scrape.prettify(i)
print "Content-Type: application/json\n"
print json.dumps({"output": out})
elif 'search' in data:
keyword = sanitize(data['search'].value)
db = dataset.connect("sqlite:///leyes.db")
query = "SELECT short_url, codigo, titulo, pdf_url, link_to_pdf, seguimiento_page FROM proyectos WHERE "
query += "codigo like \"%" + keyword + "%\" OR "
query += "congresistas like \"%" + keyword + "%\" OR "
query += "titulo like \"%" + keyword + "%\" ORDER BY codigo DESC"
res = db.query(query)
out = []
for i in res:
out.append(i)
print "Content-Type: application/json\n"
#print query
#print json.dumps({"query": query, "keyword": keyword})
print json.dumps(out)
elif 'codigo' in data:
# This is not codigo, it is actually short_url
keyword = sanitize(data['codigo'].value)
db = dataset.connect("sqlite:///leyes.db")
table = db['proyectos']
res = table.find_one(short_url=keyword)
if res:
out = scrape.prettify(res)
f = codecs.open("base.html", "r", "utf-8")
base_html = f.read()
f.close()
html = string.replace(base_html, "{% base_url %}", config.base_url)
html = string.replace(html, "{% titulo %}", "<h1 id='proyectos_de_ley'>Proyecto de ley</h1>")
html = string.replace(html, 'contenido" class="container">',
'contenido" class="container">' + out)
print "Content-Type: text/html; charset=utf-8\n"
print html.encode("utf-8")
else:
pass
else:
pass