# -*- 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 import myredis 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 = myredis.hmgetall(key) info = [] for id in ids: temp = myredis.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}