{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Unix Email\n\n\nCreate a directed graph, allowing multiple edges and self loops, from\na unix mailbox.  The nodes are email addresses with links\nthat point from the sender to the recievers.  The edge data\nis a Python email.Message object which contains all of\nthe email message data.\n\nThis example shows the power of `DiGraph` to hold edge data\nof arbitrary Python objects (in this case a list of email messages).\n\nThe sample unix email mailbox called \"unix_email.mbox\" may be found here:\nhttps://raw.githubusercontent.com/networkx/networkx/master/examples/drawing/unix_email.mbox\n\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Author: Aric Hagberg (hagberg@lanl.gov)\n\n#    Copyright (C) 2005-2017 by\n#    Aric Hagberg <hagberg@lanl.gov>\n#    Dan Schult <dschult@colgate.edu>\n#    Pieter Swart <swart@lanl.gov>\n#    All rights reserved.\n#    BSD license.\n\nimport email\nfrom email.utils import getaddresses, parseaddr\nimport mailbox\nimport sys\n\nimport matplotlib.pyplot as plt\nimport networkx as nx\n\n# unix mailbox recipe\n# see http://www.python.org/doc/current/lib/module-mailbox.html\n\ndef mbox_graph():\n    try:\n        fh = open(\"unix_email.mbox\", 'rb')\n    except IOError:\n        print(\"unix_email.mbox not found\")\n        raise\n\n    mbox = mailbox.UnixMailbox(fh, email.message_from_file)  # parse unix mailbox\n\n    G = nx.MultiDiGraph()  # create empty graph\n\n    # parse each messages and build graph\n    for msg in mbox:  # msg is python email.Message.Message object\n        (source_name, source_addr) = parseaddr(msg['From'])  # sender\n        # get all recipients\n        # see http://www.python.org/doc/current/lib/module-email.Utils.html\n        tos = msg.get_all('to', [])\n        ccs = msg.get_all('cc', [])\n        resent_tos = msg.get_all('resent-to', [])\n        resent_ccs = msg.get_all('resent-cc', [])\n        all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)\n        # now add the edges for this mail message\n        for (target_name, target_addr) in all_recipients:\n            G.add_edge(source_addr, target_addr, message=msg)\n\n    return G\n\nif __name__ == '__main__':\n\n    G = mbox_graph()\n\n    # print edges with message subject\n    for (u, v, d) in G.edges(data=True):\n        print(\"From: %s To: %s Subject: %s\" % (u, v, d['message'][\"Subject\"]))\n\n    pos = nx.spring_layout(G, iterations=10)\n    nx.draw(G, pos, node_size=0, alpha=0.4, edge_color='r', font_size=16, with_labels=True)\n    plt.show()"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }
  ], 
  "metadata": {
    "kernelspec": {
      "display_name": "Python 2", 
      "name": "python2", 
      "language": "python"
    }, 
    "language_info": {
      "mimetype": "text/x-python", 
      "nbconvert_exporter": "python", 
      "name": "python", 
      "file_extension": ".py", 
      "version": "2.7.12", 
      "pygments_lexer": "ipython2", 
      "codemirror_mode": {
        "version": 2, 
        "name": "ipython"
      }
    }
  }
}