From 0ac61402dbe2ec23d77b2b5e03972b7301dbc596 Mon Sep 17 00:00:00 2001 From: pengtao Date: Tue, 10 Sep 2019 15:17:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B9=BF=E5=91=8A=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E5=88=B7=E6=96=B0=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ops/reflush_ad.py | 117 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 108 insertions(+), 9 deletions(-) diff --git a/ops/reflush_ad.py b/ops/reflush_ad.py index 9be4ee0..04dad5d 100644 --- a/ops/reflush_ad.py +++ b/ops/reflush_ad.py @@ -4,7 +4,7 @@ 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__) @@ -12,11 +12,10 @@ log = logging.getLogger(__name__) class Reflush_AdInfo(): def __init__(self): - self.myredis = my_redis self.mysql = MysqlBase(**mysql_promotion_config) def reflush_ad_via_localtionid(self, locationid): - all = self.myredis.keys() + all = my_redis.keys() change_key = [] if all: for one_key in all: @@ -24,20 +23,120 @@ class Reflush_AdInfo(): change_key.append(one_key) else: raise Exception("Redis is null!") - + ad_list = [] for item in change_key: - pass + temp = my_redis.smembers(item) + for one in temp: + ad_list.append(one) + self.reflush_ad(ad_list) - self.reflush_ad(item) + 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 reflush_ad(self, adid): - pass def main(): - pass + cc = Reflush_AdInfo() + cc.reflush_ad_via_localtionid('1016') if __name__ == "__main__":