|
from dotenv import load_dotenv |
|
import pymysql |
|
import os |
|
|
|
|
|
load_dotenv() |
|
|
|
class DatabaseComponent: |
|
def __init__(self): |
|
|
|
self.connection = pymysql.connect( |
|
host=os.getenv("DB_HOST"), |
|
user=os.getenv("DB_USER"), |
|
password=os.getenv("DB_PASSWORD"), |
|
port=int(os.getenv("DB_PORT")), |
|
database=os.getenv("DB_NAME"), |
|
ssl={'ca': None} |
|
) |
|
|
|
def save_news_verification(self, title, score, citation): |
|
""" |
|
Saves a news verification result to the database. |
|
""" |
|
with self.connection.cursor() as cursor: |
|
sql = """ |
|
INSERT INTO news_verifications (article_title, truthfulness_score, citation) |
|
VALUES (%s, %s, %s) |
|
""" |
|
cursor.execute(sql, (title, score, citation)) |
|
self.connection.commit() |
|
|
|
def get_total_news_count(self): |
|
""" |
|
Fetches the total number of news articles checked. |
|
""" |
|
with self.connection.cursor() as cursor: |
|
sql = "SELECT COUNT(*) FROM news_verifications" |
|
cursor.execute(sql) |
|
result = cursor.fetchone() |
|
return result[0] if result else 0 |
|
|
|
def get_last_10_news(self): |
|
""" |
|
Fetches the last 10 saved news verification entries. |
|
""" |
|
with self.connection.cursor() as cursor: |
|
sql = """ |
|
SELECT article_title, truthfulness_score, citation, created_at |
|
FROM news_verifications |
|
ORDER BY created_at DESC |
|
LIMIT 10 |
|
""" |
|
cursor.execute(sql) |
|
result = cursor.fetchall() |
|
return [{"title": row[0], "score": row[1], "citation": row[2], "timestamp": row[3]} for row in result] |
|
|