# -*- coding: utf-8 -*- # http://mp.kingsome.cn/api/open/ad-uid?gameid=8002&channelid=6001 from ops.mtga import FromTga, GetTgaConfig from ops.plog import define_logger import logging import datetime from ops.mmysql import MysqlBase import random import requests import pdb define_logger("/data/logs/get_news.log") log = logging.getLogger(__name__) TimeDelay = 60*60 # DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'games_report'} DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'ad'} class InitFirstAD(): def __init__(self, kwargs): self.now = kwargs.get('time') # self.begin = (datetime.datetime.strptime(self.end, '%Y-%m-%d %H:%M:%S') - datetime.timedelta( # minutes=TimeDelay)).strftime('%Y-%m-%d %H:%M:%S') self.gameid = kwargs.get('gameid') self.channelid = kwargs.get('channelid') self.ratio = self.get_ratio() g = GetTgaConfig() item = g.get_api_key(self.gameid) self.url = item['url'] self.tgaid = item['tgaid'] self.suffix = item.get('suffix', 0) self.api_key = item.get('api_key', None) self.tga = FromTga(self.url, self.api_key) self.mysql = MysqlBase(**DB) # self.hide = 0.8 # self.tga.init_tga_write(self.tgaid) def get_ratio(self): # url="https://mp-test.kingsome.cn/api/open/ad-uid?gameid=8002&channelid=6001" ratios = dict() url = f"https://mp.kingsome.cn/api/open/ad-uid?gameid={self.gameid}&channelid={self.channelid}" r = requests.get(url) if r.status_code == 200: data = r.json().get('result', []) else: log.error(f"get {url} from mp was {r.status_code}", exc_info=True) data = None if data: for line in data: try: ratios[line['adChannel']] = line['ratio'] except Exception: log.error(f"{line} split 'adChannel','ratio' failed! ", exc_info=True) return ratios # def get_ad_channel(self): # sql = f"""SELECT # DISTINCT first_ad_channel # FROM # v_user_{self.suffix} # where # gameid='{self.gameid}'""" # temp = [] # data = self.tga.get_data(sql) # if data: # for line in data: # try: # temp.append(line[0]) # except Exception: # log.error(f"split data {line} failed", exc_info=True) # return temp def run(self): if self.ratio: for ad_channel in self.ratio.keys(): ratio = self.ratio.get(ad_channel, 100) begin = self.get_last_times(ad_channel) users = self.get_new_user(begin,ad_channel) if users: mark_users = self.hide_user(users, ratio) table_name = "newuser" if mark_users: log.info(f"gameid={self.gameid},channel={self.channelid},ad_channel={ad_channel},ratio={ratio},begin={begin},get users {len(users)},write {len(mark_users)} to mysql!") for line in mark_users: try: temp = {} temp['gameid'] = self.gameid temp['channelid'] = self.channelid temp['accountid'], temp['register_time'], temp['ad_channel'] = line self.mysql.insert(table_name, temp) except Exception: log.error(f"insert {line} to mysql Failed !", exc_info=True) def get_last_times(self, ad_channel): sql = f"""SELECT register_time FROM newuser where ad_channel='{ad_channel}' AND gameid={self.gameid} AND channelid={self.channelid} ORDER BY register_time DESC LIMIT 1;""" data = self.mysql.query(sql) try: last_time = data[0][0] except Exception: last_time = "1999-09-09 00:00:00" return last_time def hide_user(self, data, ratio): if data: line = len(data) nums = int(line * ratio / 100) if nums: return random.sample(data, nums) else: return None def get_new_user(self, begin,ad_channel): rdata = [] sql = f"""SELECT "#account_id", account_register_time, first_ad_channel FROM v_user_{self.suffix} where gameid='{self.gameid}' AND first_ad_channel='{ad_channel}' AND "account_register_time" > timestamp'{begin}' AND "account_register_time"