Beispiel #1
0
 def __get_mails(self, mails_info: dict, folder: Folder):
     mails = mails_info.get('mailMap')
     if mails is None or len(mails) == 0:
         return
     for k, mail in mails.items():
         try:
             mailid = mail.get('messageId')
             msid = mail.get('msId')
             newmail = mail.get('newMail')
             eml = EML(self._clientid, self.task, self._userid, mailid, folder, self.task.apptype)
             if newmail:
                 eml.state = 0
             else:
                 eml.state = 1
             eml.provider = mail.get('sender')
             sendtime = mail.get('sendDate')
             if sendtime is not None and sendtime != '':
                 eml.sendtime = datetime.datetime.fromtimestamp(sendtime // 1000)
             eml.subject = mail.get('subject')
             eml_info = self.__get_mail_streams(mailid, msid)
             eml.io_stream = eml_info[0]
             eml.stream_length = eml_info[1]
             time.sleep(5)  # 防止取邮件的频率过快
             yield eml
         except Exception:
             self._logger.error(f'Get {mailid} email error, err:{traceback.format_exc()}')
             continue
Beispiel #2
0
 def _get_mails(self, folder: Folder) -> iter:
     error_times = 0
     current_page = 1
     all_pagenum = 1000
     mail_url = 'http://m0.mail.sina.com.cn/wa.php?a=list_mail'
     headers = {
         'Accept':
         "*/*",
         'Accept-Encoding':
         "gzip, deflate",
         'Accept-Language':
         "zh-CN,zh;q=0.9,en;q=0.8",
         'Cache-Control':
         "no-cache",
         'Content-Length':
         "98",
         'Content-type':
         "application/x-www-form-urlencoded;charset=UTF-8",
         'Cookie':
         self.task.cookie,
         'Host':
         "m0.mail.sina.com.cn",
         'Origin':
         "http://m0.mail.sina.com.cn",
         'Pragma':
         "no-cache",
         'Proxy-Connection':
         "keep-alive",
         'Referer':
         "http://m0.mail.sina.com.cn/classic/index.php",
         'User-Agent':
         "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
     }
     while True:
         try:
             if current_page >= all_pagenum:
                 break
             payload_data = {
                 'fid': '{}'.format(folder.folderid),
                 'order': 'htime',
                 'sorttype': 'desc',
                 'type': '0',
                 'pageno': '{}'.format(current_page),
                 'tag': '-1',
                 'webmail': '1'
             }
             response = requests.request("POST",
                                         mail_url,
                                         data=payload_data,
                                         headers=headers)
             mail_data_json = json.loads(response.text)
             if not mail_data_json.get('result'):
                 break
             if mail_data_json.get('errno') != 0:
                 break
             mail_data = mail_data_json.get('data')
             all_pagenum = mail_data.get('pagenum')
             maillist = mail_data.get('maillist')
             if len(maillist) == 0:
                 self._logger.info("No mail in mail list")
                 break
             for mail_info in maillist:
                 mailid = mail_info[0]
                 eml = EML(self._clientid, self.task, self._userid, mailid,
                           folder, self.task.apptype)
                 eml.sendtime = datetime.datetime.fromtimestamp(
                     mail_info[4])
                 eml.subject = mail_info[3]
                 eml.provider = mail_info[1]
                 eml.owner = mail_info[2]
                 eml_info = self.__download_mails(folder.folderid, mailid)
                 eml.io_stream = eml_info[0]
                 eml.stream_length = eml_info[1]
                 time.sleep(5)  # 防止取邮件的频率过快
                 yield eml
             if current_page == all_pagenum:
                 break
             else:
                 current_page += 1
         except Exception:
             if error_times >= 5:
                 break
             self._logger.error(
                 f"Get mail or download mail error, err:{traceback.format_exc()}"
             )
             error_times += 1
             continue
Beispiel #3
0
    def parse_mail(self, message, fo: Folder):
        try:
            sj = json.loads(message)
            respMsgs = sj["Body"]["ResponseMessages"]["Items"]
            if not respMsgs:
                yield
        except:
            return

        for msg in respMsgs:
            items = msg["RootFolder"]["Items"]
            for item in items:
                size = 0
                rcvTime = None
                if not item.__contains__('ItemId') or not item.__contains__(
                        'Subject') or not item.__contains__('IsRead'):
                    continue
                if not item['ItemId'].__contains__('Id'):
                    continue
                id = item["ItemId"]["Id"]
                subject = item["Subject"]
                isRead = item["IsRead"]

                if item.__contains__('DateTimeReceived'):
                    # 2019-03-09T00:52:47+08:00
                    rcvTime = item["DateTimeReceived"]
                    rcvTime = datetime.datetime.strptime(
                        rcvTime,
                        '%Y-%m-%dT%H:%M:%S+08:00') + datetime.timedelta(
                            hours=8)
                if item.__contains__('Size'):
                    size = item["Size"]

                # sender
                # if item.__contains__('IsDraft') and not item["IsDraft"]:
                #     if item.__contains__('Sender') and item['Sender'].__contains__('Mailbox'):
                #         if item["Sender"]["Mailbox"].__contains__('EmailAddress'):
                #             sender = item["Sender"]["Mailbox"]["EmailAddress"]
                #         elif item["Sender"]["Mailbox"].__contains__("Name"):
                #             sender = item["Sender"]["Mailbox"]["Name"]
                #         else:
                #             sender = "Unknow"
                #     elif item.__contains__("From") and item["From"].__contains__("Mailbox"):
                #         if item["From"]["Mailbox"].__contains__("EmailAddress"):
                #             sender = item["From"]["Mailbox"]["EmailAddress"]
                #         elif item["From"]["Mailbox"].__contains__("Name"):
                #             sender = item["From"]["Mailbox"]["Name"]
                #         else:
                #             sender = "Unknow"
                #     else:
                #         sender = "Unknow"
                # else:
                #     sender = self._userid

                m = EML(self._clientid, self.task, self._userid, id, fo,
                        self.task.apptype)
                m.subject = subject
                m.sendtime = rcvTime
                m.provider = 'outlook.live.com'
                if isRead:
                    m.state = 1
                else:
                    m.state = 0
                m.stream_length = size
                yield m