def get_doctor_features(did, log=False): """ 获取医生特征, [年龄, 性别, 职称, 图文价, 医院省份, 医院等级, 科室号] :param did: 医生ID :param log: 日志 :return: 医生特征 """ if log: print "提取医生特征中... did: %s" % did s_time = time.time() try: params = {'doctor_ids': [did], 'fields': ['sex', 'born_date', 'hospital_province', 'hospital_grade', 'title', 'pro_price', 'clinic_no', 'second_class_clinic_no']} rpc_time = time.time() # 统计RPC时间 client = get_user_profile_service() result = client.getDoctorProfileInfo(json.dumps(params)) did_info = json.loads(result)[did] [age, sex, level_title, pro_price, hospital_province, hospital_grade, clinic_no] = \ get_doctor_info_from_rpc_info(did_info) if log: print u'\tRPC time: %s, Result: %s' % (timedelta(seconds=(time.time() - rpc_time)), result) print u'\t年龄: %s, 性别: %s, 职称: %s, 图文价: %s, 医院省份: %s, 医院等级: %s, 科室号: %s' % \ (age, sex, level_title, pro_price, hospital_province, hospital_grade, clinic_no) except Exception as e: print u"\t医生特征 %s :异常 %s" % (did, e) return None if log: print "提取医生特征完成... time: %s" % timedelta(seconds=time.time() - s_time) return [age, sex, level_title, pro_price, hospital_province, hospital_grade, clinic_no]
def get_problem_info(problem_ids): problem_ids = [unicode(x) for x in problem_ids] params = { 'problem_ids': problem_ids, 'fields': [ 'created_time', 'doctor_checkup', 'user_checkup', 'user_first_ask_checkup', 'doctor_drug', 'user_drug', 'user_first_ask_drug', 'doctor_confirm_disease', 'interaction_num' ] } client = get_user_profile_service() result = json.loads(client.getProblemProfileInfo(json.dumps(params))) for pid in problem_ids: p_info = result.get(pid, None) print p_info
def get_users_province(uids): """ 获取用户的省份字典 :param uids: 用户ID列表 :return: 用户的ID字典 """ uids = [unicode(x) for x in uids] params = {'user_ids': uids, 'fields': ['province']} client = get_user_profile_service() result = json.loads(client.getUserProfileInfo(json.dumps(params))) uid_province_dict = dict() for uid in uids: p_info = result.get(uid, None) if p_info: province = p_info.get('province', None) uid_province_dict[uid] = unicode(province) else: uid_province_dict[uid] = None return uid_province_dict
def get_doctors_province(dids): """ 获取医生所在医院的省份和城市 :param dids: 医生列表 :return: 医生地址字典 """ dids = [unicode(x) for x in dids] params = {'doctor_ids': dids, 'fields': ['hospital_province', 'hospital_city']} client = get_user_profile_service() result = json.loads(client.getDoctorProfileInfo(json.dumps(params))) did_province_dict = dict() for did in dids: p_info = result.get(did, None) if p_info: province = p_info.get('hospital_province', None) city = p_info.get('hospital_city', None) did_province_dict[did] = (unicode(province), unicode(city)) else: did_province_dict[did] = None return did_province_dict
def get_user_paid_avg(uid, log=False): """ 获取用户特征, [年龄, 性别, 省份, 平台, 总额, 总单数, 最大额, 平均额] :param uid: 用户ID :param log: 是否输出日志 :return: 用户特征 """ if log: print "提取用户特征中... uid: %s" % uid s_time = time.time() try: params = { 'user_ids': [uid], 'fields': ['graph_paid_num', 'paid_details'] } rpc_time = time.time() # 统计RPC时间 client = get_user_profile_service() result = client.getUserProfileInfo(json.dumps(params)) uid_info = json.loads(result)[uid] graph_paid_num = uid_info.get('graph_paid_num', 0) paid_details = uid_info.get('paid_details', None) paid_info = get_user_order_from_rpc_info(paid_details) # 获取订单信息 paid_sum = paid_info[0] paid_num = paid_info[1] paid_max = paid_info[2] paid_avg = paid_info[4] if log: print '\tRPC time: %s, Result: %s' % (timedelta( seconds=(time.time() - rpc_time)), result) print '\t付费单数: %s, 总额: %s, 总单数: %s, 最大额: %s, 平均额: %s' % \ (graph_paid_num, paid_sum, paid_num, paid_max, paid_avg) except Exception as e: print "\t用户特征 %s :异常 %s" % (uid, e) return None if log: print "提取用户特征完成... time: %s" % timedelta(seconds=time.time() - s_time) # return [graph_paid_num, paid_sum, paid_num, paid_max, paid_avg] return [graph_paid_num, safe_div(float(paid_sum), float(graph_paid_num))]
def get_user_intention(uid): """ 获取用户的付费意愿, 0其他, 1低付费, 2高付费 :param uid: :return: 付费意愿 """ try: uid = unicode(uid) params = {'user_ids': [uid], 'fields': ['tag_details']} client = get_user_profile_service() result = client.getUserProfileInfo(json.dumps(params)) tag_details = json.loads(result)[uid].get(u'tag_details', None) if tag_details: for tag_detail in tag_details: name = tag_detail.get(u'name', None) if name == 'pay_ability': value = json.loads(tag_detail.get(u'value')).get(u'value') return value return 0 except Exception as e: print "\tException: get_user_intention(%s)" % uid return 0
def get_user_features(uid, log=False): """ 获取用户特征, [年龄, 性别, 省份, 平台, 总额, 总单数, 最大额, 平均额] :param uid: 用户ID :param log: 是否输出日志 :return: 用户特征 """ if log: print "提取用户特征中... uid: %s" % uid s_time = time.time() try: params = {'user_ids': [uid], 'fields': ['sex', 'born_date', 'province', 'gold_coin', 'weight', 'height', 'ehr_num', 'join_date', 'platform', 'paid_details', 'graph_total_num', 'graph_free_num', 'graph_paid_num', 'graph_refund_num', 'graph_rebuy_num', 'graph_revisit_num', 'graph_assess_rating_num', 'graph_assess_rating_ratio', 'graph_assess_good_rating_num', 'graph_assess_good_rating_ratio', 'reward_total_num', 'reward_total_pay', 'tag_details']} rpc_time = time.time() # 统计RPC时间 client = get_user_profile_service() result = client.getUserProfileInfo(json.dumps(params)) uid_info = json.loads(result)[uid] user_info_list = get_user_info_from_rpc_info(uid_info) # 获取个人信息 user_info_list = [None if v is u'' else v for v in user_info_list] if log: print u'\t年龄: %s, 性别: %s, 省份: %s, 金币数: %s, 体重: %s, ' \ u'身高: %s, 档案数: %s, 加入时间: %s, 用户平台: %s, 图文总数: %s, ' \ u'免费图文数: %s, 付费图文数: %s, 图文退款数: %s, 图文重复购买数: %s, 图文重复访问数: %s, ' \ u'图文评价数: %s, 图文评价率: %s, 图文好评数: %s, 图片好评率: %s, 打赏总数: %s, ' \ u'打赏总金额: %s' % tuple(user_info_list) paid_details = uid_info.get('paid_details', None) user_order_list = get_user_order_from_rpc_info(paid_details) # 获取订单信息 if log: print u'\t订单额: %s, 订单数: %s, 订单最大值: %s, 订单最小值: %s, 订单均值: %s, ' \ u'图文订单额: %s, 图文订单数: %s, 图文订单最大值: %s, 图文订单最小值: %s, 图文订单均值: %s' \ % tuple(user_order_list) tag_details = uid_info.get('tag_details', None) user_tag_list = get_user_tags_from_rpc_info(tag_details) fi_user_tag_list = list() for index in range(8): if index < len(user_tag_list): item = user_tag_list[index] else: item = None fi_user_tag_list.append(item) if log: print u"\t标签列表: %s" % fi_user_tag_list if log: print u'\tRPC time: %s, Result: %s' % (timedelta(seconds=(time.time() - rpc_time)), result) except Exception as e: print u"\t用户特征 %s :异常 %s" % (uid, e) return None if log: print u"提取用户特征完成... time: %s" % timedelta(seconds=time.time() - s_time) return user_info_list + user_order_list + fi_user_tag_list