def get_video_by_users(self, user_id=0, req_user_ids=[], batch_num=30): import time from adjuster.hero_adjuster import HeroAdjuster from dao.common import assemble_adjuster from dao.user_profile import UserProfile self.__base = self.__base or BaseDao() if not req_user_ids: req_user_ids = _get_watch_users(user_id) if not req_user_ids: return None udp_client = self._get_herotime_client() arr = [] data = {} data['videos'] = arr data['total'] = 0 data['user_info'] = [] try: video = Video(self.__base) user = UserProfile(self.__base) uid_tmps = [] userid_tmps = [] uid_time_map = {} uid_user_id_map = {} user_id_uid_map = {} user_id_profile_map = {} for user_item in req_user_ids: #req_user_id is chuanchuan id #uid is chuanchuan openid req_user_id = user_item['req_user_id'] if not req_user_id: logger.warn('req_user_id is null, continue') continue uid = user.get_uid_by_userid(req_user_id) if not uid: logger.error('uid is null, req_user_id:%s' % req_user_id) continue #return None uid_user_id_map[uid] = req_user_id user_id_uid_map[req_user_id] = uid uid_tmps.append(uid) userid_tmps.append(req_user_id) uid_time_map[uid] = (user_item.get('begin_sec') or 0, user_item.get('begin_usec') or 0) if not len(uid_tmps): logger.warn('uid_tmps is null, return None') return None res = self._convert_openid(uid_tmps) openids = [] openid_user_id_map = {} for uid_tmp in uid_tmps: lp_openid = res.get(uid_tmp) if not lp_openid: logger.warn( 'convert uid:%s to lp uid fail, maybe it is qq openid' % uid) continue userid = uid_user_id_map.get(uid_tmp) openid_user_id_map[lp_openid] = userid begin_sec, begin_usec = uid_time_map.get(uid_tmp) openids.append({ 'uid': lp_openid.encode("utf-8"), 'begin_sec': begin_sec, 'begin_usec': begin_usec }) if not len(openids): logger.warn( 'lp openids is null, maybe it is all qq openid:%s' % uid_tmps) return None logger.info('convert openid success %s->%s, start get videos' % (uid_tmps, openids)) adjuster = HeroAdjuster(udp_client) logger.info('[Time]:get_videos_by_batch_users,start:%s' % time.time()) res_obj = adjuster.get_videos_by_batch_users(openids=openids, batch_num=batch_num) logger.info('[Time]:get_videos_by_batch_users,end:%s' % time.time()) obj_tmp = [] for item in res_obj: datas = item.get('datas') req_userid = openid_user_id_map.get(item.get('uid')) for info in datas: #logger.info('debug:info:%s'%info) obj = assemble_adjuster(info) row_id = video.add_video(video_obj=obj) if row_id: video.add_video_game(video_obj=obj) user.update_user_profile(obj=obj) obj.uid = user_id_uid_map[req_userid] obj.userid = req_userid obj_tmp.append(obj) #logger.info('get_video_by_users, add video info:%s'%obj.toDict()) end_sec = item.get('end_sec') or 0 end_usec = item.get('end_usec') or 0 #end_sec 0 represent has no more datas, so do not response to client. if not end_sec: continue data['user_info'].append({ 'req_user_id': req_userid, 'begin_sec': end_sec, 'begin_usec': end_usec }) for user_profile in user.get_user_profile_by_ids(userid_tmps): user_id_profile_map[user_profile.no] = user_profile for obj in obj_tmp: profile = user_id_profile_map[obj.userid] obj.nickname = profile.nickname obj.usericon = profile.user_icon arr.append(obj.toDict()) data['total'] = len(arr) except: logger.error(traceback.format_exc()) finally: udp_client.close() return data
def get_uid_by_userid(self, userid): self.__base = self.__base or BaseDao() op = UserProfile(self.__base) uid = op.get_uid_by_userid(userid) return uid