-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
120 lines (93 loc) · 4.97 KB
/
main.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
import imap_tools as imap
import sqlite3 as sql
import os
from dotenv import load_dotenv
conn = sql.connect('message.db')
cursor = conn.cursor()
load_dotenv()
def getMaxOfUIDs(mailbox: imap.mailbox.MailBox, folder: str) -> str:
mailbox.folder.set(folder)
for msg in mailbox.fetch('ALL'):
first_subjs = int(msg.uid)
for msgs in mailbox.fetch('ALL', reverse=True):
sec_subjs = int(msgs.uid)
return str(sec_subjs - first_subjs)
def get_max_id_of_table(table_name: str) -> str | None:
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,))
table_exists = cursor.fetchone()
if table_exists:
cursor.execute(f"SELECT MAX(id) FROM {table_name}")
max_id = cursor.fetchone()[0]
return str(max_id)
else:
return None
def create_table_if_not_exist(folder: str) -> None:
cursor.execute(f'''
CREATE TABLE IF NOT EXISTS '{''.join(e for e in folder if e.isalnum())}' (
id INTEGER PRIMARY KEY AUTOINCREMENT,
header TEXT,
date TEXT,
sender TEXT,
reply_to TEXT,
recipient TEXT,
uid TEXT,
text TEXT,
html TEXT
)
''')
return None
def insert_message_data_to_db(message_data: dict, folder: str) -> None:
cursor.execute(f'''
INSERT INTO {''.join(e for e in folder if e.isalnum())} (header, date, sender, recipient, uid, text, html)
VALUES (?, ?, ?, ?, ?, ?, ?)
''',(message_data['header'], message_data['date'], message_data['sender'],
message_data['recipient'][0], message_data['uid'], message_data['text'], message_data['html']))
conn.commit()
def generate_message_data(msg: imap.message.MailMessage) -> dict:
return {
'header': msg.subject,
'date': msg.date_str,
'sender': msg.from_,
'reply_to': msg.reply_to,
'recipient': msg.to,
'uid': msg.uid,
'text': msg.text,
'html': msg.html
}
def start():
program_ends = False
while not program_ends:
try:
with imap.MailBox(os.environ['DOMAIN']).login(os.environ['LOGIN'], os.environ['PASSWORD']) as mailbox:
for folder in mailbox.folder.list():
max_id_of_exists_table = get_max_id_of_table(''.join(e for e in folder.name if e.isalnum()))
skip_current_folder = False
max_uid = getMaxOfUIDs(mailbox=mailbox, folder=folder.name)
uid_tuple = [str(1), max_uid]
if max_id_of_exists_table is not None:
uid_tuple = [max_id_of_exists_table, max_uid]
try:
if float(max_id_of_exists_table) > int(max_uid) * 0.8 or max_uid < 1 :
skip_current_folder = True
except:
skip_current_folder = True
if not skip_current_folder:
global_count = int(max_id_of_exists_table) if max_id_of_exists_table is not None else 0
create_table_if_not_exist(folder=folder.name)
mailbox.folder.set(folder=folder.name)
for msg in mailbox.fetch(imap.U(uid_tuple[0], uid_tuple[1])):
try:
print(f'range: {global_count} - {max_uid}')
print('currentMessageSubject:', msg.subject)
print('currentFolder:', folder.name)
message_data = generate_message_data(msg)
insert_message_data_to_db(message_data=message_data, folder=folder.name)
global_count += 1
except Exception as e:
print(f"Ошибка обработки сообщения: {str(e)}")
global_count = 0
program_ends = True
except Exception as e:
print('ERROR:', e)
input()
start()