def getUsersInfoByUidInteract(ui): users_rank = [] rank = 1 for k, v in ui: count, get_results = xapian_search_user.search(query={'name': k}, \ fields=['_id', 'name', 'followers_count', \ 'friends_count', 'location']) if count == 0: try: count, get_results = xapian_search_user.search(query={'_id': int(k)}, \ fields=['_id', 'name', 'followers_count', \ 'friends_count', 'location']) except: pass if count: for r in get_results(): uid = r['_id'] name = r['name'] followers_count = r['followers_count'] friends_count = r['friends_count'] location = r['location'] domain = user2domain(uid) interact = v row = (rank, interact, uid, name, followers_count, friends_count, location, domain) users_rank.append(row) rank += 1 return users_rank
def getUidByName(name): count, users = xapian_search_user.search(query={'name': name}, fields=['_id']) if count: for user in users(): return user['_id'] else: return None
def yymInfo(uid): try: query_dict = { '_id': int(uid) } except: query_dict = { 'name': uid } count, get_results = xapian_search_user.search(query=query_dict, fields=['created_at', '_id', 'name', \ 'statuses_count', 'followers_count', 'friends_count', 'description', 'profile_image_url', 'verified', 'gender']) if count: for r in get_results(): statusesCount = r['statuses_count'] followersCount = r['followers_count'] friendsCount = r['friends_count'] userName = r['name'] description = r['description'] uid = r['_id'] profileImageUrl = r['profile_image_url'] verified = r['verified'] gender = r['gender'] user = {'id': uid, 'userName': userName, 'statusesCount': statusesCount, 'followersCount': \ followersCount, 'friendsCount': friendsCount, 'description': description, 'profileImageUrl': profileImageUrl, 'verified': verified, 'gender': gender} return user else: return None
def profile_person(uid): read_from_xapian = 0 sharding = False timerange = request.args.get('during_time', None) if not timerange: timerange = default_timerange else: timerange = timerange.strip() start_ts, end_ts = _time_zone(_utf_encode(timerange)) if 'logged_in' in session and session['logged_in']: if session['user'] == 'admin': if uid: user = {} _active, _important, _reposts, _original, _emoticon, _direct_interact, _retweeted_interact, _keywords_dict = getPersonData(uid, default_profile_person_time) user['updatetime'] = default_profile_person_time user['active_rank'] = _active user['important_rank'] = _important if read_from_xapian: count, get_results = xapian_search_user.search(query={'_id': int(uid)}, fields=['profile_image_url', 'name', 'friends_count', \ 'statuses_count', 'followers_count', 'gender', 'verified', 'created_at', 'location']) if count > 0: for r in get_results(): user_character = {'id': uid, 'profile_image_url': r['profile_image_url'], 'userName': _utf_8_decode(r['name']), 'friends_count': r['friends_count'], \ 'statuses_count': r['statuses_count'], 'followers_count': r['followers_count'], 'gender': r['gender'], \ 'verified': r['verified'], 'created_at': r['created_at'], 'location': _utf_8_decode(r['location'])} user.update(user_character) user['created_at'] = ts2HMS(user['created_at']); else: return 'no such user' else: status1, personbasic = _search_person_basic(uid, sharding) if status1 == 'success': verifiedTypenum = personbasic.verifiedType friendsCount = personbasic.friendsCount followersCount = personbasic.followersCount statuseCount = personbasic.statuseCount created_at = time.strftime("%m月 %d日, %Y", time.localtime(personbasic.created_at)) user_character = {'id': personbasic.userId, 'profile_image_url': personbasic.profileImageUrl, 'userName': _utf_8_decode(personbasic.name), \ 'friends_count': friendsCount, 'statuses_count': statuseCount, 'followers_count': followersCount, \ 'gender': personbasic.gender, 'verified': personbasic.verified, 'created_at': _utf_8_decode(created_at), \ 'location': _utf_8_decode(personbasic.location), 'date': personbasic.date, \ 'verifiedTypenum': verifiedTypenum, 'description': _utf_8_decode(personbasic.description)} user.update(user_character) else: return 'no such user' return render_template('profile/profile_person.html', user=user, start_ts=start_ts, end_ts=end_ts, timerange=timerange) else: return redirect('/') else: return redirect('/')
def acquire_user_by_id(uid): count, get_results = user_search.search(query={'_id': uid}) user = {} if count == 0: return 0 for r in get_results(): user['name'] = r['name'] user['location'] = r['location'] user['friends_count'] = r['friends_count'] return user
def acquire_user_by_id(uid): count, get_results = user_search.search(query={"_id": uid}) user = {} if count == 0: return 0 for r in get_results(): user["name"] = r["name"] user["location"] = r["location"] user["friends_count"] = r["friends_count"] return user
def getUserInfoById(uid): count, get_results = xapian_search_user.search(query={'_id': uid}, fields=['profile_image_url', 'name', 'friends_count', \ 'statuses_count', 'followers_count', 'gender', 'verified', 'created_at', 'location', 'description']) if count: for r in get_results(): user = {'id': uid, 'profile_image_url': r['profile_image_url'], 'userName': r['name'], 'friendsCount': r['friends_count'], \ 'statusesCount': r['statuses_count'], 'followersCount': r['followers_count'], 'gender': r['gender'], \ 'verified': r['verified'], 'created_at': r['created_at'], 'location': _utf_8_decode(r['location']), 'description': r['description']} return user else: return None
def add_trash(): result = "Right" new_field = request.form["f_id"] count, get_results = xapian_search_user.search(query={"_id": new_field}, fields=["_id", "name"]) if count > 0: for get_result in get_results(): new_item = BlackList(blackID=get_result["_id"], blackName=get_result["name"]) db.session.add(new_item) db.session.commit() else: result = "Wrong" return json.dumps(result)
def add_media(): result = 'Right' new_field = request.form['topic'] count, get_results = xapian_search_user.search(query={'_id': new_field}, fields=['_id', 'name']) if count > 0: for get_result in get_results(): new_item = IMedia(mediaID=get_result['_id'],mediaName=get_result['name']) db.session.add(new_item) db.session.commit() else: result = 'Wrong' return json.dumps(result)
def add_kd(): result = "Right" new_field = request.form["f_id"] t = request.form["type"] count, get_results = xapian_search_user.search(query={"_id": new_field}, fields=["_id", "name"]) if count > 0: for get_result in get_results(): new_item = KnowledgeList(kID=get_result["_id"], kName=get_result["name"], domain=t) db.session.add(new_item) db.session.commit() else: result = "Wrong" return json.dumps(result)
def burst_caculate(date, identifyWindow, identifyMethod, top_n): current_time = datetime2ts(date) current_date = ts2datetime(current_time) data = [] if identifyWindow == 1:#时间窗为1,直接从数据库中读取 items = db.session.query(BurstIdentification).filter((BurstIdentification.identifyMethod==identifyMethod)&(BurstIdentification.identifyWindow==identifyWindow)&(BurstIdentification.identifyDate==current_date)&(BurstIdentification.rank<=top_n)).all() if len(items): for item in items: rank = int(item.rank) uid = str(item.userId) followersCount = int(item.followersCount) activeCount = int(item.activeCount) importantCount = int(item.importantCount) activeDiff = int(item.activeDiff) importantDiff = int(item.importantDiff) user = acquire_user_by_id(uid) if user == 0: continue name = user['name'] location = user['location'] friends_count = int(user['friends_count']) status = user_status(uid) if identifyMethod == 'active': row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, activeDiff, status] else: row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, importantDiff, status] data.append(row) else:#时间窗为7或30天 end = datetime2ts(date) active = dict() important = dict() follower = dict() active_diff = dict() important_diff = dict() for i in range(0,identifyWindow):#遍历所有时间 begints = end - i*24*3600 begin = ts2datetime(begints) items = db.session.query(BurstIdentification).filter((BurstIdentification.identifyMethod==identifyMethod)&(BurstIdentification.identifyWindow==1)&(BurstIdentification.identifyDate==begin)).all() if len(items): for item in items:#将数据库中粉丝数、活跃度、重要度累加 uid = str(item.userId) if follower.has_key(uid): pass else: count, get_results = user_search.search(query={'_id': uid},field=['followers_count'])#查找对应用户的粉丝数 if count == 0: continue for r in get_results(): follower[uid] = int(r['followers_count']) if active.has_key(uid): active[uid] = active[uid] + int(item.activeCount) else: active[uid] = int(item.activeCount) if important.has_key(uid): important[uid] = important[uid] + int(item.importantCount) else: important[uid] = int(item.importantCount) if active_diff.has_key(uid): active_diff[uid] = active_diff[uid] + int(item.activeDiff) else: active_diff[uid] = int(item.activeDiff) if important_diff.has_key(uid): important_diff[uid] = important_diff[uid] + int(item.importantDiff) else: important_diff[uid] = int(item.importantDiff) else: continue rank_data = burst_rank(active,important,follower,active_diff,important_diff,top_n,identifyMethod) if identifyMethod == 'active': for i in range(0,len(rank_data)): rank = i+1 uid = rank_data[i][5] followersCount = rank_data[i][4] activeCount = rank_data[i][2] importantCount = rank_data[i][3] activeDiff = rank_data[i][0] importantDiff = rank_data[i][1] user = acquire_user_by_id(uid) if user == 0: continue name = user['name'] location = user['location'] friends_count = int(user['friends_count']) status = user_status(uid) row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, activeDiff, status] data.append(row) if identifyMethod == 'important': for i in range(0,len(rank_data)): rank = i+1 uid = rank_data[i][5] followersCount = rank_data[i][4] activeCount = rank_data[i][2] importantCount = rank_data[i][3] activeDiff = rank_data[i][1] importantDiff = rank_data[i][0] user = acquire_user_by_id(uid) if user == 0: continue name = user['name'] location = user['location'] friends_count = int(user['friends_count']) status = user_status(uid) row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, importantDiff, status] data.append(row) return data
def area_caculate(date, identifyWindow, identifyMethod, top_n, area): current_time = datetime2ts(date) current_date = ts2datetime(current_time) data = [] if identifyWindow == 1: # 时间窗为1,直接从数据库中读取 items = ( db.session.query(AreaIdentification) .filter( (AreaIdentification.identifyMethod == identifyMethod) & (AreaIdentification.identifyWindow == identifyWindow) & (AreaIdentification.identifyDate == current_date) & (AreaIdentification.rank <= top_n) & (AreaIdentification.topicId == area) ) .all() ) if len(items): for item in items: rank = int(item.rank) uid = str(item.userId) followersCount = int(item.followersCount) activeCount = int(item.activeCount) importantCount = int(item.importantCount) user = acquire_user_by_id(uid) if user == 0: continue name = user["name"] location = user["location"] friends_count = int(user["friends_count"]) status = user_status(uid) row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, status] data.append(row) else: # 时间窗为7或30天 end = datetime2ts(date) active = dict() important = dict() follower = dict() for i in range(0, identifyWindow): # 遍历所有时间 begints = end - i * 24 * 3600 begin = ts2datetime(begints) items = ( db.session.query(AreaIdentification) .filter( (AreaIdentification.identifyMethod == identifyMethod) & (AreaIdentification.identifyWindow == 1) & (AreaIdentification.identifyDate == begin) & (AreaIdentification.topicId == area) ) .all() ) if len(items): for item in items: # 将数据库中粉丝数、活跃度、重要度累加 uid = str(item.userId) if follower.has_key(uid): pass else: count, get_results = user_search.search( query={"_id": uid}, field=["followers_count"] ) # 查找对应用户的粉丝数 if count == 0: continue for r in get_results(): follower[uid] = int(r["followers_count"]) if active.has_key(uid): active[uid] = active[uid] + int(item.activeCount) else: active[uid] = int(item.activeCount) if important.has_key(uid): important[uid] = important[uid] + int(item.importantCount) else: important[uid] = int(item.importantCount) else: continue rank_data = area_rank(active, important, follower, top_n, identifyMethod) if identifyMethod == "active": for i in range(0, len(rank_data)): rank = i + 1 uid = rank_data[i][3] followersCount = rank_data[i][2] activeCount = rank_data[i][0] importantCount = rank_data[i][1] user = acquire_user_by_id(uid) if user == 0: continue name = user["name"] location = user["location"] friends_count = int(user["friends_count"]) status = user_status(uid) row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, status] data.append(row) if identifyMethod == "important": for i in range(0, len(rank_data)): rank = i + 1 uid = rank_data[i][3] followersCount = rank_data[i][2] activeCount = rank_data[i][1] importantCount = rank_data[i][0] user = acquire_user_by_id(uid) if user == 0: continue name = user["name"] location = user["location"] friends_count = int(user["friends_count"]) status = user_status(uid) row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, status] data.append(row) if identifyMethod == "followers": for i in range(0, len(rank_data)): rank = i + 1 uid = rank_data[i][3] followersCount = rank_data[i][0] activeCount = rank_data[i][1] importantCount = rank_data[i][2] user = acquire_user_by_id(uid) if user == 0: continue name = user["name"] location = user["location"] friends_count = int(user["friends_count"]) status = user_status(uid) row = [rank, uid, name, location, friends_count, followersCount, activeCount, importantCount, status] data.append(row) return data
def profile_search(model='hotest'): default_search_time = get_default_search_time() statuses_count_upBound = default_statuses_range friends_count_upBound = default_friends_range followers_count_upBound = default_followers_range local_statuses_range = statuses_count_upBound[0] local_followers_range = followers_count_upBound[0] local_friends_range = friends_count_upBound[0] if 'logged_in' in session and session['logged_in']: if session['user'] == 'admin': if request.method == 'GET': province = default_province province_str = ','.join(province) if model == 'person': nickname = urllib2.unquote(request.args.get('nickname')) return render_template('profile/profile_search.html', statusescount_up=statuses_count_upBound, \ friendscount_up=friends_count_upBound, followerscount_up=followers_count_upBound, \ location=province_str, model=model, result=None, nickname=nickname, \ field_dict=default_field_dict, time=default_search_time, \ local_statusescount_up=local_statuses_range, local_friendscount_up=local_friends_range, \ local_followerscount_up=local_followers_range) elif model == 'find': if request.args.get('statuses_count_upBound', None): local_statuses_range = request.args.get('statuses_count_upBound', None) local_statuses_range = (local_statuses_range, default_statuses_dict[local_statuses_range]) if request.args.get('friends_count_upBound',None): local_friends_range = request.args.get('friends_count_upBound',None) local_friends_range = (local_friends_range, default_friends_dict[local_friends_range]) if request.args.get('followers_count_upBound',None): local_followers_range = request.args.get('followers_count_upBound',None) local_followers_range = (local_followers_range, default_followers_dict[local_followers_range]) top_n = request.args.get('search_top_n', None) province_str = request.args.get('province_str', None) rank_str = request.args.get('rankcount', None) rank_str_zh = '' if rank_str == 'followers_count': rank_str_zh = u'粉丝数降序' elif rank_str == 'statuses_count': rank_str_zh = u'微博数降序' elif rank_str == 'friends_count': rank_str_zh = u'关注数降序' elif rank_str == 'created_at': rank_str_zh = u'注册时间降序' if(top_n): result_count = int(top_n) else: result_count = 100 return render_template('profile/profile_search.html',result_count=result_count, statusescount_up=statuses_count_upBound, \ friendscount_up=friends_count_upBound, followerscount_up=followers_count_upBound, \ location=province_str, model=model, rankcount=rank_str, field_dict=default_field_dict, \ time=default_search_time, sort=rank_str_zh, \ local_statusescount_up=local_statuses_range, local_friendscount_up=local_friends_range, \ local_followerscount_up=local_followers_range) else: return render_template('profile/profile_search.html', statusescount_up=statuses_count_upBound, \ friendscount_up=friends_count_upBound, followerscount_up=followers_count_upBound, \ location=province_str, model=model, result=None, \ field_dict=default_field_dict, time=default_search_time, \ local_statusescount_up=local_statuses_range, local_friendscount_up=local_friends_range, \ local_followerscount_up=local_followers_range) if request.method == 'POST' and request.form['page']: if model == 'newest': top_n = 1000 page = int(request.form['page']) if page == 1: startoffset = 0 else: startoffset = (page - 1) * COUNT_PER_PAGE endoffset = startoffset + COUNT_PER_PAGE - 1 users = _newest_users(top_n) return json.dumps(users[startoffset:endoffset]) elif model == 'hotest': top_n = 1000 latest = get_default_hotest_time() page = int(request.form['page']) if page == 1: startoffset = 0 else: startoffset = (page - 1) * COUNT_PER_PAGE endoffset = startoffset + COUNT_PER_PAGE - 1 users = _hotest_users(top_n, latest) return json.dumps(users[startoffset:endoffset]) elif model == 'oversea': page = int(request.form['page']) if page == 1: startoffset = 0 else: startoffset = (page - 1) * COUNT_PER_PAGE endoffset = startoffset + COUNT_PER_PAGE - 1 province = '海外' count, get_results = xapian_search_user.search(query={'location': province}, sort_by=['followers_count'], max_offset=10000, fields=['_id', 'name', 'statuses_count', 'friends_count', 'followers_count', 'profile_image_url', 'description']) users = [] offset = 0 for r in get_results(): if offset >= startoffset and offset <= endoffset: users.append({'id': r['_id'], 'profileImageUrl': r['profile_image_url'], 'userName': r['name'], 'statusesCount': r['statuses_count'], 'friendsCount': r['friends_count'], 'followersCount': r['followers_count'], 'description': r['description']}) if offset >= endoffset: break offset += 1 return json.dumps(users) elif model in DOMAIN_LIST: page = int(request.form['page']) if page == 1: startoffset = 0 else: startoffset = (page - 1) * COUNT_PER_PAGE endoffset = startoffset + COUNT_PER_PAGE - 1 users = _domain_users(model) return json.dumps(users[startoffset:endoffset]) elif model == 'person': sharding = True nickname = urllib2.unquote(request.form['nickname']) uid = getUidByName(nickname) users = [] if uid: status, user = _search_person_basic(uid, sharding) if status == 'success': users.append(user._to_dict()) return json.dumps(users) elif model == 'find': read_from_xapian = 0 sharding = True page = int(request.form['page']) if page == 1: startoffset = 0 else: startoffset = (page - 1) * COUNT_PER_PAGE endoffset = startoffset + COUNT_PER_PAGE - 1 result_count = int(request.form['result_count']) statusescount_low, statusescount_up = parseRangePara(request.form['statusescount_up']) friendscount_low, friendscount_up = parseRangePara(request.form['friendscount_up']) followerscount_low, followerscount_up = parseRangePara(request.form['followerscount_up']) province_str = request.form['province'] rankcount = request.form['rankcount'] province = province_str.split(',') users = [] query_dict = {} query_dict['$and'] = [] query_dict['$and'].append({'statuses_count':{'$gt': statusescount_low, '$lt': statusescount_up }}) query_dict['$and'].append({'followers_count':{'$gt': followerscount_low, '$lt': followerscount_up}}) query_dict['$and'].append({'friends_count':{'$gt': friendscount_low, '$lt': friendscount_up}}) or_dict = {} or_dict['$or'] = [] for pro in province: or_dict['$or'].append({'location': pro}) query_dict['$and'].append(or_dict) if read_from_xapian: count, get_results = xapian_search_user.search(query=query_dict, max_offset=result_count, fields=[ '_id', 'name', 'statuses_count', 'followers_count', 'friends_count', 'description', 'profile_image_url'], sort_by=[rankcount]) for r in get_results(): statusesCount = r['statuses_count'] followersCount = r['followers_count'] friendsCount = r['friends_count'] userName = r['name'] description = r['description'] uid = r['_id'] profileImageUrl = r['profile_image_url'] users.append({'id': uid, 'userName': userName, 'statusesCount': statusesCount, 'followersCount': followersCount, 'friendsCount': friendsCount, 'description': description, 'profileImageUrl': profileImageUrl}) else: users = _multi_search(query_dict, rankcount, result_count, sharding) return json.dumps(users[startoffset:endoffset]) else: return redirect('/') else: return redirect('/')
def getUserIdByName(name): count, get_results = xapian_search_user.search(query={'name': name}, fields=['_id']) uid = name for r in get_results(): uid = user['_id'] return uid