#coding:utf-8
from flask import Flask
from flask import jsonify
from flask import request
import re
import time,random
from util import time_to_date,time_to_day
import json
from tyc_search import Tycsearch
class Apibase:
def __init__(self):
self.mongodb = Mongobase(GsmongoDB)
self.mongodb.connect()
def get_mongoone(self,condition,tname):
return self.mongodb.select(condition,tname)
def get_mongoall(self,tname):
return self.mongodb.all_dt(tname)
def close_mongo(self):
return self.mongodb.close()
class ComplexEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj, date):
return obj.strftime('%Y-%m-%d')
else:
return json.JSONEncoder.default(self, obj)
app = Flask(__name__)
apibase = Apibase()
tycseach = Tycsearch()
@
app.route('/cha/search', methods=['GET'])
def get_tasks7():
#global m
kw = request.args.get("key")
kw = kw.replace(' (','(').replace(') ',')').replace(' ','')
print(f'请求信息:{kw}......')
try:
_ = apibase.mongodb.select({'searchkey': {'=':kw}},tname=GsmongoDB['tyc_records'])
if len(_)==0:
dic = tycseach.search(kw)
#print(dic)
return json.dumps(dic,ensure_ascii=False)
else:
dic = _[-1]
del dic['_id']
#apibase.close_mongo()
return json.dumps(dic,ensure_ascii=False)
except Exception as E:
print(f'请求失败:原因是:{E}..')
dic = {}
dic['code'] = '405'
dic['urlid'] = ""
dic['pingfen'] = ''
dic['searchkey'] = kw
dic['qymc'] = ''
dic['请求失败原因'] = '接口调用异常..'
now_time,now_day = time_to_day(time.time())
dic['climbdate'] = now_day
return json.dumps(dic,ensure_ascii=False)
@
app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
#
app.run(debug=True)
app.run(host="0.0.0.0",port=8090,threaded=True)#h