35 lines
1.2 KiB
Python
35 lines
1.2 KiB
Python
import datetime
|
|
import mailbox
|
|
import rfc822
|
|
import time
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from django.utils.dateparse import parse_datetime
|
|
|
|
from eodb.events.models import Email
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument('maildir', nargs='+')
|
|
|
|
def handle(self, *args, **options):
|
|
for maildir in options.get('maildir'):
|
|
box = mailbox.Maildir(maildir, create=False)
|
|
for message_id in box.iterkeys():
|
|
message = box[message_id]
|
|
try:
|
|
list_id = message['list-id'].strip('<>').replace('.listes.entrouvert.com', '')
|
|
except KeyError:
|
|
print 'failed to get list id', message['date'], message['subject']
|
|
continue
|
|
author_email = rfc822.parseaddr(message['From'])[1]
|
|
author_date = datetime.datetime.fromtimestamp(time.mktime(rfc822.parsedate(message['date'])))
|
|
|
|
msg, created = Email.objects.get_or_create(msgid=message_id,
|
|
defaults={
|
|
'list_id': list_id,
|
|
'author_email': author_email,
|
|
'author_datetime': author_date})
|