1
0

plot-num-msg-by-time.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python2
  2. # -*- coding: UTF-8 -*-
  3. # File: plot-num-msg-by-time.py
  4. # Date: Wed Mar 25 17:44:39 2015 +0800
  5. # Author: Yuxin Wu <ppwwyyxxc@gmail.com>
  6. from wechat.parser import WeChatDBParser
  7. from common.textutil import ensure_unicode
  8. from datetime import timedelta, datetime
  9. import numpy as np
  10. import matplotlib.pyplot as plt
  11. import sys, os
  12. if len(sys.argv) != 3:
  13. sys.exit("Usage: {0} <path to decrypted_database.db> <name>".format(sys.argv[0]))
  14. db_file = sys.argv[1]
  15. name = ensure_unicode(sys.argv[2])
  16. every_k_days = 2
  17. parser = WeChatDBParser(db_file)
  18. msgs = parser.msgs_by_chat[name]
  19. times = [x.createTime for x in msgs]
  20. start_time = times[0]
  21. diffs = [(x - start_time).days for x in times]
  22. max_day = diffs[-1]
  23. width = 20
  24. numbers = range((max_day / width + 1) * width + 1)[::width]
  25. labels = [(start_time + timedelta(x)).strftime("%m/%d") for x in numbers]
  26. plt.xticks(numbers, labels)
  27. plt.xlabel("Date")
  28. plt.ylabel("Number of msgs in k days")
  29. plt.hist(diffs, bins=max_day / every_k_days)
  30. plt.show()
  31. # statistics by hour
  32. # I'm in a different time zone in this period:
  33. #TZ_DELTA = {(datetime(2014, 7, 13), datetime(2014, 10, 1)): -15}
  34. #def real_hour(x):
  35. #for k, v in TZ_DELTA.iteritems():
  36. #if x > k[0] and x < k[1]:
  37. #print x
  38. #return (x.hour + v + 24) % 24
  39. #return x.hour
  40. #hours = [real_hour(x) for x in times]
  41. #plt.ylabel("Number of msgs")
  42. #plt.xlabel("Hour in a day")
  43. #plt.hist(hours, bins=24)
  44. #plt.show()