1
1
import os
2
2
import sqlite3
3
3
from sqlite3 import Error
4
+ import sys
4
5
5
6
import _logging_ as _log
6
7
import _config_ as _cfg
@@ -29,7 +30,7 @@ def create_schema_messages(db):
29
30
Args:
30
31
db: Local db connection object.
31
32
"""
32
- _log .logger .debug ("creating table message " )
33
+ _log .logger .debug ("creating table messages " )
33
34
cursorObj = db .cursor ()
34
35
cursorObj .execute ("""CREATE TABLE IF NOT EXISTS messages(
35
36
id text,
@@ -50,6 +51,23 @@ def create_schema_messages(db):
50
51
db .commit ()
51
52
52
53
54
+ def create_schema_labels (db ):
55
+ """Creates 'labels' schema on given DB connection.
56
+
57
+ Args:
58
+ db: Local db connection object.
59
+ """
60
+ _log .logger .debug ("creating table labels" )
61
+ cursorObj = db .cursor ()
62
+ cursorObj .execute ("""CREATE TABLE IF NOT EXISTS labels(
63
+ name text,
64
+ label blob,
65
+ CONSTRAINT name_pk PRIMARY KEY (name))""" )
66
+ cursorObj .execute ("""CREATE INDEX IF NOT EXISTS idx_labels_name
67
+ ON labels (name);""" )
68
+ db .commit ()
69
+
70
+
53
71
def sender_of_message (message ):
54
72
try :
55
73
return [item ['value' ] for item in message ['payload' ]['headers' ] if item ['name' ] == 'From' ][0 ]
@@ -78,13 +96,26 @@ def date_of_message(message):
78
96
return ""
79
97
80
98
99
+ def message_exists (db , message_id ):
100
+ sql_stmt = """SELECT * FROM messages WHERE id = '%s';""" % (message_id )
101
+ cursorObj = db .cursor ()
102
+ result = cursorObj .execute (sql_stmt )
103
+ del cursorObj
104
+ if result .fetchone () == None :
105
+ return False
106
+ return True
107
+
108
+
81
109
def add_message (db , message ):
82
- """Get and Delete a list of messages from GMail by Query .
110
+ """Persist a message from GMail if doesn't already exists locally .
83
111
84
112
Args:
85
113
db: Local db connection object.
86
114
message: GMail message JSON object as read by get api.
87
115
"""
116
+ if message_exists (db , message ['id' ]):
117
+ _log .logger .info ("message:%s already exists, skipping db entry" % (message ['id' ]))
118
+ return
88
119
_log .logger .debug ("[+] adding message: %s" % (message ['id' ]))
89
120
90
121
cursorObj = db .cursor ()
@@ -113,12 +144,60 @@ def add_message(db, message):
113
144
subject_of_message (message )))
114
145
_log .logger .debug ("---------------------" )
115
146
db .commit ()
147
+ del cursorObj
148
+ return True
116
149
except :
117
150
_log .logger .error ("failed to insert for %s" % (message ['id' ]))
118
- sys .exit (0 )
151
+ return False
152
+
153
+
154
+ def label_exists (db , label_name ):
155
+ sql_stmt = """SELECT * FROM labels WHERE name = '%s';""" % (label_name )
156
+ cursorObj = db .cursor ()
157
+ result = cursorObj .execute (sql_stmt )
158
+ del cursorObj
159
+ if result .fetchone () == None :
160
+ return False
161
+ return True
162
+
163
+
164
+ def add_label (db , label ):
165
+ """Persist a label from GMail if doesn't already exists locally.
166
+
167
+ Args:
168
+ db: Local db connection object.
169
+ label: GMail label
170
+ """
171
+ label_name = label ['name' ]
172
+
173
+ if label_exists (db , label_name ):
174
+ _log .logger .info ("label:%s already exists, skipping db entry" % (label_name ))
175
+ return
119
176
177
+ _log .logger .debug ("[+] adding label: %s" % (label_name ))
120
178
121
- def dbpath_by_year (year ):
179
+ cursorObj = db .cursor ()
180
+ sql_stmt = """INSERT INTO labels
181
+ (name, label)
182
+ VALUES (?,?)"""
183
+ values = (
184
+ label_name ,
185
+ str (label ),
186
+ )
187
+
188
+ try :
189
+ cursorObj .execute (sql_stmt , values )
190
+ _log .logger .debug ("adding label: %s" % (label_name ))
191
+ _log .logger .debug ("---------------------" )
192
+ db .commit ()
193
+ del cursorObj
194
+ return True
195
+ except :
196
+ _log .logger .error ("failed to insert for %s" % (label_name ))
197
+ return False
198
+
199
+
200
+ def dbpath_by_token (basename , token ):
122
201
"""Returns DB path generated based on year.
123
202
124
203
Args:
@@ -127,7 +206,7 @@ def dbpath_by_year(year):
127
206
Returns:
128
207
DB filesystem path.
129
208
"""
130
- db_name = "gmail-to-delete-%d .db" % (year )
209
+ db_name = "%s-%s .db" % (str ( basename ), str ( token ) )
131
210
return os .path .join (_cfg .data_basepath (), db_name )
132
211
133
212
@@ -140,4 +219,16 @@ def connection_by_year(year):
140
219
Returns:
141
220
Local db connection object.
142
221
"""
143
- return sql_connection (dbpath_by_year (year ))
222
+ return sql_connection (dbpath_by_token ("gmail-to-delete" , year ))
223
+
224
+
225
+ def connection_by_feature (feature ):
226
+ """Returns SQLite3 connection to db created at path for given year.
227
+
228
+ Args:
229
+ feature: Name of feature/construct; like mails, labels, spams, etc
230
+
231
+ Returns:
232
+ Local db connection object.
233
+ """
234
+ return sql_connection (dbpath_by_token ("gmail" , feature ))
0 commit comments