-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
325 lines (252 loc) · 9.37 KB
/
database.py
File metadata and controls
325 lines (252 loc) · 9.37 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
"""
Database module for the application.
"""
import datetime
import email
import hashlib
from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime, BLOB, Index, text
from sqlalchemy.pool import NullPool
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from common import config, logging
Base = declarative_base()
class Email(Base):
"""
Represents an email entity.
Attributes:
id (int): The unique identifier of the email.
sender (str): The sender of the email.
subject (str): The subject of the email.
content (str): The content of the email.
timestamp (datetime): The timestamp when the email was received.
"""
__tablename__ = "emails"
id = Column(Integer, primary_key=True)
receiver = Column(String)
sender = Column(String, index=True)
email_id = Column(Integer, unique=True, index=True)
subject = Column(Text)
content = Column(BLOB)
timestamp = Column(DateTime, default=datetime.datetime.utcnow, index=True)
__table_args__ = (
Index('idx_sender_timestamp', 'sender', 'timestamp'),
)
data_dir = config.get("data_dir", "data")
engine = create_engine(f"sqlite:///{data_dir}/emails.db", poolclass=NullPool)
def migrate_database():
"""
Migrate existing database to add indexes if they don't exist.
This function is safe to run on both new and existing databases.
"""
logging.info("Checking database schema and indexes...")
# Create tables if they don't exist
Base.metadata.create_all(engine)
# Check and create indexes for existing databases
with engine.connect() as conn:
# Get list of existing indexes
result = conn.execute(
text("SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='emails'")
)
existing_indexes = {row[0] for row in result}
# Define required indexes
required_indexes = {
'ix_emails_sender': 'CREATE INDEX IF NOT EXISTS ix_emails_sender ON emails (sender)',
'ix_emails_email_id': 'CREATE UNIQUE INDEX IF NOT EXISTS ix_emails_email_id ON emails (email_id)',
'ix_emails_timestamp': 'CREATE INDEX IF NOT EXISTS ix_emails_timestamp ON emails (timestamp)',
'idx_sender_timestamp': 'CREATE INDEX IF NOT EXISTS idx_sender_timestamp ON emails (sender, timestamp)',
}
# Create missing indexes
for index_name, create_sql in required_indexes.items():
if index_name not in existing_indexes:
logging.info(f"Creating index: {index_name}")
conn.execute(text(create_sql))
conn.commit()
else:
logging.debug(f"Index already exists: {index_name}")
logging.info("Database migration completed successfully")
# Run migration on startup
migrate_database()
Session = sessionmaker(bind=engine)
def save_email(
sender: str,
receiver: str,
email_id: int,
subject: str,
content: bytes,
timestamp: datetime,
):
"""
Save an email to the database.
Args:
sender (str): The sender of the email.
receiver (str): The receiver of the email.
email_id (int): The unique identifier of the email.
subject (str): The subject of the email.
content (bytes): The content of the email.
timestamp (datetime): The timestamp when the email was received.
Returns:
None
"""
with Session() as session:
existing_email = session.query(Email).filter_by(email_id=email_id).first()
if existing_email is None:
email = Email(
sender=sender,
receiver=receiver,
email_id=email_id,
subject=subject,
content=content,
timestamp=timestamp,
)
session.add(email)
session.commit()
else:
print(f"Email with id {email_id} already exists. Discarding.")
def get_email(sender: str) -> list:
"""
Get all emails from a specific sender.
Args:
sender (str): The sender of the emails.
Returns:
list: A list of email objects.
"""
max_item_per_feed = config.get("max_item_per_feed")
with Session() as session:
emails = (
session.query(Email)
.filter_by(sender=sender)
.order_by(Email.timestamp.desc())
.limit(max_item_per_feed)
)
return emails
def get_senders() -> list:
"""
Get all unique senders from the database.
Returns:
list: A list of unique sender email addresses.
"""
with Session() as session:
senders = session.query(Email.sender).distinct().all()
return [sender[0] for sender in senders]
def get_entry_count():
"""
Check if the database is empty.
Returns:
bool: True if the database is empty, False otherwise.
"""
with Session() as session:
return session.query(Email).count()
def get_last_email_id():
"""
Get the last email id from the database.
Returns:
int: The last email id.
"""
with Session() as session:
last_email = session.query(Email).order_by(Email.timestamp.desc()).first()
if last_email:
return last_email.email_id
return 0
def get_email_by_guid(sender: str, guid: str):
"""
Get an email by its sender and GUID.
The GUID is calculated as MD5(subject + date + from) from the email message.
This function queries emails by sender and calculates GUID for each to find a match.
Args:
sender (str): The sender email address
guid (str): The MD5 GUID hash to match
Returns:
Email object if found, None otherwise
"""
with Session() as session:
emails = session.query(Email).filter_by(sender=sender).all()
for email_record in emails:
try:
# Parse the email content BLOB
msg = email.message_from_bytes(email_record.content)
# Calculate GUID using the same logic as feed_generator.py
unique_string = msg["subject"] + msg["date"] + msg["from"]
calculated_guid = hashlib.md5(unique_string.encode()).hexdigest()
# Check if this is the email we're looking for
if calculated_guid == guid:
return email_record
except Exception as e:
# Skip emails that fail to parse
continue
return None
def get_all_emails_with_metadata():
"""
Get all emails from the database with parsed metadata.
Returns:
list: A list of dictionaries containing email metadata:
- sender: sender email address
- subject: email subject
- date: email date
- guid: calculated MD5 GUID
- timestamp: database timestamp
"""
max_item_per_feed = config.get("max_item_per_feed")
with Session() as session:
emails = (
session.query(Email)
.order_by(Email.timestamp.desc())
.limit(max_item_per_feed * 10) # Limit to reasonable number
.all()
)
result = []
for email_record in emails:
try:
msg = email.message_from_bytes(email_record.content)
subject = email.header.make_header(email.header.decode_header(msg["subject"]))
subject_text = str(subject)
date_text = msg["date"]
# Calculate GUID
unique_string = msg["subject"] + msg["date"] + msg["from"]
guid = hashlib.md5(unique_string.encode()).hexdigest()
result.append({
"sender": email_record.sender,
"subject": subject_text,
"date": date_text,
"guid": guid,
"timestamp": email_record.timestamp,
})
except Exception:
continue
return result
def get_emails_by_sender_with_metadata(sender: str):
"""
Get all emails from a specific sender with parsed metadata.
Args:
sender (str): The sender email address
Returns:
list: A list of dictionaries containing email metadata
"""
max_item_per_feed = config.get("max_item_per_feed")
with Session() as session:
emails = (
session.query(Email)
.filter_by(sender=sender)
.order_by(Email.timestamp.desc())
.limit(max_item_per_feed)
.all()
)
result = []
for email_record in emails:
try:
msg = email.message_from_bytes(email_record.content)
subject = email.header.make_header(email.header.decode_header(msg["subject"]))
subject_text = str(subject)
date_text = msg["date"]
# Calculate GUID
unique_string = msg["subject"] + msg["date"] + msg["from"]
guid = hashlib.md5(unique_string.encode()).hexdigest()
result.append({
"sender": email_record.sender,
"subject": subject_text,
"date": date_text,
"guid": guid,
"timestamp": email_record.timestamp,
})
except Exception:
continue
return result