Commit 7883fc7f authored by 杨林's avatar 杨林

Merge branch 'yang' into 'master'

智能导购

See merge request !1
parents 4a48b632 c02cdaf9
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/Python" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/src/main/Python/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.7 (PythonApi)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
from fastapi import FastAPI
from estools import ESTools
from typing import Optional
from urllib.parse import unquote
import uvicorn
app = FastAPI()
@app.get("/comparison")
async def comparison(price: float, title: str, size: Optional[int] = 20, userid: Optional[str] = None):
item_name = unquote(title, encoding='utf-8')
es = ESTools()
if not es:
return {
"code": 2005,
"msg": "连接es错误"
}
else:
res = es.data_format(price=price, title=item_name, size=size)
if res:
return {
"code": 2000,
"msg": "成功",
"data": {
"size": len(res),
"list": res
}
}
else:
return {
"code": 2004,
"msg": "没有找到商品"
}
if __name__ == '__main__':
uvicorn.run(app='api:app', host="0.0.0.0", port=8000, reload=True, debug=True)
from elasticsearch import Elasticsearch
class ESTools:
"""
es工具
"""
_index = 'goods'
_host = [{"host": '172.18.45.18', "port": 9200}, {"host": '172.18.45.17', "port": 9200},
{"host": '172.18.45.16', "port": 9200}, {"host": '172.18.69.190', "port": 9200}]
_TB_RATE = 0.7
_JD_RATE = 0.55
def __init__(self):
self.es = Elasticsearch(self._host)
def search_tools(self, price=0, title="", size=20):
"""
搜索es方法
:param price: 券后价
:param title: 商品名称
:param size: 商品数量
:return: 搜索结果和分数
"""
if self.es.ping():
query = {
"query": {
"bool": {
"filter": {
"range": {
"coupon_price": {
"lte": price
}
}
},
"should": {
"match": {
"item_name": title
}
}
}
},
"from": 0,
"size": size
}
hits = self.es.search(index=self._index, body=query)
res = hits['hits']['hits']
scores = [i['_score'] for i in res]
scores = sorted(scores)
return res, scores
else:
return None
def data_format(self, price=0, title="", size=20):
"""
格式化数据方法
:param price: 券后价
:param title: 商品标题
:param size: 商品数量
:return: 格式化后的数据
"""
res, sco = self.search_tools(price=price, title=title, size=size)
if res:
temp = []
# 匹配分数平均数
score_avg = sum(sco) / len(sco)
# 匹配分数中位数
score_median = sco[int(len(sco) / 2)]
for i in res:
# 对每条数据进行处理
if i["_score"] > score_median:
if i["_score"] > score_avg:
source = i['_source']
platform = source["platform"]
coupon_commission = source["coupon_commission"]
# 给用户的佣金比例
rate = self._JD_RATE if platform == 2 else self._TB_RATE
coupon_amount = source["coupon_amount"]
discount_text = f'¥{coupon_amount}券' if coupon_amount > 0 else ''
user_commission = "{:.2f}".format(coupon_commission * rate)
end_price = source["coupon_price"]
final_price = round(price - end_price, 2)
result = {
"item_id": source["item_id"],
"item_pic": source["item_pic_url"].split(',')[0],
"item_title": source["item_name"],
"discount_type": "1",
"discount_amount": str(source["coupon_amount"]),
"platform": str(platform),
"shop_title": source["shop_name"],
"user_commission": user_commission,
"price": str(source["price"]),
"end_price": str(end_price),
"volume_text": f'已抢{source["item_volume"]}件',
"discount_text": discount_text,
"discount_tips": f'比查券商品便宜{final_price}元',
"user_commission_text": f'返现{user_commission}'
}
temp.append(result)
results = sorted(temp, key=lambda x: x["end_price"])
return results
else:
return None
debug = True
loglevel = 'debug'
bind = '0.0.0.0:8000' # 内部端口
pidfile = 'log/gunicorn.pid'
logfile = 'log/debug.log'
accesslog = 'log/access.log'
workers = 32
worker_class = 'uvicorn.workers.UvicornWorker'
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment