This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
eodb/eodb/events/management/commands/feed_emails.py

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})