139 lines
5.4 KiB
Python
139 lines
5.4 KiB
Python
# -*- coding: utf-8 -*-
|
||
from __future__ import absolute_import
|
||
import sys
|
||
from flask import Flask, jsonify
|
||
from flask_restful import reqparse, abort, Api, Resource
|
||
import logging
|
||
from myredis.myredis import my_redis
|
||
from mysql.mmysql import MysqlBase
|
||
from prod_config import mysql_promotion_config
|
||
import datetime
|
||
import pdb
|
||
import json
|
||
import copy
|
||
|
||
log = logging.getLogger(__name__)
|
||
|
||
parser = reqparse.RequestParser()
|
||
parser.add_argument('locationid')
|
||
|
||
|
||
class RelushADinfo(Resource):
|
||
|
||
def __init__(self):
|
||
self.args = parser.parse_args()
|
||
self.mydb = MysqlBase(**mysql_promotion_config)
|
||
self.expired = 60 * 2 * 60
|
||
self.remove_list = ('ad_num', 'gameid')
|
||
|
||
def get(self):
|
||
locationid = self.args['locationid']
|
||
pdb.set_trace()
|
||
if locationid:
|
||
need_change_keys = set()
|
||
key_word = f"ad::*_{locationid}*::*::{locationid}"
|
||
adlists = my_redis.keys(key_word)
|
||
if adlists:
|
||
for key in adlists:
|
||
need_change_keys.update(my_redis.smembers(key))
|
||
|
||
if need_change_keys:
|
||
for one in need_change_keys:
|
||
key = f"adinfo::{one}::info"
|
||
adid = one.split('_')[0]
|
||
locationid = one.split('_')[1]
|
||
areas = self._get_areas(locationid)
|
||
if areas:
|
||
for area in areas:
|
||
info = self._get_adinfo(adid)
|
||
info['area'] = area
|
||
full_info = self._update_localtion(locationid, info)
|
||
for item in self.remove_list:
|
||
full_info.pop(item)
|
||
my_redis.hmset(key, full_info)
|
||
my_redis.expire(key, self.expired)
|
||
return jsonify({'code': 200, 'message': f"reflush key locationid={locationid}!"})
|
||
else:
|
||
return jsonify({'code': 200, 'message': f"key not found with locationid={locationid}!"})
|
||
|
||
|
||
def _get_areas(self, locationid):
|
||
sql = f"SELECT area FROM `location` WHERE id={locationid}"
|
||
data = self.mydb.query(sql)
|
||
if data:
|
||
return data[0].replace("[", "").replace("]", "").replace('"', "").split(',')
|
||
else:
|
||
log.error(f"get area from db failed,location={locationid}!")
|
||
return None
|
||
|
||
def _get_adinfo(self, id):
|
||
all = []
|
||
sql = f"""select
|
||
id,
|
||
name,
|
||
ad_num,
|
||
ad_title,
|
||
ad_body,
|
||
ad_image,
|
||
jump_param,
|
||
ad_sort,
|
||
companyid,
|
||
gameid,
|
||
channelid,
|
||
jump_status,
|
||
locationid ,
|
||
ad_property,
|
||
createtime
|
||
from
|
||
ad
|
||
where
|
||
status=1
|
||
and in_used=1
|
||
and id={id} """
|
||
|
||
|
||
data = self.mydb.query(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['locationid'], item['ad_property'], item['createtime'] = line
|
||
item['createtime'] = datetime.datetime.strftime(item['createtime'], "%Y-%m-%d %H:%M:%S")
|
||
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.append(item)
|
||
except Exception:
|
||
log.error("split data failed", exc_info=True)
|
||
return all
|
||
|
||
|
||
def _update_localtion(self, id, line):
|
||
new = copy.deepcopy(line)
|
||
location_sql = f"select x,y,x_offset,y_offset,type,mode,ld_property from location WHERE in_used=1 and id={id}"
|
||
data = self.mydb.query(location_sql)
|
||
try:
|
||
new['x'], new['y'], new['x_offset'], new['y_offset'], new['type'], new['mode'], new['ld_property'] = data[0]
|
||
new['area'] = f"{new['area']},{new['x']},{new['y']},{new['x_offset']},{new['y_offset']}"
|
||
except Exception:
|
||
log.error(f"get localtion info by sql={location_sql} error!", exc_info=True)
|
||
return None
|
||
return new
|