def validate_parameter(self, st, et, t, cids, n, s): """ check param ok or not :param d: date string like 20180101 :param t: time string int(time.time()), len is 10 :param n:nonce not empty :param s:sign string not empty :return: """ if not n or not s: return False if not SUtils.is_time_str(t): return False if st: if not SUtils.is_date_str(st): return False if et: if not SUtils.is_date_str(et): return False if cids: try: tl = eval(cids) if not isinstance(tl, list): return False except Exception as e: return False return True
def validate_parameter(self, st, et, t, cids, n, s): """ :param st: start date :param et: end date :param t: time len is 10 :param cids: [123,12] list string :param n: nonce :param s: signed string :return: True or False """ if not n or not s: return False if not SUtils.is_time_str(t): return False if st: if not SUtils.is_date_str(st): return False if et: if not SUtils.is_date_str(et): return False if cids: try: tl = eval(cids) if not isinstance(tl, list): return False except Exception as e: return False return True
def get(self): """ http get handler """ try: SUtils.init() return_val = { 'data': {}, 'status': ConstantVar.st_other, 'msg': '', } logger.info("\n\nget %s", self.request.body) param_date = self.get_argument("date", '') param_time = self.get_argument("t", '') param_nonce = self.get_argument("n", '') param_sign = self.get_argument('s', '') be_ok = self.validate_parameter(param_date, param_time, param_nonce, param_sign) if not be_ok: return_val['msg'] = ConstantVar.status_msg[ ConstantVar.st_param_error] return_val['status'] = ConstantVar.st_param_error result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str) return try: be, desc = SUtils.check_sign(param_time, param_nonce, param_sign) if not be: return_val['msg'] = ConstantVar.status_msg[ ConstantVar.st_access_deny] return_val['status'] = ConstantVar.st_access_deny result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str) return except Exception as e: logger.error(e) return_val['msg'] = ConstantVar.status_msg[ ConstantVar.st_other] return_val['status'] = ConstantVar.st_other result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str) return result = self.deal(param_date) return_val['data'] = result return_val['status'] = ConstantVar.st_success result_str = ujson.encode(return_val, ensure_ascii=False) # logger.info("result_str:%s", result_str) self.write(result_str) except Exception, e: logger.error(e) return_val['msg'] = ConstantVar.status_msg[ConstantVar.st_other] return_val['status'] = ConstantVar.st_other result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str)
def validate_parameter(self, d, t, n, s): ''' :return: check input paramter ok or not d:date ,string t: time ,used to calculate signed string m: nonce not '' s; sign string not '' ''' if not s or not n: return False if SUtils.is_date_str(d) and SUtils.is_time_str(t): return True else: return False
def validate_parameter(self, d, t, n, s): """ check param ok or not :param d: date string like 20180101 :param t: time string int(time.time()), len is 10 :param n:nonce not empty :param s:sign string not empty :return: """ if not n or not s: return False if SUtils.is_time_str(t) and SUtils.is_date_str(d): return True else: return False
def post(self): """ : http post handler: """ try: SUtils.init() return_val = { 'data': {}, 'status': ConstantVar.st_other, 'msg': '', } logger.info("\n\npost: %s", self.request.body) param_time = self.get_argument('t', '') param_nonce = self.get_argument('n', '') param_sign = self.get_argument('s', '') para_start_date = self.get_argument('start_date', '') para_end_date = self.get_argument('end_date', '') cids_list = self.get_argument('cids', '') crawler_channel = self.get_argument('crawler_channel', '') telecom = self.get_argument('telecom', '') province = self.get_argument('province', '') be_ok = self.validate_parameter(para_start_date, para_end_date, param_time, cids_list, param_nonce, param_sign) if not be_ok: return_val['msg'] = ConstantVar.status_msg[ ConstantVar.st_param_error] return_val['status'] = ConstantVar.st_param_error result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str) return try: be, desc = SUtils.check_sign(param_time, param_nonce, param_sign) if not be: return_val['msg'] = ConstantVar.status_msg[ ConstantVar.st_access_deny] return_val['status'] = ConstantVar.st_access_deny result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str) return except Exception as e: logger.error(e) return_val['status'] = ConstantVar.st_other return_val['msg'] = ConstantVar.status_msg[ ConstantVar.st_other] result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str) return if cids_list: cids_list = eval(cids_list) # deal result = self.deal(para_start_date, para_end_date, cids_list, telecom, province, crawler_channel) # json obj 2 str return_val['data'] = result return_val['status'] = ConstantVar.st_success result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str) except Exception as e: logger.error(e) return_val['status'] = ConstantVar.st_other return_val['msg'] = ConstantVar.status_msg[ConstantVar.st_other] result_str = ujson.encode(return_val, ensure_ascii=False) self.write(result_str)
def get_tel_num(self, mongo_handle, para_date): """ :param param_date: :param mongo_handle: :return form sid_info summary tel_num (unique) """ logger.info(para_date) ori_sid_info = mongo_handle[ConstantVar.mongo_db_name][ ConstantVar.mongo_db_ori_col] ss, es = SUtils.fill_day_with_time(para_date) logger.info("%s,%s", ss, es) ori_telnums_data = ori_sid_info.find( {"end_time": { '$gte': ss, '$lte': es }}, { 'cid': 1, 'tel': 1 }) # get realtime data from sid_info cid_tel_data = {} for k in ori_telnums_data: cid = k.get('cid', ConstantVar.cid_default_value) if cid not in cid_tel_data: cid_tel_data[cid] = set() tel = k.get('tel', ConstantVar.other_default_value) if isinstance(tel, dict) or isinstance(tel, list): # tel maybe a dict or list whick can not be add into a set logger.info("tel :%s is not a str", tel) continue cid_tel_data[cid].add(tel) # get total month before today should_deal, dd = self.get_days_before_param(para_date) logger.info('%s, %s', should_deal, dd) # first day , return if not should_deal: return cid_tel_data first, yestoday = dd ss, es = SUtils.fill_day_with_time(first, yestoday) ori_telnums_data_dd = ori_sid_info.find( {"end_time": { '$gte': ss, '$lte': es }}, { 'cid': 1, 'tel': 1 }) cid_tel_data_dd = {} for k in ori_telnums_data_dd: cid = k.get('cid', ConstantVar.cid_default_value) if cid not in cid_tel_data_dd: cid_tel_data_dd[cid] = set() tel = k.get('tel', ConstantVar.other_default_value) if isinstance(tel, dict) or isinstance(tel, list): # tel maybe a dict or list whick can not be add into a set logger.info("tel :%s is not a str", tel) continue cid_tel_data_dd[cid].add(tel) for k in cid_tel_data: para_date_tel = cid_tel_data[k] before_date_tel = cid_tel_data_dd.get(k, set()) cid_tel_data[k] = para_date_tel - before_date_tel return cid_tel_data