-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest_history.py
49 lines (40 loc) · 1.1 KB
/
digest_history.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
# Lint as: python2
"""Scan through bash_history and store which commands I use
Saves past results in a JSON dict, digested_history.json
"""
#!/usr/bin/env python2.7
import json
import os
import time
from collections import defaultdict
NOW= int(time.time())
TWELVE_HOURS = 12*60*60
ZERO = int
def main():
command_dict = defaultdict(ZERO)
homedir = os.path.expanduser('~')
with open(homedir+"/.bash_history", 'r') as bash_history:
skip = True
for line in bash_history:
if skip:
skip = False
continue
if line[0] == "#":
try:
then = int(line[1:])
if then < NOW - TWELVE_HOURS:
skip = True
continue
except ValueError:
pass
cmd = line.partition(" ")[0]
command_dict[cmd.strip()] += 1
digested_loc = os.path.join(homedir, "dotfiles", "digested_history.json")
with open(digested_loc, 'r') as f:
old_dict = json.load(f)
for k, v in old_dict.items():
command_dict[k] += v
with open(digested_loc, 'w') as f:
json.dump(command_dict, f, indent=4)
if __name__ == '__main__':
main()