PATCH: fixed issues with authentication

This commit is contained in:
white
2025-01-29 11:31:43 +00:00
parent cd1a246483
commit e2070be9f9
9 changed files with 86 additions and 107 deletions

View File

@@ -6,10 +6,10 @@ def user_recommendation_category(user_id: int) -> Optional[int]:
Queries user_preferences database to find users favourite streaming category and returns the category
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
data = cursor.execute(
"SELECT category_id FROM user_preferences WHERE user_id = ? ORDER BY favourability DESC LIMIT 1", (user_id,)).fetchone()
data = db.fetchone(
"SELECT category_id FROM user_preferences WHERE user_id = ? ORDER BY favourability DESC LIMIT 1", (user_id,))
return data[0]
def followed_categories_recommendations(user_id: int):

View File

@@ -8,15 +8,15 @@ def streamer_live_status(user_id: int) -> bool:
Returns boolean on whether the given streamer is live
"""
db = Database()
cursor = db.create_connection()
return bool(cursor.execute("SELECT 1 FROM streams WHERE user_id = ? AND isLive = 1 ORDER BY stream_id DESC", (user_id,)).fetchone())
db.create_connection()
return bool(db.fetchone("SELECT 1 FROM streams WHERE user_id = ? AND isLive = 1 ORDER BY stream_id DESC", (user_id,)))
def followed_live_streams(user_id: int) -> list[dict]:
"""
Searches for streamers who the user followed which are currently live
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
live_streams = db.fetchall("""
SELECT user_id, stream_id, title, num_viewers
@@ -60,7 +60,7 @@ def user_stream(user_id: int, stream_id: int) -> dict:
Returns data of a streamers selected stream
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
stream = db.fetchone("SELECT * FROM streams WHERE user_id = ? AND stream_id = ?", (user_id,stream_id))
return stream

View File

@@ -14,13 +14,13 @@ def get_user_id(username: str) -> Optional[int]:
Returns user_id associated with given username
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
try:
data = cursor.execute(
data = db.fetchone(
"SELECT user_id FROM users WHERE username = ?",
(username,)
).fetchone()
)
return data[0] if data else None
except Exception as e:
print(f"Error: {e}")
@@ -31,13 +31,13 @@ def get_username(user_id: str) -> Optional[str]:
Returns username associated with given user_id
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
try:
data = cursor.execute(
data = db.fetchone(
"SELECT username FROM user WHERE user_id = ?",
(user_id,)
).fetchone()
)
return data[0] if data else None
except Exception as e:
print(f"Error: {e}")
@@ -48,13 +48,13 @@ def is_user_partner(user_id: int) -> bool:
Returns True if user is a partner, else False
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
try:
data = cursor.execute(
data = db.fetchone(
"SELECT is_partnered FROM users WHERE user_id = ?",
(user_id,)
).fetchone()
)
return bool(data)
except Exception as e:
print(f"Error: {e}")
@@ -65,13 +65,13 @@ def is_subscribed(user_id: int, streamer_id: int) -> bool:
Returns True if user is subscribed to a streamer, else False
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
try:
result = cursor.execute(
result = db.fetchone(
"SELECT 1 FROM subscribes WHERE user_id = ? AND streamer_id = ? AND expires > ?",
(user_id, streamer_id, datetime.now())
).fetchone()
)
return bool(result)
except Exception as e:
print(f"Error: {e}")
@@ -79,13 +79,13 @@ def is_subscribed(user_id: int, streamer_id: int) -> bool:
def is_following(user_id: int, followed_id: int) -> bool:
db = Database()
cursor = db.create_connection()
db.create_connection()
try:
result = cursor.execute(
result = db.fetchone(
"SELECT 1 FROM follows WHERE user_id = ? AND followed_id = ?",
(user_id, followed_id)
).fetchone()
)
return bool(result)
except Exception as e:
print(f"Error: {e}")
@@ -96,11 +96,11 @@ def subscription_expiration(user_id: int, subscribed_id: int) -> int:
Returns the amount of time left until user subscription to a streamer ends
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
remaining_time = 0
try:
data = cursor.execute(
"SELECT expires from subscriptions WHERE user_id = ? AND subscribed_id = ? AND expires > since", (user_id,subscribed_id)).fetchone()
data = db.fetchone(
"SELECT expires from subscriptions WHERE user_id = ? AND subscribed_id = ? AND expires > since", (user_id,subscribed_id))
if data:
expiration_date = data[0]
@@ -130,7 +130,7 @@ def reset_password(new_password: str, email: str):
try:
db.execute("UPDATE users SET password = ? WHERE email = ?", (generate_password_hash(new_password), email))
db.commit()
db.commit_data()
return True
except Exception as e:
print(f"Error: {e}")

View File

@@ -5,8 +5,8 @@ def categories():
Returns all possible streaming categories
"""
db = Database()
cursor = db.create_connection()
all_categories = cursor.execute("SELECT * FROM categories").fetchall()
db.create_connection()
all_categories = db.fetchall("SELECT * FROM categories")
return all_categories
def tags():
@@ -14,8 +14,8 @@ def tags():
Returns all possible streaming tags
"""
db = Database()
cursor = db.create_connection()
all_tags = cursor.execute("SELECT * FROM tags").fetchall()
db.create_connection()
all_tags = db.fetchall("SELECT * FROM tags")
return all_tags
def most_popular_category():
@@ -23,9 +23,9 @@ def most_popular_category():
Returns the most popular category based on live stream viewers
"""
db = Database()
cursor = db.create_connection()
db.create_connection()
category = cursor.execute("""
category = db.fetchone("""
SELECT categories.category_id, categories.category_name
FROM streams
JOIN categories ON streams.category_id = categories.category_id
@@ -33,7 +33,7 @@ def most_popular_category():
GROUP BY categories.category_name
ORDER BY SUM(streams.num_viewers) DESC
LIMIT 1;
""").fetchone()
""")
return category