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

38 lines
1.3 KiB
Python

import datetime
import email.utils
import mailbox
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, AttributeError):
print('failed to get list id', message['date'], message['subject'])
continue
try:
author_email = email.utils.parseaddr(message['From'])[1]
except TypeError:
continue
author_date = email.utils.parsedate_to_datetime(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})