121 lines
3.9 KiB
Python
121 lines
3.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
import os
|
||
import json
|
||
import time
|
||
import hashlib
|
||
import urllib.request
|
||
import tornado.ioloop
|
||
import tornado.web
|
||
import tornado.ioloop
|
||
import tornado.web
|
||
import json
|
||
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
|
||
|
||
mydb = MysqlBase(**mysql_promotion_config)
|
||
define_logger("/data/logs/make_ad_cache.log")
|
||
log = logging.getLogger(__name__)
|
||
|
||
BEGIN = '1999-01-01'
|
||
END = '3000-01-01'
|
||
|
||
|
||
class Adlist(tornado.web.RequestHandler):
|
||
def post(self):
|
||
res = adlist_workflow(json.loads(self.request.body))
|
||
self.write(json.dumps(res))
|
||
|
||
|
||
def adlist_workflow(input):
|
||
gameid = input['gameid']
|
||
localid = input['localtionid']
|
||
key = f"{gameid}::{localid}"
|
||
ids = my_redis.hmgetall(key)
|
||
info = []
|
||
for id in ids:
|
||
temp = my_redis.hgetall[f"{id}::info"]
|
||
info.append(temp)
|
||
|
||
return info
|
||
|
||
|
||
def getDaySeconds(time_val, incdays):
|
||
time_zone = 8
|
||
return int((time_val + time_zone * 3600) / 3600 / 24 + incdays) * 3600 * 24 - 3600 * time_zone;
|
||
|
||
|
||
def send_cache_data():
|
||
now = datetime.datetime.today().strftime("%Y-%m-%d")
|
||
all = []
|
||
# 添加无天数限定的记录
|
||
get_full_data = f"select id,name,ad_num,ad_title,ad_body,ad_image,ad_url,ad_sort,companyid,locationid,gameid 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:
|
||
item = {}
|
||
try:
|
||
item['id'], item['name'], item['ad_num'], item['ad_title'], item['ad_body'], item['ad_image'], item[
|
||
'ad_url'], item['ad_sort'], item['companyid'], item['locationid'], item['gameid'] = 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,ad_url,ad_sort,companyid,locationid,gameid 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[
|
||
'ad_url'], item['ad_sort'], item['companyid'], item['locationid'], item['gameid'] = line
|
||
all.append(item)
|
||
except Exception:
|
||
log.error("split data failed", exc_info=True)
|
||
# 检查ID是否存在播放列表中,以及播放次数是否完毕
|
||
pdb.set_trace()
|
||
if all:
|
||
for line in all:
|
||
key = f"{line.get('gameid', 0)}::{line.get('locationid', 0)}"
|
||
if my_redis.exists(key):
|
||
# 检查num是否已达到设定数值
|
||
num = my_redis.get(f"{key}:num") or 0
|
||
if line['num'] != -1 and line['num'] <= num:
|
||
# 该广告已播放达到次数,从缓存中清除
|
||
my_redis.srem(key, line['id'])
|
||
# 清理广告详细记录
|
||
my_redis.expire(f"{line['id']}::info", 0)
|
||
else:
|
||
my_redis.sadd(key, line['id'])
|
||
|
||
if not my_redis.exists(f"{line['id']}::info"):
|
||
my_redis.hmset(f"{line['id']}::info", line)
|
||
|
||
|
||
class SelfCheckingHandler(tornado.web.RequestHandler):
|
||
|
||
def get(self):
|
||
self.write(json.dumps({
|
||
'errcode': 0, 'errmsg': '', 'healthy': 1, 'max_rundelay': 10
|
||
}, separators=(',', ':')))
|
||
|
||
|
||
def make_app():
|
||
return tornado.web.Application([(r"/webapp/index[\.]php", SelfCheckingHandler), (r"/", hello),
|
||
|
||
])
|
||
|
||
|
||
if __name__ == "__main__":
|
||
import pdb
|
||
|
||
send_cache_data() # print('start!') # conf = json.loads(open('../config/kefu_robot.json', 'r').read()) # app = make_app() # app.listen(conf['listen_port']) # # tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['sendtime1'], # lambda: sendNotify(conf, conf['sendtime1'])) # tornado.ioloop.IOLoop.current().call_at(getDaySeconds(time.time(), 1) + conf['sendtime2'], # lambda: sendNotify(conf, conf['sendtime2'])) # tornado.ioloop.IOLoop.current().start()
|