-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
108 lines (81 loc) · 2.44 KB
/
db.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
db.py
~~~~~
Sqlite3 database wrapper for download history.
The database stores information on downloaded images to prevent
downloading images more than once.
"""
import logging
import os.path
import sqlite3
from utils import data_dir
logger = logging.getLogger(__name__)
class DownloadDatabase:
"""Configure and establish connection to tweet database.
Note:
This class acts as a context manager.
Args:
db_filename (`str`, optional): filename of database
"""
def __init__(
self, db_filename=os.path.join(data_dir(), 'wikiwall.db'), tablename='downloads'
):
self.db_filename = db_filename
self.tablename = tablename
def __enter__(self):
self._connect()
self._create_table()
return self
def __exit__(self, exception_type, exception_value, traceback):
if self.conn:
self.conn.commit()
self.conn.close()
def _connect(self):
"""Connect to database. """
try:
self.conn = sqlite3.connect(self.db_filename)
except sqlite3.Error:
logger.exception('Failed to connect to database!')
def _create_table(self):
"""Create a table for image data if it does not exist.
Args:
name (`str`, optional): name of table
"""
query = '''
CREATE TABLE IF NOT EXISTS {} (
id integer PRIMARY KEY,
url text NOT NULL UNIQUE)
'''.format(
self.tablename
)
self.conn.execute(query)
def add(self, url):
"""Add image url to database.
Args:
url (str): image url
Raises:
sqlite3.IntegrityError: If data already exists in database.
"""
record_sql = '''
INSERT INTO {} (url)
VALUES (?)
'''.format(
self.tablename
)
try:
with self.conn:
self.conn.execute(record_sql, (url,))
except sqlite3.IntegrityError:
logger.exception('Already tweeted %s!', url)
def is_duplicate(self, url):
"""Check if `url` already exists in database.
Args:
url(`str`): url of image
"""
dupl_check_sql = '''
SELECT url FROM {} WHERE url=?
'''.format(
self.tablename
)
with self.conn:
return self.conn.execute(dupl_check_sql, (url,)).fetchone()