lightmate commited on
Commit
455555b
·
verified ·
1 Parent(s): d294409

Upload 2 files

Browse files
Files changed (2) hide show
  1. modules/record.py +55 -0
  2. modules/utils.py +9 -0
modules/record.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dotenv import load_dotenv
2
+ import pymysql
3
+ import os
4
+
5
+ # Load environment variables
6
+ load_dotenv()
7
+
8
+ class DatabaseComponent:
9
+ def __init__(self):
10
+ # Establish the connection
11
+ self.connection = pymysql.connect(
12
+ host=os.getenv("DB_HOST"),
13
+ user=os.getenv("DB_USER"),
14
+ password=os.getenv("DB_PASSWORD"),
15
+ port=int(os.getenv("DB_PORT")),
16
+ database=os.getenv("DB_NAME"),
17
+ ssl={'ca': None} # Disable SSL for Windows
18
+ )
19
+
20
+ def save_news_verification(self, title, score, citation):
21
+ """
22
+ Saves a news verification result to the database.
23
+ """
24
+ with self.connection.cursor() as cursor:
25
+ sql = """
26
+ INSERT INTO news_verifications (article_title, truthfulness_score, citation)
27
+ VALUES (%s, %s, %s)
28
+ """
29
+ cursor.execute(sql, (title, score, citation))
30
+ self.connection.commit()
31
+
32
+ def get_total_news_count(self):
33
+ """
34
+ Fetches the total number of news articles checked.
35
+ """
36
+ with self.connection.cursor() as cursor:
37
+ sql = "SELECT COUNT(*) FROM news_verifications"
38
+ cursor.execute(sql)
39
+ result = cursor.fetchone()
40
+ return result[0] if result else 0
41
+
42
+ def get_last_10_news(self):
43
+ """
44
+ Fetches the last 10 saved news verification entries.
45
+ """
46
+ with self.connection.cursor() as cursor:
47
+ sql = """
48
+ SELECT article_title, truthfulness_score, citation, created_at
49
+ FROM news_verifications
50
+ ORDER BY created_at DESC
51
+ LIMIT 10
52
+ """
53
+ cursor.execute(sql)
54
+ result = cursor.fetchall()
55
+ return [{"title": row[0], "score": row[1], "citation": row[2], "timestamp": row[3]} for row in result]
modules/utils.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ def clean_text(text):
4
+ """Removes unnecessary spaces, special characters, and formats text properly."""
5
+ return re.sub(r'\s+', ' ', text).strip()
6
+
7
+ def normalize_score(score):
8
+ """Ensures the score is within the range 0 to 1."""
9
+ return max(0, min(score, 1))