|
import asyncio |
|
import poplib |
|
import datetime |
|
from email.parser import Parser |
|
from email.header import decode_header |
|
from email.utils import parseaddr |
|
|
|
|
|
|
|
|
|
|
|
def parse_email(msg, indent): |
|
if indent == 0: |
|
|
|
for header in ['From', 'To', 'Subject', 'Date']: |
|
value = msg.get(header, '') |
|
if value: |
|
if header == 'Subject': |
|
|
|
value = decode_str(value) |
|
if header == 'Date': |
|
value = value |
|
else: |
|
|
|
hdr, addr = parseaddr(value) |
|
name = decode_str(hdr) |
|
value = u'%s <%s>' % (name, addr) |
|
print('%s%s: %s' % (' ' * indent, header, value)) |
|
if (msg.is_multipart()): |
|
|
|
|
|
parts = msg.get_payload() |
|
for n, part in enumerate(parts): |
|
|
|
return parse_email(part, indent + 1) |
|
else: |
|
|
|
|
|
content_type = msg.get_content_type() |
|
if content_type == 'text/plain' or content_type == 'text/html': |
|
|
|
content = msg.get_payload(decode=True) |
|
|
|
charset = guess_charset(msg) |
|
if charset: |
|
global Text |
|
Text = content.decode(charset) |
|
print('%sText: %s' % (' ' * indent, Text)) |
|
else: |
|
|
|
print('%sAttachment: %s' % (' ' * indent, content_type)) |
|
|
|
|
|
|
|
|
|
def decode_str(s): |
|
value, charset = decode_header(s)[0] |
|
if charset: |
|
value = value.decode(charset) |
|
return value |
|
|
|
|
|
|
|
|
|
def guess_charset(msg): |
|
|
|
charset = msg.get_charset() |
|
if charset is None: |
|
|
|
content_type = msg.get('Content-Type', '').lower() |
|
for item in content_type.split(';'): |
|
item = item.strip() |
|
if item.startswith('charset'): |
|
charset = item.split('=')[1] |
|
break |
|
return charset |
|
|
|
|
|
|
|
|
|
async def qq_pop_serve(): |
|
|
|
email_address = "1348006516@qq.com" |
|
|
|
|
|
|
|
email_password = "tfspjbixhevlihfi" |
|
|
|
|
|
pop_server_host = "pop.qq.com" |
|
|
|
pop_server_port = 995 |
|
try: |
|
|
|
email_server = poplib.POP3_SSL( |
|
host=pop_server_host, port=pop_server_port, timeout=10) |
|
print("pop3----connect server success, now will check username") |
|
except: |
|
print("pop3----sorry the given email server address connect time out") |
|
exit(1) |
|
try: |
|
|
|
email_server.user(email_address) |
|
print("pop3----username exist, now will check password") |
|
except: |
|
print("pop3----sorry the given email address seem do not exist") |
|
exit(1) |
|
try: |
|
|
|
email_server.pass_(email_password) |
|
print("pop3----password correct,now will list email") |
|
except: |
|
print("pop3----sorry the given username seem do not correct") |
|
exit(1) |
|
while True: |
|
|
|
email_count = len(email_server.list()[1]) |
|
|
|
resp, mails, octets = email_server.list() |
|
|
|
for i in range(1, len(mails) + 1): |
|
|
|
resp, lines, octets = email_server.retr(i) |
|
|
|
email_content = b'\r\n'.join(lines) |
|
try: |
|
|
|
email_content = email_content.decode('utf-8') |
|
except Exception as e: |
|
print(str(e)) |
|
continue |
|
|
|
|
|
msg = Parser().parsestr(email_content) |
|
print('------------------------------ 华丽分隔符 ------------------------------') |
|
|
|
parse_email(msg, 0) |
|
|
|
|
|
|
|
|
|
global gtime |
|
gtime = msg.get('Date', '') |
|
gtime = gtime.replace(":", ' ') |
|
gtime = gtime.split() |
|
day = int(gtime[1]) |
|
mon = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
|
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
|
month = mon.index(gtime[2]) + 1 |
|
year = int(gtime[3]) |
|
hour = int(gtime[4]) |
|
minute = int(gtime[5]) |
|
second = int(gtime[6]) |
|
|
|
|
|
totime = datetime.datetime(year, month, day, hour, minute, second) |
|
|
|
lasttime = 500000 |
|
if msg.get("From", '') == "TradingView <noreply@tradingview.com>" and (totime-datetime.datetime.now()).seconds <= lasttime: |
|
|
|
print('totime=', totime, 'now=', datetime.datetime.now()) |
|
print('TEXT=', Text) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await asyncio.sleep(1) |
|
|
|
|
|
loop = asyncio.get_event_loop() |
|
loop.run_until_complete( |
|
qq_pop_serve(), |
|
) |
|
|
|
|
|
|