-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimeline.py
More file actions
168 lines (139 loc) · 4.59 KB
/
timeline.py
File metadata and controls
168 lines (139 loc) · 4.59 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from nostalgia import register_modules
import json
import time
import os
import numpy as np
import pandas as pd
from flask import Flask, request, make_response, redirect
from flask_cors import CORS
from plot_graph import plot_graph
from nostalgia.ndf import Results, registry
from threading import Thread
res = None
last = None
app = Flask(__name__)
app.secret_key = os.urandom(12)
CORS(app)
loaded = set()
def reload_periodically():
while True:
time.sleep(60 * 60)
for r in registry.values():
r.load()
tr = Thread(target=reload_periodically, daemon=True)
tr.start()
@app.route("/image")
def image():
global last
index = request.values["index"]
html = request.values.get("html")
for i in range(10):
print("image", index, html)
if index == "undefined":
return None
origin = last.get_original(int(index))
fname = origin.get("image", origin.get("path", origin.get("url")))
if not fname or fname.endswith("mp4"):
return None
fname = fname.replace("file://", "")
if html == "1":
return '<html><body><img src="/image?index={}" /></body></html>'.format(index)
if fname.startswith("http"):
return redirect(fname)
with open(fname, "rb") as f:
img = f.read()
resp = make_response(img)
ext = fname.split(".")[-1]
resp.headers = {"Content-Type": "application/" + ext}
return resp
# "http://localhost:5551/?start=2019-09-25%2014:00&end=2019-09-25%2020:00"
@app.route("/")
def root():
global last, res
if res is None:
register_modules()
res = Results.merge(*[v for k, v in registry.items() if v.df_name != "results"])
start = request.values.get("start")
end = request.values.get("end")
containing = request.values.get("containing") or request.values.get("q")
allowed_types = [k for k, v in request.values.items() if k not in ["start", "end"] and v == "on"]
if allowed_types:
new = False
for tp in allowed_types:
if tp not in loaded:
print("loading", tp)
t1 = time.time()
registry[tp].load()
print("loaded", tp, "in", time.time() - t1)
loaded.add(tp)
new = True
if new:
res = Results.merge(*[v for k, v in registry.items() if v.df_name != "results"])
print("Loaded")
if start is not None and end is not None:
last = res.at_time(start, end)
elif start is not None:
last = res.at_time(start)
else:
last = res
if containing is not None:
last = last.containing(containing)
if allowed_types:
last = last[last.type.isin(allowed_types)]
else:
allowed_types = list(registry.keys())
num_results = last.shape[0]
last = last.sort_values("end")
if not last.empty:
max_n = 600 / len(last.type.unique())
last["sel"] = False
for name, group in last.groupby("type"):
mod = 1 if group.shape[0] < max_n else int(group.shape[0] / max_n)
try:
last.loc[last.type == name, "sel"] = [x % mod == 0 for x in range(group.shape[0])]
except Exception as e:
print(e)
import pdb
pdb.set_trace()
last = Results(last[last.sel])
else:
last = Results(last)
# last = Results(last.tail(1000))
last.add_heartrate()
return plot_graph(last, allowed_types, num_results, start or "365 days ago", end or "1 days ago")
@app.route("/sample")
def sample():
global res
from nostalgia.sources.ing_banking.mijn_ing import Payments
from nostalgia.sources.fitbit.heartrate import FitbitHeartrate
from nostalgia.sources.google.gmail import Gmail
# from nostalgia.sources.chrome_history import WebHistory
dfs = [x.load_sample_data() for x in [Payments, FitbitHeartrate, Gmail]]
res = Results.merge(*dfs)
return redirect("/")
def safe_convert(x):
if isinstance(x, pd.Timestamp):
x = str(x)
if "int64" in str(type(x)):
x = int(x)
if "bool_" in str(type(x)):
x = bool(x)
return x
@app.route("/info")
def info():
global last
index = request.values["index"]
if index == "undefined":
return None
origin = last.get_original(int(index))
return json.dumps(
{k: safe_convert(v) for k, v in dict(origin).items() if not (isinstance(v, float) and np.isnan(v))},
indent=4,
)
if __name__ == "__main__":
import sys
try:
port = int(sys.argv[-1])
except:
port = 5551
app.run(host="0.0.0.0", port=port)