forked from marcboeker/gmail-to-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
143 lines (117 loc) · 4.02 KB
/
db.py
File metadata and controls
143 lines (117 loc) · 4.02 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
import logging
from datetime import datetime
from peewee import *
from playhouse.sqlite_ext import *
database_proxy = Proxy()
class Message(Model):
"""
Represents an email message.
Attributes:
message_id (TextField): The unique identifier of the message.
thread_id (TextField): The unique identifier of the thread.
sender (JSONField): The sender of the message.
recipients (JSONField): The recipients of the message.
labels (JSONField): The labels of the message.
subject (TextField): The subject of the message.
body (TextField): The last messages sent or received without all other replies to the thread.
size (IntegerField): The size of the message.
timestamp (DateTimeField): The timestamp of the message.
is_read (BooleanField): Indicates whether the message has been read.
is_outgoing BooleanField(): Indicates whether the message was sent by the user.
last_indexed (DateTimeField): The timestamp when the message was last indexed.
Meta:
database (Database): The database connection to use.
db_table (str): The name of the database table for storing messages.
"""
message_id = TextField(unique=True)
thread_id = TextField()
sender = JSONField()
recipients = JSONField()
labels = JSONField()
subject = TextField(null=True)
body = TextField(null=True)
size = IntegerField()
timestamp = DateTimeField()
is_read = BooleanField()
is_outgoing = BooleanField()
last_indexed = DateTimeField()
class Meta:
database = database_proxy
db_table = "messages"
def init(data_dir: str, enable_logging=False) -> SqliteDatabase:
"""
Initialize the database for the given data_dir. The database is stored in <data_dir>/messages.db.
Args:
data_dir (str): The path where to store the data.
enable_logging (bool, optional): Whether to enable logging. Defaults to False.
Returns:
SqliteDatabase: The initialized database object.
"""
db = SqliteDatabase(f"{data_dir}/messages.db")
database_proxy.initialize(db)
db.create_tables([Message])
if enable_logging:
logger = logging.getLogger("peewee")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
return db
def create_message(msg: Message):
"""
Saves a message to the database.
Args:
msg (Message): The message to save.
"""
last_indexed = datetime.now()
Message.insert(
message_id=msg.id,
thread_id=msg.thread_id,
sender=msg.sender,
recipients=msg.recipients,
labels=msg.labels,
subject=msg.subject,
body=msg.body,
size=msg.size,
timestamp=msg.timestamp,
is_read=msg.is_read,
is_outgoing=msg.is_outgoing,
last_indexed=last_indexed,
).on_conflict(
conflict_target=[Message.message_id],
preserve=[
Message.thread_id,
Message.sender,
Message.recipients,
Message.subject,
Message.body,
Message.size,
Message.timestamp,
Message.is_outgoing,
],
update={
Message.is_read: msg.is_read,
Message.last_indexed: last_indexed,
Message.labels: msg.labels,
},
).execute()
def last_indexed() -> datetime:
"""
Returns the timestamp of the last indexed message.
Returns:
datetime: The timestamp of the last indexed message.
"""
msg = Message.select().order_by(Message.timestamp.desc()).first()
if msg:
return datetime.fromisoformat(msg.timestamp)
else:
return None
def first_indexed() -> datetime:
"""
Returns the timestamp of the first indexed message.
Returns:
datetime: The timestamp of the first indexed message.
"""
msg = Message.select().order_by(Message.timestamp.asc()).first()
if msg:
return datetime.fromisoformat(msg.timestamp)
else:
return None