-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
147 lines (112 loc) · 3.55 KB
/
database.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
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
"""
Database module for the application.
"""
import datetime
from sqlalchemy import create_engine, Column, Integer, String, Text, DateTime, BLOB
from sqlalchemy.pool import NullPool
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from common import config
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)
email_id = Column(Integer)
subject = Column(Text)
content = Column(BLOB)
timestamp = Column(DateTime, default=datetime.datetime.utcnow)
data_dir = config.get("data_dir", "data")
engine = create_engine(f"sqlite:///{data_dir}/emails.db", poolclass=NullPool)
Base.metadata.create_all(engine)
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.asc())
.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