-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·87 lines (73 loc) · 2.77 KB
/
main.py
File metadata and controls
executable file
·87 lines (73 loc) · 2.77 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
#!/usr/bin/env python3
# coding: utf-8
import functions
import config
import email
from email.header import decode_header
import base64
import sys
import time
import asyncio
import traceback
ENCODING = config.encoding
def main():
imap = functions.connection()
if not imap:
sys.exit()
status, messages = imap.select("INBOX") # папка входящие
res, unseen_msg = imap.uid("search", "UNSEEN", "ALL")
unseen_msg = unseen_msg[0].decode(ENCODING).split(" ")
if unseen_msg[0]:
for letter in unseen_msg:
attachments = []
res, msg = imap.uid("fetch", letter, "(RFC822)")
if res == "OK":
msg = email.message_from_bytes(msg[0][1])
msg_date = functions.date_parse(email.utils.parsedate_tz(msg["Date"]))
msg_from = functions.from_subj_decode(msg["From"])
msg_subj = functions.from_subj_decode(msg["Subject"])
if msg["Message-ID"]:
msg_id = msg["Message-ID"].lstrip("<").rstrip(">")
else:
msg_id = msg["Received"]
if msg["Return-path"]:
msg_email = msg["Return-path"].lstrip("<").rstrip(">")
else:
msg_email = msg_from
if not msg_email:
encoding = decode_header(msg["From"])[0][1] # не проверено
msg_email = (
decode_header(msg["From"])[1][0]
.decode(encoding)
.replace("<", "")
.replace(">", "")
.replace(" ", "")
)
letter_text = functions.get_letter_text(msg)
attachments = functions.get_attachments(msg)
post_text = functions.post_construct(
msg_subj, msg_from, msg_email, letter_text, attachments
)
if len(post_text) > 4000:
post_text = post_text[:4000]
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
reply_id = loop.run_until_complete(
functions.send_message(config.bot_key, post_text, config.chat_id)
)
if config.send_attach:
functions.send_attach(msg, msg_subj, reply_id)
imap.logout()
else:
imap.logout()
sys.exit()
if __name__ == "__main__":
try:
main()
except (Exception) as exp:
text = str("ошибка: " + str(exp))
print(traceback.format_exc())
loop = asyncio.get_event_loop()
loop.run_until_complete(
functions.send_message(config.bot_key, text, config.chat_id)
)