Skip to content

Instantly share code, notes, and snippets.

@kleinschnitker
Created July 1, 2012 10:09
Show Gist options
  • Select an option

  • Save kleinschnitker/3027795 to your computer and use it in GitHub Desktop.

Select an option

Save kleinschnitker/3027795 to your computer and use it in GitHub Desktop.
Analyze mails in Thunderbird profile
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------------------
#----- Ich mache ne Faxe wie hier:
#----- http://blog.stephenwolfram.com/2012/03/the-personal-analytics-of-my-life/
#-----
#----- Anpassung:
#----- 2012-06-28 - Micha (mail@kleinschnitker.com)
#----- Alle mboxen werden rekursiv duchgegangen und analysiert
#----- Beispielaufruf: analyzeMails.py ~/.thunderbird/xxxxxx.default/ImapMail/xxRootMBox
#-----
#----- 2012-07-01 mk Veraenderung der Namensgebung der Ausgabedatei
#----- Add plotting feature (does not work yet)
#-----
#-----
#-----
#----- Lizenz: GPL
#------------------------------------------------------------------------------------------
import sys
import os
import string
import sys
import time
import datetime
from mailbox import mbox as MBox
from email.utils import parsedate
import matplotlib.pyplot as plt
import pylab
mBoxes = [] # Pfade aller mBoxen
xValues = []
yValues = []
''' Analysiert alles Mails in der uebergebenen mbox und schreibt die
Ergebnisse in Datei'''
def analyzeMBox(dir, file):
noOfMails = 0
mbox = MBox(dir)
for msg in mbox:
noOfMails = noOfMails + 1
try: # bei mir sind manche Mails kaputt, darum vorher testen...
maildate = time.strftime('%Y-%m-%d', parsedate(msg.get("date")))
xValues.append(maildate)
mailtime = time.strftime('%H:%M:%S', parsedate(msg.get("date")))
hour = time.strftime('%H', parsedate(msg.get("date")))
minute = time.strftime('%M', parsedate(msg.get("date")))
second = time.strftime('%S', parsedate(msg.get("date")))
yValues.append(float(hour*3600 + minute*60 + second))
except:
file.write(str('NA,NA\n')) # wenn kaputt, erzeuge NAs (missing values) fuer GNU_R
else:
file.write(str("%s,%s\n") % (maildate, mailtime))
return noOfMails # Anzahl der Mails in dieser mBox
''' Findet rekursiv alle mBoxen vom uebergebenen Verzeichnis aus'''
def findMBoxes(dir):
join = os.path.join
for cur in os.listdir(dir): # Fuer jedes Element im aktuellen Verzeichnis
pathname = join(dir, cur) # Fuege Pfadnamen zusammen
if os.path.isdir(pathname): # Pruefen, ob aktueller Pfadname ein Verzeichnis ist
if string.find(cur, '.sbd') != -1: # Finde alle Verzeichnisse, die mit sbd enden
findMBoxes(pathname) # Funktion rekursiv aufrufen
elif os.path.isfile(pathname): # Aktuelles Element ist eine Datei
if(string.find(cur, '.') == -1): # Wenn aktuelles Element keinen Punkt enthaelt
mBoxes.append(pathname) # Verzeichnisname zur Liste hinzufuegen
''' Extrahiere den Mailboxnamen aus dem gesamten Pfadnamen'''
def getRootMailBoxName(pathname):
mailbox = pathname.split('/')
return mailbox[-1]
def main():
numberOfMails = 0 # Anzahl der Mails
findMBoxes(sys.argv[1]) # Finde alle mBoxen
mailboxName = getRootMailBoxName(sys.argv[1]) # Bezeichnung der WurzelMailbox
mBoxes.sort() # mBoxen sortieren
print('Mailbox enthaelt %d mBoxes' % len(mBoxes))
dest = ("mBox-Summary-%s.txt" % (mailboxName) ) # Name der Ziel-Datei
file = open(dest,"w") # Oeffne Datei zum Schreiben
for i in mBoxes: # Fuer jede mBox
print(i)
numberOfMails = numberOfMails + analyzeMBox(i,file) # Analysiere die Mails in der mBox
file.close() # Schliesse Datei
print('%d Mails analysiert' % numberOfMails)
if __name__ == "__main__":
main()
## END OF FILE
@produnis

produnis commented Jul 1, 2012

Copy link
Copy Markdown

ich bekomme immer folgenden fehler:

produnis@Legolas:~/scriptz$ ./analyzeMails.py ~/.thunderbird/kxxxx.default/ImapMail/imap.produnis.de
from: can't read /var/mail/mailbox
from: can't read /var/mail/email.utils
./analyzeMails.py: Zeile 26: mBoxes: Kommando nicht gefunden.
./analyzeMails.py: Zeile 31: Syntaxfehler beim unerwarteten Wort ('./analyzeMails.py: Zeile 31:def analyzeMBox(dir, file):'

@kleinschnitker

Copy link
Copy Markdown
Author

Auf welchem System mit welcher Python Version versuchst du das Skript zu starten?

Wie genau startest du das Skript?

python analyzeMail.py oder ./analyzeMail.py

@produnis

produnis commented Jul 1, 2012

Copy link
Copy Markdown

ubuntu 12.04 mit python 2.7.3

Aufruf erfolgt wie oben beschrieben:
/Pfad/zu/analyzeMails.py ~/.thunderbird/kxxxx.default/ImapMail/imap.produnis.de

habe aber auch schon
/Pfad/zu/analyzeMails.py ~/.thunderbird/kxxxx.default/ImapMail/imap.produnis.de/
versucht (also mit nem SLASH am Ende)

@kleinschnitker

Copy link
Copy Markdown
Author

Kannst du in einer Python2 Konsole bitte folgende Zeilen ausführen:

from mailbox import mbox as MBox
from email.utils import parsedate

Funktionieren die Imports ohne Probleme?

@produnis

produnis commented Jul 1, 2012

Copy link
Copy Markdown

Problem gelöst...
zum einen hatte ich noch falsche zeichencodes im script (die kamen vom copy-paste aus dem browser arrgsss), die den ersten fehler verursacht haben.

zum anderen sollte man im terminal zunächste in das Mailboxhauptverzeichnis wechseln, und das Scrupt dannn so: aufrufen

/Pfad/zu/analyzeMails.py .

Jetzt klappt es
;)

@kleinschnitker

Copy link
Copy Markdown
Author

Perfekt. Ich habe das Skript aber auch nochmal angepasst.
Jetzt kannst du es auch ausserhalb des Mailbox WurzelVerzeichnisses ausrufen. Die Ausgabedatei enthaelt jetzt das Wurzelverzeichnis der Mailbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment