This commit is contained in:
pengtao 2019-08-09 10:47:06 +08:00
parent bd895eca46
commit 2ea83ae69e
17 changed files with 573 additions and 318 deletions

364
README.MD
View File

@ -2,11 +2,8 @@
TODO
广告添加 own字段
返回所有广告[status]
返回单条广告
图片上传接口
区域/类型 增删改检查的接口
@ -14,7 +11,7 @@ TODO
##### 接口地址
http://192.168.100.20:8888/company
http://154.8.214.202:5015/interface/company
##### 返回格式json
@ -24,7 +21,7 @@ http://192.168.100.20:8888/company
http://154.8.214.202:5015/company
http://192.168.100.20:8888/company?status=0
http://154.8.214.202:5015/interface/company?status=0
##### 请求参数说明
@ -47,36 +44,26 @@ http://192.168.100.20:8888/company?status=0
| companykey | string | 是 | 用于API认证 |
##### 返回示例
{
"code": 200,
"message": [
{
"appid": "zpoHY37u",
"appkey": "undefined",
"contact": "miles",
"id": "1006",
"name": "kingsome",
"status": 0,
"tel": "17771"
},
{
"appid": "5bOaA82t",
"appkey": "undefined",
"contact": "miles",
"id": "1007",
"name": "kingsome",
"status": 0,
"tel": "17771"
}
]
"code": 200,
"message": [
{
"appid": "BKadJbmZ",
"appkey": "undefined",
"contact": "pt",
"id": "1001",
"name": "kings",
"status": 0,
"tel": "1234",
"user": "None"
}
]
}
#### 2、新增公司信息
##### 接口地址
http://192.168.100.20:8888/company
http://154.8.214.202:5015/interface/company
##### 返回格式json
@ -84,7 +71,7 @@ http://192.168.100.20:8888/company
##### 请求示范
http://192.168.100.20:8888/company?name=kings&contact=pt&tel=1234
http://154.8.214.202:5015/interface/company?name=kings&contact=pt&tel=1234
##### 请求参数说明
@ -112,7 +99,7 @@ http://192.168.100.20:8888/company?name=kings&contact=pt&tel=1234
##### 接口地址
http://192.168.100.20:8888/company
http://154.8.214.202:5015/interface/company
##### 返回格式json
@ -120,7 +107,7 @@ http://192.168.100.20:8888/company
##### 请求示范
http://192.168.100.20:8888/company?appid=zpoHY37u&appkey=undefined&contact=miles001&id=1006&name=kingsome&status=1&tel=17771&user=assd
http://154.8.214.202:5015/interface/company?appid=zpoHY37u&appkey=undefined&contact=miles001&id=1006&name=kingsome&status=1&tel=17771&user=assd
##### 请求参数说明
@ -152,7 +139,7 @@ http://192.168.100.20:8888/company?appid=zpoHY37u&appkey=undefined&contact=miles
##### 接口地址
http://192.168.100.20:8888/company
http://154.8.214.202:5015/interface/company
##### 返回格式json
@ -160,7 +147,7 @@ http://192.168.100.20:8888/company
##### 请求示范
http://192.168.100.20:8888/company?id=1006
http://154.8.214.202:5015/interface/company?id=1006
##### 请求参数说明
@ -185,7 +172,7 @@ http://192.168.100.20:8888/company?id=1006
##### 接口地址
http://192.168.100.20:8888/location
http://154.8.214.202:5015/interface/location
##### 返回格式json
@ -193,13 +180,14 @@ http://192.168.100.20:8888/location
##### 请求示范
http://192.168.100.20:8888/location
http://154.8.214.202:5015/interface/location?gameid=1004
##### 请求参数说明
| 名称 | 类型 | 必填 | 说明 |
| ------ | ---- | ---- | ------ |
| gameid | int | 是 | 游戏ID |
| 名称 | 类型 | 必填 | 说明 |
| --------- | ---- | ---- | ------------------ |
| gameid | int | 是 | 游戏ID |
| channelid | int | 是 | 渠道ID默认是6001 |
##### 返回参数说明
@ -209,24 +197,24 @@ http://192.168.100.20:8888/location
| message | json | 是 | 所有位置信息 |
##### 返回示例
{
"code": 200,
"message": [
{
"area": 1,
"gameid": 1003,
"id": 1001,
"type": 1
}
]
"code": 200,
"message": [
{
"area": "1,8,15",
"channelid": 6001,
"gameid": 1004,
"id": 1007,
"mode": 1,
"type": 2
}
]
}
#### 6、添加位置服务信息
##### 接口地址
http://192.168.100.20:8888/location
http://154.8.214.202:5015/interface/location
##### 返回格式json
@ -234,15 +222,17 @@ http://192.168.100.20:8888/location
##### 请求示范
http://192.168.100.20:8888/location?area=1&type=1&gameid=1003
http://154.8.214.202:5015/interface/location?gameid=1003&area=1,1,1&type=1&mode=2
##### 请求参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | ---------------------------------------- |
| area | int | 是 | 区域(1=首页2=游戏中3=结算页4=关闭) |
| type | int | 是 | 类型1=icon2=banner3=浮窗) |
| | int | 是 | 游戏ID |
| 名称 | 类型 | 必填 | 说明 |
| --------- | ------ | ---- | ------------------------------------------------------------ |
| area | string | 是 | 区域(第一元素1=首页2=游戏中3=结算页4=关闭第二元素X轴第三元素Y轴其中X,Y取值范围0,15)如1,0,0代表首页左上 |
| type | int | 是 | 类型1=icon2=banner3=浮窗) |
| mode | int | 是 | 广告单播或轮播模式1=单播2=轮播) |
| gameid | int | 是 | 游戏ID |
| channelid | int | 否 | 渠道ID默认是6001 |
##### 返回参数说明
@ -256,89 +246,11 @@ http://192.168.100.20:8888/location?area=1&type=1&gameid=1003
"code": 200
}
#### 7、删除位置服务信息
#### 7、变更位置服务信息
##### 接口地址
http://192.168.100.20:8888/location
##### 返回格式json
##### 请求方式delete
##### 请求示范
http://192.168.100.20:8888/location?id=1002
##### 请求参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | -------------- |
| id | int | 是 | 需删除的位置ID |
##### 返回参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | -------- |
| code | int | 是 | 返回状态 |
##### 返回示例
{
"code": 200
}
#### 8、添加广告信息
##### 接口地址
http://192.168.100.20:8888/ad
##### 返回格式json
##### 请求方式post
##### 请求示范
http://192.168.100.20:8888/ad?name=abc&gameid=1003&locationid=1&ad_title=hello&ad_body=hi world&ad_image=http://1&ad_url=http://2&companyid=2
##### 请求参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---------- | -------- | ---- | --------------------------------------------------- |
| name | string | 是 | 广告名称 |
| gameid | int | 是 | 广告投放的游戏ID |
| locationid | int | 是 | 位置ID |
| ad_title | string | 是 | 广告标题 |
| ad_body | string | 是 | 广告正文 |
| ad_image | string | 是 | 头像链接 |
| ad_url | string | 是 | 跳转链接 |
| begin_time | datetime | 否 | 开始时间默认是1999-01-01 |
| end_time | datetime | 否 | 结束时间默认是3000-01-01 |
| ad_num | int | 否 | 投放次数默认是0(次数无限) |
| ad_sort | int | 否 | 优先级默认为0 |
| status | int | 否 | 默认为0未审批1=审批通过2=审批未通过3=暂停 |
| companyid | int | 是 | 广告提交公司 |
##### 返回参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | -------- |
| code | int | 是 | 返回状态 |
##### 返回示例
{
"code": 200
}
#### 9、变更广告信息审批
##### 接口地址
http://192.168.100.20:8888/ad
http://154.8.214.202:5015/interface/location
##### 返回格式json
@ -346,28 +258,19 @@ http://192.168.100.20:8888/ad
##### 请求示范
http://192.168.100.20:8888/ad?name=abc&gameid=1003&locationid=1&ad_title=hello&ad_body=hi world&ad_image=http://1&ad_url=http://2&begin_time=2019-06-01&end_time=2019-12-01&ad_num=300&ad_sort=1&status=1&id=1001&companyid=2
http://154.8.214.202:5015/interface/location?area=1&type=1&gameid=1003
##### 请求参数说明
#####
| 名称 | 类型 | 必填 | 说明 |
| ---------- | -------- | ---- | --------------------------------------------------- |
| id | int | 是 | 广告ID |
| name | string | 是 | 广告名称 |
| gameid | int | 是 | 广告投放的游戏ID |
| locationid | int | 是 | 位置ID |
| ad_title | string | 是 | 广告标题 |
| ad_body | string | 是 | 广告正文 |
| ad_image | string | 是 | 头像链接 |
| ad_url | string | 是 | 跳转链接 |
| begin_time | datetime | 是 | 开始时间默认是1999-01-01 |
| end_time | datetime | 是 | 结束时间默认是3000-01-01 |
| ad_num | int | 是 | 投放次数,默认是-1(无限) |
| ad_sort | int | 是 | 优先级默认为0 |
| status | int | 是 | 默认为0未审批1=审批通过2=审批未通过3=暂停 |
| companyid | int | 是 | 广告提交公司 |
| 名称 | 类型 | 必填 | 说明 |
| --------- | ------ | ---- | ------------------------------------------------------------ |
| area | string | 是 | 区域(第一元素1=首页2=游戏中3=结算页4=关闭第二元素X轴第三元素Y轴其中X,Y取值范围0,15)如1,0,0代表首页左上 |
| type | int | 是 | 类型1=icon2=banner3=浮窗) |
| mode | int | 是 | 广告单播或轮播模式1=单播2=轮播) |
| id | int | 是 | 位置ID |
| | int | 是 | 状态 0=未审批1=审批通过2=审批未通过默认为1 |
| gameid | int | 是 | 游戏ID |
| channelid | int | 否 | 渠道ID默认是6001 |
##### 返回参数说明
@ -381,11 +284,11 @@ http://192.168.100.20:8888/ad?name=abc&gameid=1003&locationid=1&ad_title=hello&a
"code": 200
}
#### 10、删除广告信息
#### 8、删除位置服务信息
##### 接口地址
http://192.168.100.20:8888/ad
http://154.8.214.202:5015/interface/location
##### 返回格式json
@ -393,7 +296,7 @@ http://192.168.100.20:8888/ad
##### 请求示范
http://192.168.100.20:8888/ad
http://154.8.214.202:5015/interface/location?id=1015
##### 请求参数说明
@ -413,11 +316,140 @@ http://192.168.100.20:8888/ad
"code": 200
}
#### 11、返回广告列表信息
#### 9、添加广告信息
##### 接口地址
http://192.168.100.20:8888/ad
http://154.8.214.202:5015/interface/prometion
##### 返回格式json
##### 请求方式post
##### 请求示范
http://154.8.214.202:5015/interface/promotion?name=abc&gameid=1003&locationid=1007&ad_title=hello&ad_body=hi world&ad_image=http://1&ad_url=http://2&companyid=2&ad_property={is_shake:1}&jump_status=1
##### 请求参数说明
| 名称 | 类型 | 必填 | 说明 |
| ----------- | -------- | ---- | ------------------------------------------------------- |
| name | string | 是 | 广告名称 |
| gameid | int | 是 | 广告投放的游戏ID |
| channelid | int | 是 | 渠道ID默认是6001 NEW |
| locationid | int | 是 | 位置ID通过该ID到localtion表中获得areatypemode信息 |
| ad_title | string | 是 | 广告标题 |
| ad_body | string | 是 | 广告正文 |
| ad_image | string | 是 | 头像链接 |
| ad_url | string | 是 | 跳转链接 |
| begin_time | datetime | 否 | 开始时间默认是1999-01-01 |
| end_time | datetime | 否 | 结束时间默认是3000-01-01 |
| ad_num | int | 否 | 投放次数默认是0(次数无限) |
| ad_sort | int | 否 | 优先级默认为0 |
| status | int | 否 | 默认为0未审批1=审批通过2=审批未通过3=暂停 |
| companyid | int | 是 | 广告提交公司 |
| ad_property | json | 是 | 广告属性描述 |
##### 返回参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | -------- |
| code | int | 是 | 返回状态 |
##### 返回示例
{
"code": 200
}
#### 10、变更广告信息审批
##### 接口地址
http://154.8.214.202:5015/interface/promotion
##### 返回格式json
##### 请求方式put
##### 请求示范
http://154.8.214.202:5015/interface/promotion?name=abc&gameid=1003&locationid=1&ad_title=hello&ad_body=hi world&ad_image=http://1&ad_url=http://2&begin_time=2019-06-01&end_time=2019-12-01&ad_num=300&ad_sort=1&status=1&id=1001&companyid=2
##### 请求参数说明
#####
| 名称 | 类型 | 必填 | 说明 |
| ----------- | -------- | ---- | --------------------------------------------------- |
| id | int | 是 | 广告ID |
| name | string | 是 | 广告名称 |
| gameid | int | 是 | 广告投放的游戏ID |
| channelid | int | 是 | 渠道ID默认是6001 NEW |
| locationid | int | 是 | 位置ID |
| ad_title | string | 是 | 广告标题 |
| ad_body | string | 是 | 广告正文 |
| ad_image | string | 是 | 头像链接 |
| ad_url | string | 是 | 跳转链接 |
| begin_time | datetime | 是 | 开始时间默认是1999-01-01 |
| end_time | datetime | 是 | 结束时间默认是3000-01-01 |
| ad_num | int | 是 | 投放次数,默认是-1(无限) |
| ad_sort | int | 是 | 优先级默认为0 |
| status | int | 是 | 默认为0未审批1=审批通过2=审批未通过3=暂停 |
| companyid | int | 是 | 广告提交公司 |
| ad_property | json | 是 | 广告属性描述 |
##### 返回参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | -------- |
| code | int | 是 | 返回状态 |
##### 返回示例
{
"code": 200
}
#### 11、删除广告信息实际将status 改为4
##### 接口地址
http://154.8.214.202:5015/interface/promotion
##### 返回格式json
##### 请求方式delete
##### 请求示范
http://154.8.214.202:5015/interface/promotion
##### 请求参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | -------------- |
| id | int | 是 | 需删除的位置ID |
##### 返回参数说明
| 名称 | 类型 | 必填 | 说明 |
| ---- | ---- | ---- | -------- |
| code | int | 是 | 返回状态 |
##### 返回示例
{
"code": 200
}
#### 12、返回广告列表信息
##### 接口地址
http://154.8.214.202:5015/interface/promotion
##### 返回格式json
@ -425,11 +457,11 @@ http://192.168.100.20:8888/ad
##### 请求示范
http://192.168.100.20:8888/ad?id=1002
http://154.8.214.202:5015/interface/promotion?id=1002
http://192.168.100.20:8888/ad?companyid=0
http://154.8.214.202:5015/interface/promotion?companyid=0
http://192.168.100.20:8888/ad?status=0
http://154.8.214.202:5015/interface/promotion?status=0
##### 请求参数说明
@ -481,3 +513,5 @@ http://192.168.100.20:8888/ad?status=0
}
}
#### ad_channel 获得接口
http://mp.kingsome.cn/api/open/promotion/get-uid?game_id=8002&platform_id=6001

