-
Notifications
You must be signed in to change notification settings - Fork 1
/
class_friend.py
81 lines (70 loc) · 2.6 KB
/
class_friend.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import mysql.connector
import json
password_temp_1 = input("Enter friend db password: ")
class Friend:
def __init__(self):
self.db = mysql.connector.connect(
host="db-mysql-lon1-35653-do-user-13900096-0.b.db.ondigitalocean.com",
user="doadmin",
password=password_temp_1,
database="defaultdb",
port=25060,
)
self.cursor = self.db.cursor()
def add(self, username, likes):
query = "INSERT INTO friend (Username, Likes) VALUES (%s, %s)"
values = (username, json.dumps(likes))
self.cursor.execute(query, values)
self.db.commit()
print("Friend added successfully.")
def delete(self, username, like):
likes = self.query_likes(username)
if like in likes:
likes.remove(like)
self.update_likes(username, likes)
print("Like deleted successfully.")
else:
print("Like not found for the specified username.")
def update(self, username, likes):
self.update_likes(username, likes)
print("Likes updated successfully.")
def query_likes(self, username):
query = "SELECT Likes FROM friend WHERE Username = %s"
values = (username,)
self.cursor.execute(query, values)
result = self.cursor.fetchone()
if result:
likes = json.loads(result[0])
return likes if likes else []
else:
print("Friend not found.")
return []
def update_likes(self, username, likes):
query = "UPDATE friend SET Likes = %s WHERE Username = %s"
values = (json.dumps(likes), username)
self.cursor.execute(query, values)
self.db.commit()
def query_username(self, username):
query = "SELECT * FROM friend WHERE Username = %s"
values = (username,)
self.cursor.execute(query, values)
result = self.cursor.fetchone()
if result:
return result[0], json.loads(result[1])
else:
return None
def query_same_likes(self, likes):
query = "SELECT Username, Likes FROM friend"
self.cursor.execute(query)
result = self.cursor.fetchall()
matching_usernames = []
for row in result:
username = row[0]
db_likes = json.loads(row[1])
if any(like in db_likes for like in likes):
matching_usernames.append(username)
if matching_usernames:
return matching_usernames
else:
print("No friends found with at least one similar like.")
return []