Browse Source

[REF] _fetch_from_date_imap

pull/932/head
Lorenzo Battistini 10 years ago
committed by Jordi Ballester
parent
commit
584ee18714
  1. 26
      fetchmail_bydate/model/fetchmail.py

26
fetchmail_bydate/model/fetchmail.py

@ -36,10 +36,10 @@ class FetchmailServer(orm.Model):
_inherit = "fetchmail.server" _inherit = "fetchmail.server"
_columns = { _columns = {
'last_download_date': fields.datetime('Last Download Date'),
'last_internal_date': fields.datetime('Last Download Date'),
} }
def _fetch_from_data_imap(self, cr, uid,
def _fetch_from_date_imap(self, cr, uid,
server, imap_server, server, imap_server,
mail_thread, action_pool, mail_thread, action_pool,
count, failed, count, failed,
@ -47,23 +47,23 @@ class FetchmailServer(orm.Model):
messages = [] messages = []
date_uids = {} date_uids = {}
last_date = False last_date = False
last_download_date = datetime.strptime(
server.last_download_date, "%Y-%m-%d %H:%M:%S")
last_internal_date = datetime.strptime(
server.last_internal_date, "%Y-%m-%d %H:%M:%S")
search_status, uids = imap_server.search( search_status, uids = imap_server.search(
None, None,
'SINCE', '%s' % last_download_date.strftime('%d-%b-%Y')
'SINCE', '%s' % last_internal_date.strftime('%d-%b-%Y')
) )
new_uids = uids[0].split() new_uids = uids[0].split()
for new_uid in new_uids: for new_uid in new_uids:
fetch_status, data = imap_server.fetch(
fetch_status, date = imap_server.fetch(
int(new_uid), int(new_uid),
'INTERNALDATE' 'INTERNALDATE'
) )
internaldate = imaplib.Internaldate2tuple(data[0])
internaldate = imaplib.Internaldate2tuple(date[0])
internaldate_msg = datetime.fromtimestamp( internaldate_msg = datetime.fromtimestamp(
time.mktime(internaldate) time.mktime(internaldate)
) )
if internaldate_msg > last_download_date:
if internaldate_msg > last_internal_date:
messages.append(new_uid) messages.append(new_uid)
date_uids[new_uid] = internaldate_msg date_uids[new_uid] = internaldate_msg
for num in messages: for num in messages:
@ -71,7 +71,6 @@ class FetchmailServer(orm.Model):
# recent message, even if it has already been synced # recent message, even if it has already been synced
res_id = None res_id = None
result, data = imap_server.fetch(num, '(RFC822)') result, data = imap_server.fetch(num, '(RFC822)')
imap_server.store(num, '-FLAGS', '\\Seen')
if data and data[0]: if data and data[0]:
try: try:
res_id = mail_thread.message_process( res_id = mail_thread.message_process(
@ -105,15 +104,13 @@ class FetchmailServer(orm.Model):
return count, failed, last_date return count, failed, last_date
def fetch_mail(self, cr, uid, ids, context=None): def fetch_mail(self, cr, uid, ids, context=None):
"""WARNING: meant for cron usage only -
will commit() after each email!
"""
if context is None: if context is None:
context = {} context = {}
context['fetchmail_cron_running'] = True context['fetchmail_cron_running'] = True
mail_thread = self.pool.get('mail.thread') mail_thread = self.pool.get('mail.thread')
action_pool = self.pool.get('ir.actions.server') action_pool = self.pool.get('ir.actions.server')
for server in self.browse(cr, uid, ids, context=context): for server in self.browse(cr, uid, ids, context=context):
if server.type == 'imap' and server.last_internal_date:
_logger.info( _logger.info(
'start checking for new emails by date on %s server %s', 'start checking for new emails by date on %s server %s',
server.type, server.name) server.type, server.name)
@ -122,11 +119,10 @@ class FetchmailServer(orm.Model):
count, failed = 0, 0 count, failed = 0, 0
last_date = False last_date = False
imap_server = False imap_server = False
if server.type == 'imap' and server.last_download_date:
try: try:
imap_server = server.connect() imap_server = server.connect()
imap_server.select() imap_server.select()
count, failed, last_date = self._fetch_from_data_imap(
count, failed, last_date = self._fetch_from_date_imap(
cr, uid, server, imap_server, mail_thread, cr, uid, server, imap_server, mail_thread,
action_pool, count, failed, context=context action_pool, count, failed, context=context
) )
@ -147,7 +143,7 @@ class FetchmailServer(orm.Model):
%d succeeded, %d failed.", count, %d succeeded, %d failed.", count,
server.type, server.name, server.type, server.name,
(count - failed), failed) (count - failed), failed)
vals = {'last_download_date': last_date}
vals = {'last_internal_date': last_date}
server.write(vals) server.write(vals)
return super(FetchmailServer, self).fetch_mail( return super(FetchmailServer, self).fetch_mail(
cr, uid, ids, context=context) cr, uid, ids, context=context)
Loading…
Cancel
Save