Binary file not shown.

View File

@ -14,103 +14,15 @@ import logging
from config import BEGIN, END, ad_list_interface_port
from tornado import gen
import pdb
from urllib.parse import unquote
define_logger("/data/logs/ad_interface_tornado.log")
log = logging.getLogger(__name__)
limit = 3
limit = 10
def send_cache_data():
mydb = MysqlBase(**mysql_promotion_config)
log.info("start update cache !")
now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
all = []
# 添加无天数限定的记录
get_full_data = f"select id,name,ad_num,ad_title,ad_body,ad_image,jump_param,ad_sort,companyid,locationid,gameid,jump_status from ad where begin_time='{BEGIN}' or end_time='{END}'"
full_data = mydb.query(get_full_data)
if full_data:
for line in full_data:
if line:
log.info(f"line was {line}")
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['locationid'], item['gameid'], item[
'jump_status'] = line
all.append(item)
except Exception:
log.error("split data failed", exc_info=True)
# 添加有天数限定的记录
get_data_sql = f"select id,name,ad_num,ad_title,ad_body,ad_image,jump_param,ad_sort,companyid,locationid,gameid," \
f"jump_status from ad where '{now}'>begin_time and '{now}'<end_time ;"
data = mydb.query(get_data_sql)
if data:
for line in data:
if line:
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['locationid'], item['gameid'], item[
'jump_status'] = line
all.append(item)
except Exception:
log.error("split data failed", exc_info=True)
# 检查ID是否存在播放列表中以及播放次数是否完毕
if all:
# log.info(f"get data was {all}!\n")
for line in all:
key = f"ad::{line.get('gameid', 0)}::{line.get('locationid', 0)}"
if int(line['ad_num']) > 0:
num = my_redis.get(f"ad::{line['id']}::num")
if not num:
num = 0
if int(line['ad_num']) > int(num):
my_redis.sadd(key, line['id'])
my_redis.expire(key, 120)
n = int(line['ad_num']) - int(num)
log.info(f"add {line['id']} to {key} ,num was {line['ad_num']},limit was {n}!")
else:
if my_redis.sismember(key, line['id']):
my_redis.srem(key, line['id'])
log.info(f"remove {line['id']} from {key}!")
elif int(line['ad_num']) == 0:
my_redis.sadd(key, line['id'])
my_redis.expire(key, 120)
log.info(f"add {line['id']} to {key} ,num was unlimit !")
else:
log.error(f"get ad_num from mysql failed! ad_num={line['ad_num']}")
if not my_redis.exists(f"ad::{line['id']}::info"):
# remove some filed from adinfo
remove_list = ('ad_num', 'companyid', 'gameid', 'locationid')
for item in remove_list:
line.pop(item)
my_redis.hmset(f"ad::{line['id']}::info", line)
my_redis.expire(f"ad::{line['id']}::info", 120)
# my_redis.expire(f"ad::{line['id']}::info", 3600 * 24 * 7)
log.info(f"add ad::{line['id']}::info to redis!")
# 删除过期的数据
log.info("remove expire data from cache!")
expire_sql = f"select id,gameid,locationid from ad where '{now}'>end_time"
remove_data = mydb.query(expire_sql)
if remove_data:
for line in remove_data:
try:
id, gameid, locationid = line
key = f"{gameid}:{locationid}"
if my_redis.sismember(key, id):
my_redis.srem(key, id)
log.info(f"remove {id} from {key} success!")
except Exception:
log.error("拆解过期数据出错!", exc_info=True)
class DispatchHandler(tornado.web.RequestHandler):
@gen.coroutine
@ -127,16 +39,22 @@ class DispatchHandler(tornado.web.RequestHandler):
def _upAdRecording(self):
try:
adid = self.get_query_argument('adid')
adid = unquote(self.get_query_argument('adid'), 'utf-8')
log.info(f" get adid was {adid}")
ids = json.loads(adid)
except Exception:
result = {'errcode': 2, "errmsg": f"get args failed`"}
log.error(result, exc_info=True)
self.write({'errcode': 1, "errmsg": 'get adid failed!'})
if adid:
key = f"ad::{adid}::num"
my_redis.incr(key, amount=1)
self.write({'errcode': 0, "errmsg": '', "message": f"{adid} incr success!"})
if ids:
for id in ids:
if id:
key = f"ad::{id}::num"
my_redis.incr(key, amount=1)
self.write({'errcode': 0, "errmsg": '', "message": f"{ids} incr success!"})
else:
self.write({'errcode': 1, "errmsg": 'get adid failed!'})
def _selfCheckingHandler(self):
@ -149,44 +67,58 @@ class DispatchHandler(tornado.web.RequestHandler):
try:
input = json.loads(self.get_query_argument('body'))
gameid = input['gameid']
locationid = input['locationid']
channelid=input.get('channelid',None) or 6001
area=input.get('area',0)
locationid = input.get('locationid',0)
except Exception as e:
result = {'errcode': 2, "errmsg": f"get args failed,{str(e)}"}
log.error(result)
self.write_error(2)
return False
return self.write_error(2)
if gameid and locationid:
key = f"ad::{gameid}::{locationid}"
ids = my_redis.smembers(key)
info = []
if not ids:
result = {'errcode': 0, "errmsg": '', "message": {"totoal": len(info), "result": info}}
else:
try:
# 如果取得的记录条数大于预设,扔掉多余的记录,当前采用的是随机选择,以后可能需要添加加权选择
id_list = []
if limit < len(ids):
nums = limit
else:
nums = len(ids)
for i in range(nums):
while 1:
new = my_redis.srandmember(key)
if new not in id_list:
id_list.append(new)
break
for id in id_list:
temp = my_redis.hgetall(f"ad::{id}::info")
info.append(temp)
result = {'errcode': 0, "errmsg": '', "message": {"totoal": len(info), "result": info}}
except Exception as e:
result = {'errcode': 1, "errmsg": e}
self.write(result)
key_word = f"ad::{gameid}::{channelid}::*::{locationid}"
elif gameid and area:
key_word = f"ad::{gameid}::{channelid}::{area}*"
else:
result = {'errcode': 2, "errmsg": f"get args failed!"}
self.write(result)
pass
ad_keys = my_redis.keys(key_word)
ids = []
for ad_key in ad_keys:
adlists = my_redis.smembers(ad_key)
try:
for key in adlists:
ids.append(key)
except Exception:
log.error(f"get redis data failed!", exc_info=True)
return self.write({'errcode': 2, "errmsg": f"get redis data failed!"})
dist_ids = list(set(ids))
info = []
if not dist_ids:
result = {'errcode': 0, "errmsg": '', "message": {"totoal": len(info), "result": info}}
else:
try:
# 如果取得的记录条数大于预设,扔掉多余的记录,当前采用的是随机选择,以后可能需要添加加权选择
id_list = []
if limit < len(dist_ids):
nums = limit
else:
nums = len(dist_ids)
for i in range(nums):
while 1:
# new = my_redis.srandmember(key)
import random
new = random.choice(dist_ids)
if new not in id_list:
id_list.append(new)
break
for id in id_list:
temp = my_redis.hgetall(f"ad::{id}::info")
info.append(temp)
result = {'errcode': 0, "errmsg": '', "message": {"totoal": len(info), "result": info}}
except Exception as e:
result = {'errcode': 1, "errmsg": e}
return self.write(result)
def make_app():
@ -195,8 +127,6 @@ def make_app():
if __name__ == "__main__":
print('start!')
send_cache_data()
app = make_app()
app.listen(ad_list_interface_port)
tornado.ioloop.PeriodicCallback(send_cache_data, 60000).start()
tornado.ioloop.IOLoop.current().start()

