-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.py
70 lines (59 loc) · 1.68 KB
/
export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/python
# encoding: utf-8
#
# Copyright (c) 2018 Mark Koester mark@int3c.com
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2018-06
#
"""
Export Word Counter Stats.
Usage:
export.py
"""
import subprocess
import sys
import os
import plistlib
import time
import datetime as datetime
import csv
def add(x, y): return x + y
# read PLIST
records = os.path.expanduser('~/Library/Application Support/WordCounter/app_records.plist')
data = plistlib.readPlist(records)
filepath = os.path.expanduser('~/Documents/WordCounter/export.csv')
directory = os.path.dirname(filepath)
if not os.path.exists(directory):
os.makedirs(directory)
def notify(title, text):
os.system("""
osascript -e 'display notification "{}" with title "{}"'
""".format(text, title))
with open(filepath, 'w') as file:
outputWriter = csv.writer(file)
# add header to csv
outputWriter.writerow(['date', 'wordcount'])
results = ''
# colllect stats for each date key
for date in data.keys():
# collect per-app totals
apps = data[date]
counts = []
for app in apps:
count = reduce(add, app["counts"])
counts.append(count)
# Uncomment to print per-app count
# print app["id"] + ": " + str(count)
# compute total from collection
total = reduce(add, counts)
row = date + "," + str(total)
# print row
outputWriter.writerow([date, total])
results = results + date + "," + str(total) + '\n'
totalrows = len(data.keys())
# file.close()
# return results
message = "%s Stats Rows Exported to Documents/WordCounter." % (totalrows)
notify("Word Counter Stats: Succesfully Exported", message)