plot_unix_email.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. """
  3. ==========
  4. Unix Email
  5. ==========
  6. Create a directed graph, allowing multiple edges and self loops, from
  7. a unix mailbox. The nodes are email addresses with links
  8. that point from the sender to the receivers. The edge data
  9. is a Python email.Message object which contains all of
  10. the email message data.
  11. This example shows the power of `DiGraph` to hold edge data
  12. of arbitrary Python objects (in this case a list of email messages).
  13. The sample unix email mailbox called "unix_email.mbox" may be found here:
  14. https://raw.githubusercontent.com/networkx/networkx/master/examples/drawing/unix_email.mbox
  15. """
  16. # Author: Aric Hagberg (hagberg@lanl.gov)
  17. # Copyright (C) 2005-2019 by
  18. # Aric Hagberg <hagberg@lanl.gov>
  19. # Dan Schult <dschult@colgate.edu>
  20. # Pieter Swart <swart@lanl.gov>
  21. # All rights reserved.
  22. # BSD license.
  23. from email.utils import getaddresses, parseaddr
  24. import mailbox
  25. import sys
  26. import matplotlib.pyplot as plt
  27. import networkx as nx
  28. # unix mailbox recipe
  29. # see https://docs.python.org/3/library/mailbox.html
  30. def mbox_graph():
  31. mbox = mailbox.mbox("unix_email.mbox") # parse unix mailbox
  32. G = nx.MultiDiGraph() # create empty graph
  33. # parse each messages and build graph
  34. for msg in mbox: # msg is python email.Message.Message object
  35. (source_name, source_addr) = parseaddr(msg['From']) # sender
  36. # get all recipients
  37. # see https://docs.python.org/3/library/email.html
  38. tos = msg.get_all('to', [])
  39. ccs = msg.get_all('cc', [])
  40. resent_tos = msg.get_all('resent-to', [])
  41. resent_ccs = msg.get_all('resent-cc', [])
  42. all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)
  43. # now add the edges for this mail message
  44. for (target_name, target_addr) in all_recipients:
  45. G.add_edge(source_addr, target_addr, message=msg)
  46. return G
  47. if __name__ == '__main__':
  48. G = mbox_graph()
  49. # print edges with message subject
  50. for (u, v, pen_weight) in G.edges(data=True):
  51. print("From: %s To: %s Subject: %s" % (u, v, pen_weight['message']["Subject"]))
  52. pos = nx.spring_layout(G, iterations=10)
  53. nx.draw(G, pos, node_size=0, alpha=0.4, edge_color='r', font_size=16, with_labels=True)
  54. plt.show()