/
search_service.py
40 lines (31 loc) · 1.26 KB
/
search_service.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
import urlparse
import os, sys, re
from flask import Flask, request, jsonify, abort, json, make_response
from msc_apps import *
import glob, json
import requests
from transaction_service import gettransaction
#tools_dir = os.environ.get('TOOLSDIR')
#lib_path = os.path.abspath(tools_dir)
#sys.path.append(lib_path)
#data_dir_root = os.environ.get('DATADIR')
app = Flask(__name__)
app.debug = True
@app.route('/', methods=['GET'])
def search():
if 'query' in request.args:
query = re.sub(r'\W+', '0', request.args.get('query') ) # strip and get query
else:
return jsonify({ 'status': 400, 'data': 'No query found in request' })
if query[:2] in ['0x','0X']:
return jsonify({ 'status': 400, 'data': 'Invalid search query prefix.' })
if len(query) < 3:
return jsonify({ 'status': 400, 'data': 'Search query to short.' })
ROWS=dbSelect("select * from transactions t, txjson txj where t.txhash ~* \'" + str(query) + "\' and t.txdbserialnum=txj.txdbserialnum")
response = []
if len(ROWS) > 0:
for queryrow in ROWS:
#res = requests.get('http://localhost/v1/transaction/tx/' + queryrow[0] + '.json').json()[0]
res = json.loads(gettransaction(queryrow[0]))[0]
response.append(res)
return jsonify({ 'status': 200, 'data': response })