Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
44bf0c0c33
33
check_redis.py
Normal file
33
check_redis.py
Normal file
@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from myredis.myredis import my_redis
|
||||
|
||||
|
||||
def main():
|
||||
all_keys = my_redis.keys()
|
||||
ads_list = []
|
||||
if all_keys:
|
||||
for item in all_keys:
|
||||
if len(item.split("::")) > 3:
|
||||
print(f"find item = {item}! ")
|
||||
try:
|
||||
for one in my_redis.smembers(item):
|
||||
ads_list.append(one)
|
||||
except Exception:
|
||||
print(f"{all_keys} get from redis via 'ad::[1004|2001]*' ")
|
||||
else:
|
||||
raise Exception("redis is nothing!")
|
||||
|
||||
print(f"redis ad list ={ads_list}")
|
||||
for ad in ads_list:
|
||||
name = f"ad::{ad}::info"
|
||||
temp_data = my_redis.hgetall(name)
|
||||
if temp_data:
|
||||
pass
|
||||
#print(temp_data)
|
||||
else:
|
||||
print(f"{name} ]\tnot found!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Binary file not shown.
Binary file not shown.
@ -1,141 +1,111 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import absolute_import
|
||||
from flask import Flask, jsonify
|
||||
from flask_restful import reqparse, abort, Api, Resource
|
||||
|
||||
import logging
|
||||
from ops.base import my_unquote
|
||||
from flask import jsonify
|
||||
from flask_restful import reqparse, Resource
|
||||
|
||||
from mysql.mmysql import MysqlBase
|
||||
from prod_config import mysql_promotion_config
|
||||
|
||||
import pdb
|
||||
|
||||
import json
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
import pdb
|
||||
parser = reqparse.RequestParser()
|
||||
parser.add_argument('id')
|
||||
parser.add_argument('name')
|
||||
parser.add_argument('contact')
|
||||
parser.add_argument('appid')
|
||||
parser.add_argument('appkey')
|
||||
parser.add_argument('status')
|
||||
parser.add_argument('tel')
|
||||
parser.add_argument('mapid')
|
||||
parser.add_argument('user')
|
||||
parser.add_argument('url')
|
||||
parser.add_argument('status')
|
||||
|
||||
|
||||
|
||||
class Company(Resource):
|
||||
class PrivMaps(Resource):
|
||||
def __init__(self):
|
||||
self.args = parser.parse_args()
|
||||
self.mydb = MysqlBase(**mysql_promotion_config)
|
||||
|
||||
|
||||
def get(self):
|
||||
users = self.args['user']
|
||||
status = self.args['status']
|
||||
id = self.args['id']
|
||||
user = self.args['user']
|
||||
all_data = []
|
||||
try:
|
||||
if status:
|
||||
sql = f"select id,name,contact,tel,appid,appkey,status,user from company where status={status};"
|
||||
elif id:
|
||||
sql = f"select id,name,contact,tel,appid,appkey,status,user from company where id={id};"
|
||||
elif user:
|
||||
sql = f"select id,name,contact,tel,appid,appkey,status,user from company where user='{user}';"
|
||||
if users:
|
||||
sql = f"select user,mapid,url,status from mymaps where user='{users}';"
|
||||
elif status:
|
||||
sql = f"select user,mapid,url,status from mymaps where status={status};"
|
||||
else:
|
||||
sql = f"select id,name,contact,tel,appid,appkey,status,user from company ;"
|
||||
sql = f"SELECT user,mapid,url,status FROM mymaps ;"
|
||||
data = self.mydb.query(sql)
|
||||
|
||||
log.info(f"get data from db was {data}")
|
||||
if data:
|
||||
for line in data:
|
||||
if line:
|
||||
company = {}
|
||||
company['id'], company['name'], company['contact'], company['tel'], company['appid'], company[
|
||||
'appkey'], company['status'], company['user'] = line
|
||||
all_data.append(company)
|
||||
del company
|
||||
mymaps = {}
|
||||
mymaps['user'], mymaps['mapid'], mymaps['url'], mymaps['status'] = line
|
||||
all_data.append(mymaps)
|
||||
del mymaps
|
||||
else:
|
||||
log.info(f"{data} not found in mysql !")
|
||||
return jsonify({'code': 200, 'message': []})
|
||||
except Exception:
|
||||
log.error(f"get company status={status} failed! ", exc_info=True)
|
||||
log.error(f"get maps via args={self.args} failed! ", exc_info=True)
|
||||
return jsonify({'code': 500})
|
||||
return jsonify({'code': 200, 'message': all_data})
|
||||
|
||||
def delete(self):
|
||||
id = self.args['id']
|
||||
mapid = self.args['mapid']
|
||||
try:
|
||||
del_sql = f'update ad set in_used=0 where id={id};'
|
||||
del_sql = f"update mymaps set status=0 where mapid={mapid};"
|
||||
self.mydb.change(del_sql)
|
||||
return jsonify({'code': 200})
|
||||
except Exception:
|
||||
log.error(f"remove {id} failed!", exc_info=True)
|
||||
log.error(f"remove {self.args} failed!", exc_info=True)
|
||||
return jsonify({'code': 500})
|
||||
return jsonify({'code': 200})
|
||||
|
||||
def get_random(self, num=8):
|
||||
import random
|
||||
import string
|
||||
return ''.join(random.sample(string.ascii_letters + string.digits, num))
|
||||
|
||||
def get_appid(self):
|
||||
appid = self.get_random()
|
||||
# check appid not in db
|
||||
check_appid = f"select appid from company where appid='{appid}'"
|
||||
data = self.mydb.query(check_appid)
|
||||
if data:
|
||||
self.get_appid()
|
||||
return appid
|
||||
|
||||
def get_companyid(self):
|
||||
max_id = f"select id from company order by id desc limit 1"
|
||||
def get_newid(self):
|
||||
max_id = f"SELECT mapid FROM mymaps ORDER BY mapid DESC LIMIT 1"
|
||||
data = self.mydb.query(max_id)
|
||||
log.info(f"1 {data}")
|
||||
try:
|
||||
max = int(data[0][0]) + 1
|
||||
log.info(f"max id was {max}!")
|
||||
except Exception:
|
||||
log.error(f"error with get company id ", exc_info=True)
|
||||
log.error(f"error with get maps id ", exc_info=True)
|
||||
max = 1001
|
||||
return max
|
||||
|
||||
|
||||
def post(self):
|
||||
print(f"args={self.args}")
|
||||
try:
|
||||
company = {}
|
||||
company['name'] = self.args['name']
|
||||
company['contact'] = self.args['contact']
|
||||
company['tel'] = self.args['tel']
|
||||
company['user'] = self.args['user']
|
||||
company['id'] = self.get_companyid()
|
||||
company['appid'] = self.get_appid()
|
||||
company['appkey'] = 'undefined'
|
||||
self.mydb.insert("company", company)
|
||||
mymaps = {}
|
||||
mymaps['mapid'] = self.get_newid()
|
||||
mymaps['user'] = self.args['user']
|
||||
mymaps['url'] = self.args['url']
|
||||
self.mydb.insert("mymaps", mymaps)
|
||||
return jsonify({'code': 200})
|
||||
except Exception:
|
||||
log.error("set values to company mysql failed!", exc_info=True)
|
||||
return jsonify({'code': 500})
|
||||
return jsonify({'code': 200})
|
||||
|
||||
log.error("set values to maps mysql failed!", exc_info=True)
|
||||
return jsonify({'code': 500,"message":f"get args failed,{self.args}"})
|
||||
|
||||
def put(self):
|
||||
try:
|
||||
company = {}
|
||||
company['id'] = self.args['id']
|
||||
company['name'] = self.args['name']
|
||||
company['contact'] = self.args['contact']
|
||||
company['tel'] = self.args['tel']
|
||||
company['user'] = self.args['user']
|
||||
company['status'] = self.args['status'] or 0
|
||||
company['appid'] = self.args['appid']
|
||||
company['appkey'] = self.args['appkey']
|
||||
|
||||
sel_sql = f"select name from company where id={self.args['id']};"
|
||||
try:
|
||||
mymaps = {}
|
||||
mymaps['mapid'] = self.args['mapid']
|
||||
mymaps['user'] = self.args['user']
|
||||
mymaps['url'] = self.args['url']
|
||||
mymaps['status'] = self.args['status']
|
||||
|
||||
sel_sql = f"select url from mymaps where mapid={self.args['mapid']};"
|
||||
data = self.mydb.query(sel_sql)
|
||||
if data:
|
||||
condition = f"id='{self.args['id']}'"
|
||||
self.mydb.update("company", company, condition)
|
||||
condition = f"mapid={self.args['mapid']}"
|
||||
self.mydb.update("mymaps", mymaps, condition)
|
||||
return jsonify({'code': 200})
|
||||
else:
|
||||
return jsonify({'code': 404, 'message': f"{self.args['id']} not found in mysql!"})
|
||||
return jsonify({'code': 404, 'message': f"{self.args['mapid']} not found in mysql!"})
|
||||
except Exception:
|
||||
log.error("update values to company mysql failed!", exc_info=True)
|
||||
return jsonify({'code': 500})
|
||||
|
Binary file not shown.
1
ops/__init__.py
Normal file
1
ops/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
# -*- coding: utf-8 -*-
|
13
ops/base.py
Normal file
13
ops/base.py
Normal file
@ -0,0 +1,13 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from urllib.parse import unquote, quote, urlencode
|
||||
|
||||
|
||||
def my_unquote(data):
|
||||
return unquote(data, 'utf-8').replace("'",'"')
|
||||
|
||||
|
||||
def my_quote(data):
|
||||
if isinstance(data, dict):
|
||||
return urlencode(data)
|
||||
elif isinstance(data, str):
|
||||
return quote(data)
|
143
ops/reflush_ad.py
Normal file
143
ops/reflush_ad.py
Normal file
@ -0,0 +1,143 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from myredis.myredis import my_redis
|
||||
from mysql.mmysql import MysqlBase
|
||||
from prod_config import mysql_promotion_config
|
||||
from log.mylog import define_logger
|
||||
import logging
|
||||
import json
|
||||
|
||||
define_logger("/data/logs/reflush_ad.log")
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Reflush_AdInfo():
|
||||
def __init__(self):
|
||||
self.mysql = MysqlBase(**mysql_promotion_config)
|
||||
|
||||
def reflush_ad_via_localtionid(self, locationid):
|
||||
all = my_redis.keys()
|
||||
change_key = []
|
||||
if all:
|
||||
for one_key in all:
|
||||
if len(one_key.split("::")) > 3 and one_key.split("::")[-1] == locationid:
|
||||
change_key.append(one_key)
|
||||
else:
|
||||
raise Exception("Redis is null!")
|
||||
ad_list = []
|
||||
for item in change_key:
|
||||
temp = my_redis.smembers(item)
|
||||
for one in temp:
|
||||
ad_list.append(one)
|
||||
self.reflush_ad(ad_list)
|
||||
|
||||
|
||||
def reflush_ad(self, adids):
|
||||
# expirt key via localtionid
|
||||
for item in adids:
|
||||
key = f"ad::{item}::info"
|
||||
my_redis.expire(key, 1)
|
||||
|
||||
all_data = []
|
||||
# add new key
|
||||
sql = f"""select
|
||||
a.id,
|
||||
a.name,
|
||||
a.ad_num,
|
||||
a.ad_title,
|
||||
a.ad_body,
|
||||
a.ad_image,
|
||||
a.jump_param,
|
||||
a.ad_sort,
|
||||
a.companyid,
|
||||
a.gameid,
|
||||
a.channelid,
|
||||
a.jump_status,
|
||||
a.ad_property,
|
||||
b.area,
|
||||
b.type,
|
||||
b.mode,
|
||||
a.locationid
|
||||
from
|
||||
(select
|
||||
id,
|
||||
name,
|
||||
ad_num,
|
||||
ad_title,
|
||||
ad_body,
|
||||
ad_image,
|
||||
jump_param,
|
||||
ad_sort,
|
||||
companyid,
|
||||
gameid,
|
||||
channelid,
|
||||
jump_status,
|
||||
locationid ,
|
||||
ad_property
|
||||
from
|
||||
ad
|
||||
where
|
||||
status=1
|
||||
and in_used=1
|
||||
and id={item})a ,
|
||||
(select
|
||||
id,
|
||||
area,
|
||||
type,
|
||||
mode
|
||||
from
|
||||
location) b
|
||||
where
|
||||
a.locationid=b.id
|
||||
"""
|
||||
data = self.mysql.query(sql)
|
||||
if data:
|
||||
for line in data:
|
||||
item = {}
|
||||
try:
|
||||
item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], \
|
||||
item['jump_param'], item['ad_sort'], item['companyid'], item['gameid'], item['channelid'], \
|
||||
item['jump_status'], item['ad_property'], item['area'], item['type'], item['mode'], item[
|
||||
'locationid'] = line
|
||||
try:
|
||||
item['x_offset'] = item['area'].split(',')[-2].strip()
|
||||
except:
|
||||
item['x_offset'] = 0
|
||||
try:
|
||||
item['y_offset'] = item['area'].split(',')[-1].strip().strip(')')
|
||||
except:
|
||||
item['y_offset'] = 0
|
||||
|
||||
if item.get('jump_param', "") and (item['ad_property'].find("jump_param") == -1):
|
||||
try:
|
||||
temp = {}
|
||||
temp['jump_param'] = item.get('jump_param', "")
|
||||
if not item['ad_property']:
|
||||
item['ad_property'] = {}
|
||||
if not isinstance(item.get('ad_property', {}), dict):
|
||||
if not item.get('ad_property', {}):
|
||||
item['ad_property'] = {}.update(temp)
|
||||
else:
|
||||
item['ad_property'] = json.loads(
|
||||
item.get('ad_property', {}).replace("‘", '"').replace("’", '"'))
|
||||
item['ad_property'].update(temp)
|
||||
item['ad_property'] = json.dumps(item['ad_property'])
|
||||
except Exception:
|
||||
log.error(f"write {item}", exc_info=True)
|
||||
|
||||
all_data.append(item)
|
||||
except Exception:
|
||||
log.error("split data failed", exc_info=True)
|
||||
|
||||
for item in all_data:
|
||||
my_redis.hmset(f"ad::{item['id']}::info", item)
|
||||
my_redis.expire(f"ad::{item['id']}::info", 3600 * 24)
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
cc = Reflush_AdInfo()
|
||||
cc.reflush_ad_via_localtionid('1016')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
24
print_redis.py
Normal file
24
print_redis.py
Normal file
@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from myredis.myredis import my_redis
|
||||
|
||||
def main():
|
||||
key = '1066'
|
||||
all_keys = my_redis.keys(pattern=f'*::{key}')
|
||||
ads_list = []
|
||||
if all_keys:
|
||||
for item in all_keys:
|
||||
try:
|
||||
for one in my_redis.smembers(item):
|
||||
ads_list.append(one)
|
||||
except Exception:
|
||||
print(f"{all_keys} get from redis via '*::{key}' ")
|
||||
|
||||
for ad in ads_list:
|
||||
name = f"ad::{ad}::info"
|
||||
temp_data = my_redis.hgetall(name)
|
||||
print(temp_data)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
@ -22,7 +22,7 @@ http://154.8.214.202:5015/interface/maps
|
||||
|
||||
http://154.8.214.202:5015/maps
|
||||
|
||||
http://154.8.214.202:5015/interface/maps?user=xxx
|
||||
http://154.8.214.202:5015/interface/maps?status=0
|
||||
|
||||
##### 请求参数说明
|
||||
|
||||
@ -62,7 +62,7 @@ http://154.8.214.202:5015/interface/maps
|
||||
|
||||
##### 请求示范
|
||||
|
||||
http://154.8.214.202:5015/interface/company?
|
||||
http://154.8.214.202:5015/interface/maps
|
||||
|
||||
##### 请求参数说明
|
||||
|
||||
@ -96,7 +96,7 @@ http://154.8.214.202:5015/interface/mpas
|
||||
|
||||
##### 请求示范
|
||||
|
||||
http://154.8.214.202:5015/interface/company?appid=zpoHY37u&appkey=undefined&contact=miles001&id=1006&name=kingsome&status=1&tel=17771&user=assd
|
||||
http://154.8.214.202:5015/interface/maps
|
||||
|
||||
##### 请求参数说明
|
||||
|
||||
@ -124,14 +124,13 @@ http://154.8.214.202:5015/interface/company?appid=zpoHY37u&appkey=undefined&cont
|
||||
##### 接口地址
|
||||
|
||||
http://154.8.214.202:5015/interface/maps
|
||||
|
||||
##### 返回格式:json
|
||||
|
||||
##### 请求方式:delete
|
||||
|
||||
##### 请求示范
|
||||
|
||||
http://154.8.214.202:5015/interface/maps
|
||||
http://154.8.214.202:5015/interface/maps?mapid=1006
|
||||
|
||||
##### 请求参数说明
|
||||
|
||||
|
@ -10,6 +10,7 @@ from handler.company import Company
|
||||
from handler.location import Location
|
||||
from handler.ad import Ad
|
||||
from handler.ad_image_upload import Cos_sdk
|
||||
from handler.priv_map import PrivMaps
|
||||
|
||||
define_logger("/data/logs/promotion.log")
|
||||
log = logging.getLogger(__name__)
|
||||
@ -23,7 +24,7 @@ api.add_resource(Company, '/interface/company')
|
||||
api.add_resource(Location, '/interface/location')
|
||||
api.add_resource(Ad, '/interface/promotion')
|
||||
api.add_resource(Cos_sdk, '/interface/cos_upload')
|
||||
|
||||
api.add_resource(PrivMaps, '/interface/maps')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', debug=True, port=5015)
|
||||
|
Loading…
x
Reference in New Issue
Block a user