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

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