ayush2917 commited on
Commit
72b3d89
·
verified ·
1 Parent(s): 0b5a303

Update database.py

Browse files
Files changed (1) hide show
  1. database.py +39 -88
database.py CHANGED
@@ -1,99 +1,50 @@
1
- import sqlite3
2
  import os
 
3
  from flask import g
4
- from datetime import datetime, timedelta
5
 
6
- def get_db():
7
- """Get database connection with retry logic"""
8
- if 'db' not in g:
9
- max_retries = 3
10
- for attempt in range(max_retries):
11
- try:
12
- g.db = sqlite3.connect(os.getenv('DATABASE_URL', '/app/data/news.db'))
13
- g.db.row_factory = sqlite3.Row
14
- # Enable WAL mode for better concurrency
15
- g.db.execute('PRAGMA journal_mode=WAL')
16
- g.db.execute('PRAGMA synchronous=NORMAL')
17
- break
18
- except sqlite3.Error as e:
19
- if attempt == max_retries - 1:
20
- raise
21
- time.sleep(2)
22
- return g.db
23
 
24
- def init_db(app):
25
- """Initialize database with tables"""
26
- with app.app_context():
27
- max_retries = 3
28
- for attempt in range(max_retries):
29
- try:
30
- db = get_db()
31
- db.execute('''
32
- CREATE TABLE IF NOT EXISTS news (
33
- id INTEGER PRIMARY KEY AUTOINCREMENT,
34
- title TEXT NOT NULL,
35
- source TEXT NOT NULL,
36
- published TEXT NOT NULL,
37
- url TEXT UNIQUE NOT NULL,
38
- summary TEXT NOT NULL,
39
- content TEXT NOT NULL,
40
- timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
41
- )
42
- ''')
43
- db.execute('CREATE INDEX IF NOT EXISTS idx_timestamp ON news(timestamp)')
44
- db.commit()
45
- break
46
- except sqlite3.Error as e:
47
- if attempt == max_retries - 1:
48
- raise
49
- time.sleep(2)
50
 
51
- def close_db(e=None):
52
- """Close database connection"""
53
- db = g.pop('db', None)
54
- if db is not None:
55
- db.close()
56
 
57
- def cleanup_db():
58
- """Remove old entries and optimize database"""
59
- try:
60
- db = get_db()
61
- # Keep only the last 100 articles
62
- db.execute('DELETE FROM news WHERE id NOT IN (SELECT id FROM news ORDER BY timestamp DESC LIMIT 100)')
63
  db.commit()
64
- db.execute('VACUUM')
65
- except Exception as e:
66
- app.logger.error(f"Database cleanup failed: {str(e)}")
67
 
68
- def cache_news(articles):
69
- """Cache news articles in database"""
70
- try:
71
- db = get_db()
72
- db.execute('BEGIN TRANSACTION')
73
  for article in articles:
74
- db.execute('''
75
- INSERT OR IGNORE INTO news
76
- (title, source, published, url, summary, content)
77
- VALUES (?, ?, ?, ?, ?, ?)
78
- ''', (
79
- article['title'],
80
- article['source'],
81
- article['published'],
82
- article['url'],
83
- article['summary'],
84
- article['content']
85
- ))
 
 
 
86
  db.commit()
87
- except Exception as e:
88
- db.rollback()
89
- raise e
90
 
91
- def get_cached_news(limit=15):
92
- """Get cached news articles"""
93
- try:
94
- db = get_db()
95
- cur = db.execute('SELECT * FROM news ORDER BY timestamp DESC LIMIT ?', (limit,))
96
- return [dict(row) for row in cur.fetchall()]
97
- except Exception as e:
98
- app.logger.error(f"Error fetching cached news: {str(e)}")
99
- return []
 
 
1
  import os
2
+ import sqlite3
3
  from flask import g
 
4
 
5
+ class Database:
6
+ def __init__(self, db_url):
7
+ self.db_url = db_url
8
+ self.conn = None
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ def get_db(self):
11
+ if not self.conn:
12
+ self.conn = sqlite3.connect(self.db_url)
13
+ self.conn.row_factory = sqlite3.Row
14
+ return self.conn
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ def close_db(self):
17
+ if self.conn:
18
+ self.conn.close()
19
+ self.conn = None
 
20
 
21
+ def init_db(self):
22
+ db = self.get_db()
23
+ with open('schema.sql', 'r') as f:
24
+ db.cursor().executescript(f.read())
 
 
25
  db.commit()
 
 
 
26
 
27
+ def cache_news(self, articles):
28
+ db = self.get_db()
 
 
 
29
  for article in articles:
30
+ try:
31
+ db.execute('''
32
+ INSERT OR IGNORE INTO news
33
+ (title, source, published, url, summary, content)
34
+ VALUES (?,?,?,?,?,?)
35
+ ''', (
36
+ article['title'],
37
+ article['source'],
38
+ article['published'],
39
+ article['url'],
40
+ article['summary'],
41
+ article['content']
42
+ ))
43
+ except sqlite3.IntegrityError:
44
+ continue
45
  db.commit()
 
 
 
46
 
47
+ def get_cached_news(self):
48
+ db = self.get_db()
49
+ cur = db.execute('SELECT * FROM news ORDER BY timestamp DESC LIMIT 15')
50
+ return [dict(row) for row in cur.fetchall()]