157 lines
5.1 KiB
Python
157 lines
5.1 KiB
Python
from modules.user import info as user_info
|
|
from modules.algorithms.univ import dict_key_verify
|
|
from modules.algorithms.uuid import hash_string
|
|
|
|
# RBP
|
|
import time
|
|
# RBP
|
|
|
|
class user():
|
|
def __init__(self, username, origin=False, **kwargs):
|
|
self.username = username
|
|
self.origin = origin
|
|
self.friend_list = []
|
|
self.friends = user_info.friend(username=self.username)
|
|
self.count = 1
|
|
|
|
def print_list(self):
|
|
username_list = [friend.username for friend in self.friend_list]
|
|
return username_list
|
|
|
|
def print_count(self):
|
|
count_list = []
|
|
for friend in self.friend_list:
|
|
if self.friend_count[hash(friend)] != 0:
|
|
count_list.append({friend.username: friend.count})
|
|
return count_list
|
|
|
|
def get_friends(self, depth=1, exclude=[], **kwargs):
|
|
friends = self.friends.get()
|
|
if dict_key_verify(friends, "friends"):
|
|
self.__organise_friends(friends['friends'], depth, exclude)
|
|
|
|
def __organise_friends(self, friends, depth=1, exclude=[], **kwargs):
|
|
for friend in friends:
|
|
if friend['username'] not in exclude:
|
|
self.friend_list.append(user(friend['username']))
|
|
|
|
depth -= 1
|
|
if depth > 0:
|
|
if self.username not in exclude:
|
|
exclude.append(self.username)
|
|
if self.origin:
|
|
exclude = exclude + [friend.username for friend in self.friend_list]
|
|
|
|
for friend in self.friend_list:
|
|
friend.get_friends(depth=depth, exclude=exclude)
|
|
|
|
def count_friend_frequency_old(self):
|
|
friend_count = []
|
|
self.friend_count = self.__inspect_friends(friend_count)
|
|
|
|
def __inspect_friends_old(self, friend_count):
|
|
for friend in self.friend_list:
|
|
if friend.friend_list:
|
|
friend_count = friend.__inspect_friends(friend_count)
|
|
hash_friend_count = [hash(friend) for friend in friend_count]
|
|
|
|
found = False
|
|
for i, friend_hash in enumerate(hash_friend_count):
|
|
if friend_hash == hash(friend):
|
|
friend_count[i].count += 1
|
|
found = True
|
|
break
|
|
|
|
if not found:
|
|
friend_count.append(friend)
|
|
|
|
return friend_count
|
|
|
|
def count_friend_frequency(self):
|
|
pre = time.time()
|
|
friend_count = [0]*(10**7+7)
|
|
print(f"friend_count took to create: {time.time() - pre}")
|
|
self.friend_count = self.__inspect_friends(friend_count)
|
|
|
|
def __inspect_friends(self, friend_count):
|
|
for friend in self.friend_list:
|
|
if friend.friend_list:
|
|
friend_count = friend.__inspect_friends(friend_count)
|
|
print("returned")
|
|
|
|
print("")
|
|
if friend in friend_count:
|
|
pre = time.time()
|
|
friend_count[hash(friend)].count += 1
|
|
print(f"count lookup took: {time.time() - pre}")
|
|
else:
|
|
pre = time.time()
|
|
friend_count[hash(friend)] = friend
|
|
print(f"append lookup took: {time.time() - pre}")
|
|
|
|
print("RETURNING")
|
|
return friend_count
|
|
|
|
def count_user_frequency(self):
|
|
user_id = user_info.user_id(username=self.username).get()['user_id']
|
|
self.cur.execute("SELECT COUNT * FROM friends WHERE friend_id = ?", (user_id,))
|
|
rez = self.cur.fetchone()
|
|
if rez:
|
|
return rez[0]
|
|
else:
|
|
return 0
|
|
|
|
def __hash__(self):
|
|
obj_hash = hash_string(self.username)
|
|
return obj_hash
|
|
|
|
def choose_recomendations(amount, origin_user):
|
|
recomended = []
|
|
recomended_hash = []
|
|
for i in range(amount):
|
|
largest = user("user")
|
|
largest.count = 0
|
|
|
|
for friend in origin_user.friend_list:
|
|
if origin_user.friend_count[hash(friend)] != 0:
|
|
if hash(friend) not in recomended_hash:
|
|
if friend.count > largest.count:
|
|
largest = friend
|
|
|
|
elif friend.count == largest.count:
|
|
largest_frequency = largest.count_user_frequency()
|
|
friend_frequency = friend.count_user_frequency()
|
|
|
|
if friend_frequency > largest_frequency:
|
|
largest = friend
|
|
|
|
recomended.append(largest)
|
|
recomended_hash.append(hash(largest))
|
|
|
|
# RBP
|
|
for i, friend in enumerate(recomended):
|
|
print(f"{i}: {friend.username}")
|
|
# RBP
|
|
|
|
return recomended
|
|
|
|
def friend_recomend(username, amount=1, depth=1, **kwargs):
|
|
origin_user = user(username, origin=True)
|
|
origin_user.get_friends(depth)
|
|
print(f"origin_user: {origin_user.username}: {origin_user.print_list()}")
|
|
|
|
if not origin_user.friend_list:
|
|
# NOT FINSIHED
|
|
pass
|
|
db.cur.execute("SELECT COUNT * FROM friends")
|
|
|
|
origin_user.count_friend_frequency()
|
|
print(origin_user.print_count())
|
|
recomended = choose_recomendations(amount, origin_user)
|
|
|
|
def main():
|
|
friend_recomend("user1", 1, 3)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|