File size: 1,861 Bytes
455555b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
import pymysql
import os

# Load environment variables
load_dotenv()

class DatabaseComponent:
    def __init__(self):
        # Establish the connection
        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}  # Disable SSL for Windows
        )

    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]