def as_call(self, request, context): ''' message ASRequest{ string log_id = 1; string user_id = 2; user_info.UserInfo user_info = 3; } message ASResponse { message Error { uint32 code = 1; string text = 2; } Error error = 1; repeated item_info.ItemInfo item_infos = 2; } message ItemInfo { string movie_id = 1; string title = 2; string genre = 3; } ''' recall_req = recall_pb2.RecallRequest() if request.user_id != "-1": user_id = request.user_id um_res = get_ums(user_id) recall_req.user_info.CopyFrom(um_res.user_info) else: recall_req.user_info.CopyFrom(request.user_info) recall_res = get_recall(recall_req) nid_list = [x.nid for x in recall_res.score_pairs] cm_res = get_cm(nid_list) item_dict = {} for x in cm_res.item_infos: item_dict[x.movie_id] = x rank_req = rank_pb2.RankRequest() rank_req.user_info.CopyFrom(um_res.user_info) rank_req.item_infos.extend(cm_res.item_infos) rank_res = get_rank(rank_req) as_res = as_pb2.ASResponse() as_res.error.code = 200 for sp in rank_res.score_pairs: nid = sp.nid item_info = item_dict[nid] as_res.item_infos.extend([item_info]) return as_res
else: gender = sys.argv[2] age = int(sys.argv[3]) job = sys.argv[4] req.user_info.user_id, req.user_info.gender, req.user_info.age, req.user_info.job = "0", gender, age, job print(get_as(req)) if sys.argv[1] == 'um': uid = sys.argv[2] print(get_ums(uid)) if sys.argv[1] == 'cm': nid_list_str = sys.argv[2] nid_list = nid_list_str.strip().split(",") print(get_cm(nid_list)) if sys.argv[1] == "recall": uid = sys.argv[2] user_info = get_ums(uid).user_info request = recall_pb2.RecallRequest() request.user_info.CopyFrom(user_info) print(get_recall(request)) if sys.argv[1] == "rank": request = rank_pb2.RankRequest() request.user_info.user_id = "1" request.user_info.age = 3 request.user_info.job = "1" request.user_info.gender = "M" item_info = request.item_infos.add() item_info.movie_id = "1" item_info.title = "ET" item_info.genre = "fiction" print(get_rank(request))