forked from c99koder/personal-influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexist.py
237 lines (207 loc) · 9.36 KB
/
exist.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/python3
# Copyright (C) 2020 Sam Steele
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import requests, pytz, sys
from datetime import datetime, date, timedelta, time
from influxdb import InfluxDBClient
from influxdb.exceptions import InfluxDBClientError
from publicsuffix2 import PublicSuffixList
LOCAL_TIMEZONE = pytz.timezone('America/New_York')
EXIST_ACCESS_TOKEN = ''
EXIST_USERNAME = ''
INFLUXDB_HOST = 'localhost'
INFLUXDB_PORT = 8086
INFLUXDB_USERNAME = 'root'
INFLUXDB_PASSWORD = 'root'
INFLUXDB_DATABASE = 'exist'
FITBIT_DATABASE = ''
TRAKT_DATABASE = ''
GAMING_DATABASE = ''
RESCUETIME_DATABASE = ''
points = []
start_time = str(int(LOCAL_TIMEZONE.localize(datetime.combine(date.today(), time(0,0)) - timedelta(days=7)).astimezone(pytz.utc).timestamp()) * 1000) + 'ms'
def append_tags(tags):
try:
response = requests.post('https://exist.io/api/1/attributes/custom/append/',
headers={'Authorization':'Bearer ' + EXIST_ACCESS_TOKEN},
json=tags)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print("HTTP request failed: %s" % (err))
sys.exit()
result = response.json()
if len(result['failed']) > 0:
print("Request failed: %s" % result['failed'])
sys.exit()
if len(result['success']) > 0:
print("Successfully sent %s tags" % len(result['success']))
def acquire_attributes(attributes):
try:
response = requests.post('https://exist.io/api/1/attributes/acquire/',
headers={'Authorization':'Bearer ' + EXIST_ACCESS_TOKEN},
json=attributes)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print("HTTP request failed: %s" % (err))
sys.exit()
result = response.json()
if len(result['failed']) > 0:
print("Request failed: %s" % result['failed'])
sys.exit()
def post_attributes(values):
try:
response = requests.post('https://exist.io/api/1/attributes/update/',
headers={'Authorization':'Bearer ' + EXIST_ACCESS_TOKEN},
json=values)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print("HTTP request failed: %s" % (err))
sys.exit()
result = response.json()
if len(result['failed']) > 0:
print("Request failed: %s" % result['failed'])
sys.exit()
if len(result['success']) > 0:
print("Successfully sent %s attributes" % len(result['success']))
try:
client = InfluxDBClient(host=INFLUXDB_HOST, port=INFLUXDB_PORT, username=INFLUXDB_USERNAME, password=INFLUXDB_PASSWORD)
client.create_database(INFLUXDB_DATABASE)
client.switch_database(INFLUXDB_DATABASE)
except InfluxDBClientError as err:
print("InfluxDB connection failed: %s" % (err))
sys.exit()
acquire_attributes([{"name":"gaming_min", "active":True}, {"name":"tv_min", "active":True}])
try:
response = requests.get('https://exist.io/api/1/users/' + EXIST_USERNAME + '/insights/',
headers={'Authorization':'Bearer ' + EXIST_ACCESS_TOKEN})
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print("HTTP request failed: %s" % (err))
sys.exit()
data = response.json()
print("Got %s insights from exist.io" % len(data['results']))
for insight in data['results']:
if insight['target_date'] == None:
date = datetime.fromisoformat(insight['created'].strip('Z')).strftime('%Y-%m-%d')
else:
date = insight['target_date']
points.append({
"measurement": "insight",
"time": date + "T00:00:00",
"tags": {
"type": insight['type']['name'],
"attribute": insight['type']['attribute']['label'],
"group": insight['type']['attribute']['group']['label'],
},
"fields": {
"html": insight['html'].replace("\n", "").replace("\r", ""),
"text": insight['text']
}
})
try:
response = requests.get('https://exist.io/api/1/users/' + EXIST_USERNAME + '/attributes/?limit=7&groups=custom,mood',
headers={'Authorization':'Bearer ' + EXIST_ACCESS_TOKEN})
response.raise_for_status()
except requests.exceptions.HTTPError as err:
print("HTTP request failed: %s" % (err))
sys.exit()
data = response.json()
print("Got attributes from exist.io")
for result in data:
for value in result['values']:
if value['value'] != None and value['value'] != '' and result['attribute'] != 'custom':
if result['group']['name'] == 'custom':
points.append({
"measurement": result['group']['name'],
"time": value['date'] + "T00:00:00",
"tags": {
"tag": result['label']
},
"fields": {
"value": value['value']
}
})
else:
points.append({
"measurement": result['attribute'],
"time": value['date'] + "T00:00:00",
"fields": {
"value": value['value']
}
})
try:
client.write_points(points)
except InfluxDBClientError as err:
print("Unable to write points to InfluxDB: %s" % (err))
sys.exit()
print("Successfully wrote %s data points to InfluxDB" % (len(points)))
values = []
tags = []
if FITBIT_DATABASE != '':
client.switch_database(FITBIT_DATABASE)
durations = client.query('SELECT "duration" FROM "activity" WHERE activityName = \'Meditating\' AND time >= ' + start_time)
for duration in list(durations.get_points()):
if duration['duration'] > 0:
date = datetime.fromisoformat(duration['time'].strip('Z') + "+00:00").astimezone(LOCAL_TIMEZONE).strftime('%Y-%m-%d')
tags.append({'date': date, 'value': 'meditation'})
durations = client.query('SELECT "duration","activityName" FROM "activity" WHERE activityName != \'Meditating\' AND time >= ' + start_time)
for duration in list(durations.get_points()):
if duration['duration'] > 0:
date = datetime.fromisoformat(duration['time'].strip('Z') + "+00:00").astimezone(LOCAL_TIMEZONE).strftime('%Y-%m-%d')
tags.append({'date': date, 'value': 'exercise'})
tags.append({'date': date, 'value': duration['activityName'].lower().replace(" ", "_")})
if TRAKT_DATABASE != '':
totals = {}
client.switch_database(TRAKT_DATABASE)
durations = client.query('SELECT "duration" FROM "watch" WHERE time >= ' + start_time)
for duration in list(durations.get_points()):
date = datetime.fromisoformat(duration['time'].strip('Z') + "+00:00").astimezone(LOCAL_TIMEZONE).strftime('%Y-%m-%d')
if date in totals:
totals[date] = totals[date] + duration['duration']
else:
totals[date] = duration['duration']
for date in totals:
values.append({'date': date, 'name': 'tv_min', 'value': int(totals[date])})
tags.append({'date': date, 'value': 'tv'})
if GAMING_DATABASE != '':
totals = {}
client.switch_database(GAMING_DATABASE)
durations = client.query('SELECT "value" FROM "time" WHERE "value" > 0 AND time >= ' + start_time)
for duration in list(durations.get_points()):
date = datetime.fromisoformat(duration['time'].strip('Z') + "+00:00").astimezone(LOCAL_TIMEZONE).strftime('%Y-%m-%d')
if date in totals:
totals[date] = totals[date] + duration['value']
else:
totals[date] = duration['value']
for date in totals:
values.append({'date': date, 'name': 'gaming_min', 'value': int(totals[date] / 60)})
tags.append({'date': date, 'value': 'gaming'})
elif RESCUETIME_DATABASE != '':
psl = PublicSuffixList()
totals = {}
client.switch_database(RESCUETIME_DATABASE)
durations = client.query('SELECT "duration","activity" FROM "activity" WHERE category = \'Games\' AND activity != \'Steam\' AND activity != \'steamwebhelper\' AND activity != \'origin\' AND activity != \'mixedrealityportal\' AND activity != \'holoshellapp\' AND activity != \'vrmonitor\' AND activity != \'vrserver\' AND activity != \'oculusclient\' AND activity != \'vive\' AND activity != \'obs64\' AND time >= ' + start_time)
for duration in list(durations.get_points()):
date = datetime.fromisoformat(duration['time'].strip('Z') + "+00:00").astimezone(LOCAL_TIMEZONE).strftime('%Y-%m-%d')
if psl.get_public_suffix(duration['activity'], strict=True) is None:
if date in totals:
totals[date] = totals[date] + duration['duration']
else:
totals[date] = duration['duration']
for date in totals:
values.append({'date': date, 'name': 'gaming_min', 'duration': int(totals[date] / 60)})
tags.append({'date': date, 'value': 'gaming'})
if len(tags) > 0:
append_tags(tags)
if len(values) > 0:
post_attributes(values)