-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
129 lines (111 loc) · 3.23 KB
/
app.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
#!/data/project/farhangestan/www/python/venv/bin/python
"""Provide a web interface to search farhangestan.sqlite3 database."""
import sqlite3
from flask import Flask
from flask import request
from flask import redirect, url_for
from flask import render_template
app = Flask(__name__)
conn = None
CLEANUP_TALBE = ''.maketrans({
'ك': 'ک',
'ڪ': 'ک',
'ﻙ': 'ک',
'ﻚ': 'ک',
'ي': 'ی',
'ى': 'ی',
'ے': 'ی',
'ۍ': 'ی',
'ې': 'ی',
'ہ': 'ه',
'ھ': 'ه',
})
@app.route('/')
def searchform():
return redirect(url_for('static', filename='searchform.html'))
def input_cleanup(text):
"""Replace all semi-Persian characters with standard Persian characters.
Some of the substations are copied from:
fa.wikipedia: Mediawiki:Gadget-Extra-Editbuttons-persiantools.js
"""
return text.translate(CLEANUP_TALBE).replace('ە', 'ه\u200c')
@app.route('/results')
def searchresult():
get_arg = request.args.get
daftar = get_arg('daftar', '')
if daftar.isnumeric():
daftar_int = int(daftar)
daftar = str(daftar_int)
else:
daftar_int = 0
daftar = ''
word = input_cleanup(get_arg('word', ''))
wordstart = input_cleanup(get_arg('wordstart', ''))
wordend = input_cleanup(get_arg('wordend', ''))
hozeh = input_cleanup(get_arg('hozeh', ''))
offset = int(get_arg('offset', 0))
rows = query_db(daftar, word, wordstart, wordend, hozeh, offset)
return render_template(
'results.html', word=word, wordend=wordend,
wordstart=wordstart, hozeh=hozeh, daftar=daftar_int, rows=rows,
)
def query_db(*args):
global conn
conn = conn or sqlite3.connect(
'persianacademy.ir/farhangestan.sqlite3',
check_same_thread=False, # since we don't have any writing operations
)
return conn.execute(*query_and_args(*args)).fetchall()
def query_and_args(daftar, word, wordstart, wordend, hozeh, offset):
query = """
SELECT mosavab, biganeh, hozeh, tarif, daftar
FROM words
"""
args = []
if word:
query += '''WHERE (
mosavab LIKE ?
OR biganeh LIKE ?
OR tarif LIKE ?
OR pure_mosavab LIKE ?
) '''
in_where = True
args += ('%' + word + '%',) * 4
else:
in_where = False
if wordstart:
if in_where:
query += 'AND '
else:
query += 'WHERE '
in_where = True
query += '(mosavab LIKE ? OR biganeh LIKE ?) '
args += (wordstart + '%',) * 2
if wordend:
if in_where:
query += 'AND '
else:
query += 'WHERE '
in_where = True
query += '(mosavab LIKE ? OR biganeh LIKE ?) '
args += ('%' + wordend,) * 2
if hozeh:
if in_where:
query += 'AND '
else:
query += 'WHERE '
in_where = True
query += 'hozeh LIKE ? '
args += ('%' + hozeh + '%',)
if daftar:
if in_where:
query += 'AND '
else:
query += 'WHERE '
query += 'daftar = ? '
args += (daftar,)
query += 'LIMIT 50 '
if offset:
query += 'OFFSET ? '
args += (offset,)
return query, args