# -*- coding: utf-8 -*- # 推广系统对外接口,提供与客户端之间的广告信息接口及每分钟执行一次的缓存变更操作 # http://ad.kingsome.cn/webapp/index.php?c=Ops&a=getAdList&body={"gameid":1004,"locationid":1001} # http://ad.kingsome.cn/webapp/index.php?c=Ops&a=upAdRecording&adid=1002 import tornado.ioloop import tornado.web import json from myredis.myredis import my_redis import datetime from mysql.mmysql import MysqlBase from prod_config import mysql_promotion_config from log.mylog import define_logger import logging from prod_config import BEGIN, END, ad_list_interface_port from tornado import gen import pdb from urllib.parse import unquote from ops.mtga import FromTga, GetTgaConfig define_logger("/data/logs/ad_interface_tornado.log") log = logging.getLogger(__name__) limit = 100 class DispatchHandler(tornado.web.RequestHandler): @gen.coroutine def get(self): if self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'selfChecking': self._selfCheckingHandler() elif self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'getAdList': yield self._selfGetAdList() elif self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'upAdRecording': yield self._upAdRecording() elif self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'getLocation': yield self._selfGetLocation() else: self.write("pls check args!") @gen.coroutine def post(self): if self.get_query_argument('c') == 'Ops' and self.get_query_argument('a') == 'upJumpRecording': self._selfupJumpRecording() def _selfupJumpRecording(self): """| gameid | int | 是 | 游戏ID | | locationid | int | 是 | 广告位置ID | | uid | int | 是 | 用户ID或微信openid | | ad_channel | int | 是 | 广告特征码 见上ad_property.jump_param | | adid | int | 是 | 广告ID | """ try: post_data = self.request.body_arguments post_data = {x: post_data.get(x)[0].decode("utf-8") for x in post_data.keys()} if not post_data: post_data = self.request.body.decode('utf-8') post_data = json.loads(post_data) print(post_data) pdb.set_trace() gameid = self.get_query_argument('gameid') locationid = self.get_query_argument('locationid') uid = self.get_query_argument('uid') ad_channel = self.get_query_argument('ad_channel') adid = self.get_query_argument('adid') print(f"gameid={gameid},locationid={locationid},uid={uid},ad_channel={ad_channel},aid={aid}") except Exception as e: result = {'errcode': 2, "errmsg": f"get args failed,{str(e)}"} log.error(result) return self.write({'errcode': 1, "errmsg": 'get args failed!'}) g = GetTgaConfig() item = g.get_api_key(gameid) if not (gameid and locationid and uid and ad_channel and aid): log.error(f"") return self.write({'errcode': 1, "errmsg": 'get args failed!'}) if not item: result = {'errcode': 2, "errmsg": f"get config via gameid failed ,gameid={gameid}"} log.error(result) return self.write({'errcode': 1, "errmsg": f'{gameid} not in define!'}) 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 = "jump_recording" try: tga_data = {} tga_data['ad_channel'] = ad_channel tga_data['account_id'] = uid tga_data['gameid'] = gameid tga_data['date'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") tga_data['locationid'] = locationid tga_data['adid'] = adid self.tga.put_event_data(tga_data, f'rep_{self.event_type}') return self.write({'errcode': 0, "errmsg": '', "message": ""}) except Exception: log.error(f"write 2 tga failed!", exc_info=True) return ({'errcode': 2, "errmsg": f'write failed!'}) def _selfGetLocation(self): try: input = json.loads(self.get_query_argument('body')) gameid = input['gameid'] channelid = input.get('channelid', None) or 6001 except Exception as e: result = {'errcode': 2, "errmsg": f"get args failed,{str(e)}"} log.error(result) return self.write_error(2) mydb = MysqlBase(**mysql_promotion_config) sql = f"select gameid,channelid,id,area,type,mode from location where gameid={gameid} and channelid={channelid} and in_used=1;" try: data = mydb.query(sql) except Exception: log.error("get data from location failed!", exc_info=True) return self.write({'errcode': 2, "errmsg": f"get mysql data failed!"}) all_data = [] log.debug(f"get data from localtion was {data}") if data: for line in data: if line: localtion = {} localtion['gameid'], localtion['channelid'], localtion['id'], localtion['area'], localtion['type'], \ localtion['mode'] = line all_data.append(localtion) del localtion return self.write({'errcode': 0, "errmsg": '', "message": all_data}) def _upAdRecording(self): try: adid = unquote(self.get_query_argument('adid'), 'utf-8') localtionid = self.get_query_argument('locationid') log.info(f" get adid was {adid}") ids = json.loads(adid) except Exception: result = {'errcode': 2, "errmsg": f"get args failed`"} log.error(result, exc_info=True) self.write({'errcode': 1, "errmsg": 'get adid failed!'}) if not localtionid: self.write({'errcode': 1, "errmsg": 'get localtionid failed!'}) if localtionid: if ids: for id in ids: if id: key = f"adnum::{id}_{localtionid}::num" my_redis.incr(key, amount=1) self.write({'errcode': 0, "errmsg": '', "message": f"{ids} incr success!"}) else: self.write({'errcode': 1, "errmsg": 'get adid failed!'}) else: self.write({'errcode': 1, "errmsg": 'get localtionid failed!'}) def _selfCheckingHandler(self): self.write(json.dumps({ 'errcode': 0, 'errmsg': '', 'healthy': 1, 'max_rundelay': 10 }, separators=(',', ':'))) def _selfGetAdList(self): try: input = json.loads(self.get_query_argument('body')) gameid = input['gameid'] channelid=input.get('channelid',None) or 6001 area=input.get('area',0) locationid = input.get('locationid',0) except Exception as e: result = {'errcode': 2, "errmsg": f"get args failed,{str(e)}"} log.error(result) return self.write_error(2) if gameid and locationid: key_word = f"ad::{gameid}_*::{channelid}::*::{locationid}" elif gameid and area: key_word = f"ad::{gameid}_*::{channelid}::{area}::*" else: pass ad_keys = my_redis.keys(key_word) ids = [] for ad_key in ad_keys: adlists = my_redis.smembers(ad_key) try: for key in adlists: ids.append(key) except Exception: log.error(f"get redis data failed!", exc_info=True) return self.write({'errcode': 2, "errmsg": f"get redis data failed!"}) dist_ids = list(set(ids)) info = [] if not dist_ids: result = {'errcode': 0, "errmsg": '', "message": {"totoal": len(info), "result": info}} else: try: # 如果取得的记录条数大于预设,扔掉多余的记录,当前采用的是随机选择,以后可能需要添加加权选择 id_list = [] if limit < len(dist_ids): nums = limit else: nums = len(dist_ids) for i in range(nums): while 1: # new = my_redis.srandmember(key) import random new = random.choice(dist_ids) if new not in id_list: id_list.append(new) break for id in id_list: temp = my_redis.hgetall(f"adinfo::{id}::info") if temp: info.append(temp) result = {'errcode': 0, "errmsg": '', "message": {"totoal": len(info), "result": info}} except Exception as e: result = {'errcode': 1, "errmsg": e} return self.write(result) def make_app(): return tornado.web.Application([(r"/webapp/index[\.]php", DispatchHandler)]) if __name__ == "__main__": print('start!') app = make_app() app.listen(ad_list_interface_port) tornado.ioloop.IOLoop.current().start()