1
from paths import data_dir
7
UPDATE_INTERVAL = 60 * 60
9
def needs_update(filename):
10
data_file = os.path.join(data_dir, filename)
11
return (not os.path.exists(data_file)) or \
12
(time.time()-os.path.getctime(data_file) >= UPDATE_INTERVAL)
14
def read_lines(filename, path=None):
18
data_file = os.path.join(dirname, filename)
19
lines = open(data_file, "r").readlines()
20
return map(lambda a: a.strip(), lines)
22
def read_data(filename=None, fd=None):
26
data_file = os.path.join(data_dir, filename)
27
if not os.path.exists(data_file):
30
return json.loads(fd.read())
32
def sanitise_data(data):
34
if type(entry) in [ list, tuple ]:
35
# sometimes we get "None" back from LP
37
if what in [ None, "None"]:
38
entry_index = data.index(entry)
39
what_index = data[entry_index][1].index(what)
40
data[entry_index][1][what_index] = data[entry_index-1][1][what_index]
43
def dump(filename, data):
44
data_file = os.path.join(data_dir, filename)
45
if os.path.exists(data_file):
48
if not os.path.exists(data_dir):
50
f = open(data_file, 'a')
51
f.write(json.dumps(data))
54
def add_stats(filename, what, timestamp=None, unique=False):
58
timestamp = int(time.time())
59
data = read_data(filename)
61
if type(what) not in [ tuple, list ]:
64
if unique and data and data[-1][1] == what:
65
data[-1][0] = timestamp
67
data += [[timestamp, what]]
68
data = sanitise_data(data)