# -*- 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 import json import pdb import sys define_logger("/data/logs/jc_show_split.log") log = logging.getLogger(__name__) # DB = {'user': 'mytga', 'pswd': 'gzVwh4HGR68G', 'host': '10.10.3.5', 'db': 'games_report'} class JcShowInit: def __init__(self, kwargs): self.time = kwargs.get('time') self.gameid = kwargs.get('gameid') self.channelid = kwargs.get('channelid') g = GetTgaConfig() item = g.get_api_key(self.gameid) self.url = item['url'] # self.tgaid = 99 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.tga.init_tga_write(self.tgaid) self.event_type = "jc_show" def get_data(self): sql = f"""SELECT "#account_id", "#server_time", button_param FROM v_event_{self.suffix} where gameid='{self.gameid}' and channel='{self.channelid}' and "$part_event"='event_11_31' and "button_name" ='jc_show' and "$part_date"='{self.time}' """ data = self.tga.get_data(sql) all = list() if data: for line in data: if line: all.append(line) return all def write_tga_files(self, data): try: tga_data = {} tga_data['ad_channel'] = data['ad_channel'] tga_data['account_id'] = data['account_id'] tga_data['gameid'] = self.gameid tga_data['date'] = self.time tga_data['channelid'] = self.channelid tga_data['location'] = data['location'] tga_data['show_time'] = data['show_time'] self.tga.put_event_data(tga_data, f'rep_{self.event_type}') except Exception: log.error(f"write 2 tga failed!,context={data}", exc_info=True) def split_event19(self, data): """6001_1004_oQmL74ij2oq3SyvO6QriuD0EJSjE {"ad_id":["1039","1035","1037","1036","1038","1040"],"pos":"1,0,5,0,0","ad_channel":["wxdb103a128e118619","wxf4b70c770560c2fe","wx7d84fae7fe8796d7","wx1dd2f605f4f8496b","wx2f6ebc0586e24d61","wx0e14c9f4c4a06870"]}""" for line in data: account, show_time, data_all = line data_dict = json.loads(data_all) temp = {} temp['account_id'] = account temp['location'] = data_dict['pos'] temp['show_time'] = show_time print(f"{temp}") if data_dict['ad_channel']: for ad_channel in data_dict['ad_channel']: temp['ad_channel'] = ad_channel self.write_tga_files(temp) del temp def split_event22(self, data): """{"pos":3,"channelid":6001,"ad_channel":["9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","9b4c1yxtv5","yx2nyabgm8","yx2nyabgm8","yx2nyabgm8","yx2nyabgm8","yx2nyabgm8","yx2nyabgm8","9f97o1xz44","9f97o1xz44","9f97o1xz44","9f97o1xz44","s4fxu9w1yb","s4fxu9w1yb","s4fxu9w1yb","s4fxu9w1yb","49nnealk3i","49nnealk3i"]}""" for line in data: account, show_time, data_all = line data_dict = json.loads(data_all) # print(f"account={account}\t data={data_dict}") temp = {} temp['account_id'] = account temp['location'] = data_dict['pos'] temp['show_time'] = show_time if data_dict['ad_channel']: for ad_channel in data_dict['ad_channel']: temp['ad_channel'] = ad_channel self.write_tga_files(temp) del temp def run(self): data = self.get_data() if data: log.info(f"get data from tga gameid={self.gameid},time={self.time},len={len(data)}") if self.suffix == 19: self.split_event19(data) elif self.suffix == 22: self.split_event22(data) else: log.error(f"{self.gameid} {self.channelid} not found in define!") raise Exception(f"{self.gameid} {self.channelid} not found in define!") def main(): for gameid in (1004, 1011): kwargs = {} if len(sys.argv) == 2: kwargs['time'] = sys.argv[1] else: kwargs['time'] = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d') kwargs['gameid'] = gameid kwargs['channelid'] = 6001 jc = JcShowInit(kwargs) jc.run() if __name__ == "__main__": main()