-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSQL_cache.py
More file actions
92 lines (85 loc) · 2.82 KB
/
SQL_cache.py
File metadata and controls
92 lines (85 loc) · 2.82 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sqlite3
from os.path import exists
import sys
from .log_helper import stdout_write, write_progressbar
class Database():
"""Class working with SQLite3 database"""
def __init__(self):
super(Database, self).__init__()
if not exists("cache.db"):
conn = sqlite3.connect("cache.db")
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE `feed` (`source` text unique, `name` text)
""")
cursor.execute("""
CREATE TABLE "news" ( `source` text, `date` text,
`title` text, `link` text UNIQUE,
`description` text, `links` text )
""")
conn.commit()
conn.close()
self.conn = None
self.cursor = None
def _open(self):
self.conn = sqlite3.connect("cache.db")
self.cursor = self.conn.cursor()
def _close(self):
self.conn.close()
def write_data(self, data, feed, url, verbose, color):
"""Write news to database
Params:
data: turple - article data
feed: str - rss_channel feed
url: str
verbose: bool
"""
try:
self._open()
counter = 0
if verbose:
write_progressbar(len(data)+1, counter)
for news in data:
self.cursor.execute("""
INSERT INTO news
VALUES (?,?,?,?,?,?)
""", news)
counter += 1
if verbose:
write_progressbar(len(data)+1, counter)
self.conn.commit()
self.cursor.execute("""
INSERT INTO feed
VALUES (?,?)
""", (url, feed))
self.conn.commit()
except sqlite3.IntegrityError:
pass
except sqlite3.DatabaseError:
stdout_write("Database error", color="red", colorize=color)
finally:
self._close()
counter = len(data)+1
if verbose:
write_progressbar(len(data)+1, counter)
def read_data(self, url, date, color):
"""Get url & date
Return feed & data
"""
feed, data = None, None
try:
self._open()
self.cursor.execute(f"""
SELECT name from feed WHERE source = '{url}'
""")
feed = self.cursor.fetchall()
self.cursor.execute(f"""
SELECT * from news WHERE source = '{url}' and date = '{date}'
""")
data = self.cursor.fetchall()
except Exception as e:
stdout_write(f"Database reading error {e}", color="red", colorize=color)
sys.exit()
finally:
self._close()
return feed, data