Demo entry 6357661

get_mail.py

   

Submitted by anonymous on Apr 23, 2017 at 15:42
Language: Python 3. Code size: 3.0 kB.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

import os
import re
import poplib
import base64
import urllib2
from email.parser import Parser
from email.header import decode_header


class Inbox:
    def __init__(self):
        self.conn = poplib.POP3_SSL('pop.qq.com')
        self.conn.user('3208280852@qq.com')
        self.conn.pass_('xxxxxx')  # your pop keys
        self.mails = []

    def get_charset(self, msg):
        charset = msg.get_charset()
        if charset is None:
            content_type = msg.get('Content-Type', '').lower()
            pos = content_type.find('charset=')
            if pos >= 0:
                charset = content_type[pos + 8:].strip()
        return charset

    def decode_str(self, s):
        value, charset = decode_header(s)[0]
        if charset:
            value = value.decode(charset)
        return value

    def fetch(self):
        resp, mails, octets = self.conn.uidl()
        mail_ids = [mail.split(' ')[1] for mail in mails]
        for mail_id in mail_ids:
            pos = mail_ids.index(mail_id) + 1
            resp, lines, octets = self.conn.retr(pos)
            self.mails.append('\r\n'.join(lines))
        self.conn.quit()
        for mail in self.mails:
            self.down(mail)

    def down(self, mail):
        msg = Parser().parsestr(mail)
        for part in msg.walk():
            if not part.is_multipart() and part.get_content_type() == 'application/octet-stream':
                filename = self.decode_str(part.get_filename())
                with open(r'down/%s' % filename, 'wb') as f:
                    f.write(base64.decodestring(part.get_payload()))
                    print('down %s done.' % filename)
            elif not part.is_multipart() and part.get_content_type() in ('text/plain', 'text/html'):
                    content = part.get_payload(decode=True)
                    charset = self.get_charset(part)
                    if charset:
                        content = content.decode(charset)
                    if r'ftnExs_download' in content:
                        pattern = re.compile(ur'([\S]+).*\(.*到期\).*进入下载页面:(\S+)')
                        match = pattern.findall(content)
                        if match:
                            self.down_ftnexs(match)

    def down_ftnexs(self, ftnexs):
        for filename, rurl in ftnexs:
            pattern = re.compile(ur'<a un="down".*?href="(?P<url>\S+)" target=')
            match = pattern.search(urllib2.urlopen(rurl).read())
            if match:
                url = match.groupdict().get('url').strip()
                with open(r'down/%s' % filename, 'wb') as f:
                    f.write(urllib2.urlopen(url).read())
                    print('down %s done.' % filename)

if __name__ == '__main__':
    poplib._MAXLINE = 20480
    if not os.path.exists('down'):
        os.mkdir('down')
    inbox = Inbox()
    inbox.fetch()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).