160
ad_tasks.py Normal file
View File

@ -0,0 +1,160 @@
# -*- coding: utf-8 -*-
from myredis.myredis import my_redis
import datetime
from mysql.mmysql import MysqlBase
from config import mysql_promotion_config
from log.mylog import define_logger
import logging
from config import BEGIN, END, ad_list_interface_port
import pdb
define_logger("/data/logs/ad_tasks.log")
log = logging.getLogger(__name__)
limit = 10
def send_cache_data():
mydb = MysqlBase(**mysql_promotion_config)
log.info("start update cache !")
now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
all = []
get_data_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 '{now}'> begin_time
and '{now}'<end_time ) a ,
(select
id,
area,
type,
mode
from
location) b
where
a.locationid=b.id"""
data = mydb.query(get_data_sql)
if data:
for line in data:
if line:
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
all.append(item)
except Exception:
log.error("split data failed", exc_info=True)
# 检查ID是否存在播放列表中以及播放次数是否完毕
if all:
# log.info(f"get data was {all}!\n")
for line in all:
key = f"ad::{line.get('gameid', 0)}::{line.get('channelid', 0)}::{line.get('area').split(',')[0].strip()}::{line.get('locationid', 0)}"
if int(line['ad_num']) > 0:
num = my_redis.get(f"ad::{line['id']}::num")
if not num:
num = 0
if int(line['ad_num']) > int(num):
my_redis.sadd(key, line['id'])
my_redis.expire(key, 120)
n = int(line['ad_num']) - int(num)
log.info(f"add {line['id']} to {key} ,num was {line['ad_num']},limit was {n}!")
else:
if my_redis.sismember(key, line['id']):
my_redis.srem(key, line['id'])
log.info(f"remove {line['id']} from {key}!")
elif int(line['ad_num']) == 0:
my_redis.sadd(key, line['id'])
my_redis.expire(key, 120)
log.info(f"add {line['id']} to {key} ,num was unlimit !")
else:
log.error(f"get ad_num from mysql failed! ad_num={line['ad_num']}")
if not my_redis.exists(f"ad::{line['id']}::info"):
# remove some filed from adinfo
remove_list = ('ad_num', 'gameid')
for item in remove_list:
line.pop(item)
my_redis.hmset(f"ad::{line['id']}::info", line)
my_redis.expire(f"ad::{line['id']}::info", 120)
# my_redis.expire(f"ad::{line['id']}::info", 3600 * 24 * 7)
log.info(f"add ad::{line['id']}::info to redis!")
# 删除过期的数据
log.info("remove expire data from cache!")
expire_sql = f"""select
a.id,a.gameid,a.channelid,a.locationid,a.status,b.area
from
(select
id,gameid,channelid,locationid,status
from
ad
where
'{now}'> end_time
or status in (3,4) ) a ,
(select
id,
area
from
location) b
where
a.locationid=b.id"""
remove_data = mydb.query(expire_sql)
if remove_data:
for line in remove_data:
try:
id, gameid, channelid, locationid, _, area = line
key = f"ad::{gameid}::{channelid}::{area.split(',')[0].strip()}::{locationid}"
# key = f"{gameid}:{locationid}"
if my_redis.sismember(key, id):
my_redis.srem(key, id)
my_redis.expire(f"ad::{id}::info", 1)
log.info(f"remove {id} from {key} success!")
except Exception:
log.error("拆解过期数据出错!", exc_info=True)
if __name__ == "__main__":
log.info('start!')
send_cache_data()

View File

@ -24,6 +24,7 @@ log = logging.getLogger(__name__)
parser = reqparse.RequestParser()
parser.add_argument('id')
parser.add_argument('gameid')
parser.add_argument('channelid')
parser.add_argument('name')
parser.add_argument('locationid')
parser.add_argument('begin_time')
@ -37,6 +38,7 @@ parser.add_argument('ad_sort')
parser.add_argument('status')
parser.add_argument('companyid')
parser.add_argument('jump_param')
parser.add_argument('ad_property')
class Ad(Resource):
def __init__(self):
@ -54,17 +56,120 @@ class Ad(Resource):
# now = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
if not id:
if companyid:
sel_sql = f"select id,name,begin_time,end_time,ad_num,ad_title,ad_body,ad_image,jump_param,ad_sort," \
f"status,companyid,locationid,gameid,jump_status from ad where companyid={companyid};"
sel_sql = f"""select
a.id,
a.name,
a.begin_time,
a.end_time,
a.ad_num,
a.ad_title,
a.ad_body,
a.ad_image,
a.jump_param,
a.ad_sort,
a.status,
a.companyid,
a.locationid,
a.gameid,
a.channelid,
a.jump_status,
a.ad_property,
b.area,
b.type,
b.mode
from
ad a,
location b
where
a.locationid=b.id
and a.in_used=1
and a.companyid={companyid};"""
elif status or status == 0:
sel_sql = f"select id,name,begin_time,end_time,ad_num,ad_title,ad_body,ad_image,jump_param,ad_sort," \
f"status,companyid,locationid,gameid,jump_status from ad where status={status};"
sel_sql = f"""select
a.id,
a.name,
a.begin_time,
a.end_time,
a.ad_num,
a.ad_title,
a.ad_body,
a.ad_image,
a.jump_param,
a.ad_sort,
a.status,
a.companyid,
a.locationid,
a.gameid,
a.channelid,
a.jump_status,
a.ad_property,
b.area,
b.type,
b.mode
from
ad a,
location b
where
a.locationid=b.id
and a.in_used=1
and a.status={status};"""
else:
sel_sql = f"select id,name,begin_time,end_time,ad_num,ad_title,ad_body,ad_image,jump_param,ad_sort," \
f"status,companyid,locationid,gameid,jump_status from ad ;"
sel_sql = f"""select
a.id,
a.name,
a.begin_time,
a.end_time,
a.ad_num,
a.ad_title,
a.ad_body,
a.ad_image,
a.jump_param,
a.ad_sort,
a.status,
a.companyid,
a.locationid,
a.gameid,
a.channelid,
a.jump_status,
a.ad_property,
b.area,
b.type,
b.mode
from
ad a,
location b
where
a.locationid=b.id
and a.in_used=1 ;"""
else:
sel_sql = f"select id,name,begin_time,end_time,ad_num,ad_title,ad_body,ad_image,jump_param,ad_sort," \
f"status,companyid,locationid,gameid,jump_status from ad where id={id};"
sel_sql = f"""select
a.id,
a.name,
a.begin_time,
a.end_time,
a.ad_num,
a.ad_title,
a.ad_body,
a.ad_image,
a.jump_param,
a.ad_sort,
a.status,
a.companyid,
a.locationid,
a.gameid,
a.channelid,
a.jump_status,
a.ad_property,
b.area,
b.type,
b.mode
from
ad a,
location b
where
a.locationid=b.id
and a.in_used=1
and a.id={id}; """
data = self.mydb.query(sel_sql)
log.info(f"sql={sel_sql},data={data}")
if data:
@ -74,8 +179,9 @@ class Ad(Resource):
ad_info = {}
ad_info['id'], ad_info['name'], ad_info['begin_time'], ad_info['end_time'], ad_info['ad_num'], \
ad_info['ad_title'], ad_info['ad_body'], ad_info['ad_image'], ad_info['jump_param'], ad_info[
'ad_sort'], ad_info['status'], ad_info['companyid'], ad_info['locationid'], ad_info[
'gameid'],ad_info['jump_status'] = line
'ad_sort'], ad_info['status'], ad_info['companyid'], ad_info['locationid'], ad_info['gameid'], \
ad_info['channelid'], ad_info['jump_status'],ad_info['ad_property'], ad_info['area'], ad_info['type'], ad_info[
'mode'] = line
all.append(ad_info)
return jsonify({'code': 200, 'message': all})
except Exception:
@ -90,6 +196,7 @@ class Ad(Resource):
ad['id'] = id
ad['name'] = self.args['name']
ad['gameid'] = self.args['gameid']
ad['channelid'] = self.args['channelid']
ad['locationid'] = self.args['locationid']
ad['begin_time'] = self.args['begin_time'] or '1999-01-01'
ad['end_time'] = self.args['end_time'] or '3000-01-01'
@ -101,6 +208,7 @@ class Ad(Resource):
ad['jump_status'] = self.args['jump_status']
ad['ad_sort'] = self.args['ad_sort'] or 0
ad['companyid'] = self.args['companyid']
ad['ad_property'] = self.args['ad_property']
# 检查必需有的字段
if not (
ad['name'] and ad['gameid'] and ad['locationid'] and ad['ad_image'] and ad['jump_status'] and ad['companyid']):
@ -140,6 +248,7 @@ class Ad(Resource):
ad['id'] = self.args['id']
ad['name'] = self.args['name']
ad['gameid'] = self.args['gameid']
ad['channelid'] = self.args['channelid']
ad['locationid'] = self.args['locationid']
ad['begin_time'] = self.args['begin_time'] or '1999-01-01'
ad['end_time'] = self.args['end_time'] or '3000-01-01'
@ -152,6 +261,8 @@ class Ad(Resource):
ad['status'] = self.args['status'] or 0
ad['companyid'] = self.args['companyid']
ad['jump_status'] = self.args['jump_status']
ad['ad_property'] = self.args['ad_property']
sel_sql = f"select name from ad where id={self.args['id']};"
data = self.mydb.query(sel_sql)
@ -170,7 +281,8 @@ class Ad(Resource):
id = self.args['id'] or None
if not id:
return jsonify({'code': 404, 'message': f'{id} not found!'})
del_sql = f"delete from ad where id={id};"
#del_sql = f"delete from ad where id={id};"
del_sql = f"update ad set status=4 where id={id};"
try:
self.mydb.change(del_sql)
return jsonify({'code': 200, 'message': f'remove adid={id} success!'})

View File

@ -65,8 +65,8 @@ class Company(Resource):
def delete(self):
id = self.args['id']
try:
del_sql = f'delete from company where id={id};'
self.mydb.query(del_sql)
del_sql = f'update ad set in_used=0 where id={id};'
self.mydb.change(del_sql)
except Exception:
log.error(f"remove {id} failed!", exc_info=True)
return jsonify({'code': 500})

View File

@ -15,8 +15,10 @@ parser = reqparse.RequestParser()
parser.add_argument('id')
parser.add_argument('area')
parser.add_argument('type')
parser.add_argument('mode')
parser.add_argument('in_used')
parser.add_argument('gameid')
parser.add_argument('channelid')
class Location(Resource):
def __init__(self):
@ -25,7 +27,9 @@ class Location(Resource):
def get(self):
sql = f"select id,area,type from location where in_used=1;"
gameid = self.args['gameid']
channelid = self.args['channelid'] or 6001
sql = f"select gameid,channelid,id,area,type,mode from location where gameid={gameid} and channelid={channelid} and in_used=1;"
try:
data = self.mydb.query(sql)
except Exception:
@ -37,22 +41,33 @@ class Location(Resource):
for line in data:
if line:
localtion = {}
localtion['id'], localtion['area'], localtion['type'] = line
localtion['gameid'], localtion['channelid'], localtion['id'], localtion['area'], localtion['type'], \
localtion['mode'] = line
all_data.append(localtion)
del localtion
return jsonify({'code': 200, 'message': all_data})
else:
log.error(f"{data} not found in mysql !")
return jsonify({'code': 404})
log.error(f"{sql} not found in mysql !")
return jsonify({'code': 200, 'message': ""})
def post(self):
"""
More Actions名称类型必填说明areastring是区域(第一元素1=首页2=游戏中3=结算页4=关闭第二元素X轴第三元素Y轴其中X,Y取值范围0,15)1,0,0代表首页左上typeint是类型1=icon2=banner3=浮窗modeint是广告单播或轮播模式1=单播2=轮播gameidint是游戏IDchannelidint否渠道ID默认是6001
:return:
"""
try:
location = {}
location['area'] = self.args['area']
location['type'] = self.args['type']
location['mode'] = self.args['mode']
location['gameid'] = self.args['gameid']
location['channelid'] = self.args['channelid'] or 6001
location['id'] = self.create_id()
self.mydb.insert("location", location)
if location['area'] and location['type'] and location['mode'] and location['gameid'] :
self.mydb.insert("location", location)
else:
return jsonify({'code': 500,"message":f"get args was area={location['area']},type={self.args['type']},mode={self.args['mode']},gameid={self.args['gameid']}"})
except Exception:
log.error("set values to location mysql failed!", exc_info=True)
return jsonify({'code': 500})
@ -78,7 +93,10 @@ class Location(Resource):
location['id'] = self.args['id']
location['area'] = self.args['area']
location['type'] = self.args['type']
location['in_used'] = self.args['in_used'] or 0
location['mode'] = self.args['mode']
location['gameid'] = self.args['gameid']
location['channelid'] = self.args['channelid']
location['in_used'] = self.args['in_used'] or 1
sel_sql = f"select area from location where id={self.args['id']};"
data = self.mydb.query(sel_sql)
if data:
@ -97,8 +115,8 @@ class Location(Resource):
if not id:
return jsonify({'code': 500, 'message': 'id not found'})
try:
del_sql = f'delete from location where id={id};'
self.mydb.query(del_sql)
del_sql = f'update location set in_used=0 where id={id};'
self.mydb.change(del_sql)
except Exception:
log.error(f"location remove {id} failed!", exc_info=True)
return jsonify({'code': 500})

Binary file not shown.

View File

@ -21,8 +21,9 @@ api = Api(app)
api.add_resource(Company, '/interface/company')
api.add_resource(Location, '/interface/location')
api.add_resource(Ad, '/interface/ad')
api.add_resource(Ad, '/interface/promotion')
api.add_resource(Cos_sdk, '/interface/cos_upload')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=5015)