-
Notifications
You must be signed in to change notification settings - Fork 0
/
username-update.py
94 lines (84 loc) · 3.83 KB
/
username-update.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
from requests_oauthlib import OAuth2Session
from requests.auth import HTTPBasicAuth
from mastodon import Mastodon
from datetime import date
from os import path
import json
PATH = path.dirname(path.abspath(__file__))
def read_fitbit_key():
with open(PATH + "/key/fitbit_token.secret", "r") as f:
key = f.readlines()
client_id = key[0][:-1]
client_secret = key[1][:-1]
access_token = key[2][:-1]
refresh_token = key[3]
return(client_id, client_secret, access_token, refresh_token)
class fitbit_client(object):
refresh_token_url="https://api.fitbit.com/oauth2/token"
def __init__(self, client_id, client_secret, access_token, refresh_token):
self.client_id = client_id
self.client_secret = client_secret
token = {"access_token": access_token, "refresh_token": refresh_token}
self.session = OAuth2Session(
client_id,
token = token,
auto_refresh_url = "https://api.fitbit.com/oauth2/token",
)
def refresh_call_back(token):
with open(PATH + "/key/fitbit_token.secret", "w") as f:
f.write(client_id + "\n")
f.write(client_secret + "\n")
f.write(token["access_token"] + "\n")
f.write(token["refresh_token"])
self.access_token = token["access_token"]
self.refresh_token = token["refresh_token"]
def refresh_token(self):
token = self.session.refresh_token(
self.refresh_token_url,
auth=HTTPBasicAuth(self.client_id, self.client_secret)
)
self.refresh_call_back(token)
return(token)
def make_request(self, url):
header = {"Accept-Language": None}
headers = {"headers": header}
method = "GET"
response = self.session.request(method, url, client_id=self.client_id, client_secret=self.client_secret, **headers)
if response.status_code == 401:
d = json.loads(response.content.decode("utf8"))
if d["errors"][0]["errorType"] == "expired_token":
self.refresh_token()
response = self.session.request(method, url, client_id=self.client_id, client_secret=self.client_secret, **headers)
return(response)
def get_sleep(self):
url = "https://api.fitbit.com/1.2/user/-/sleep/date/{}.json".format(date.today())
return(self.make_request(url))
def get_heart_rate(self):
url = "https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/5min.json"
return(self.make_request(url))
def get_user_status(client):
#sleep_data = json.loads(client.get_sleep().content.decode("utf8"))["sleep"]
heart_rate_data = json.loads(client.get_heart_rate().content.decode("utf8"))
heart_rate_zone = heart_rate_data["activities-heart"][0]["value"]["heartRateZones"]
heart_rate = heart_rate_data["activities-heart-intraday"]["dataset"][-1]["value"]
status_name = "non"
for zone in heart_rate_zone:
if zone["min"] < heart_rate and heart_rate <= zone["max"]:
status_name = zone["name"]
status_dic = {"Out of Range": " 😐","Fat Burn": " 😓","Cardio": " 😅","Peak": " 😅", "non": " 🤔"}
status = status_dic[status_name]
return(status)
def mstdn_name_update(status):
mastodon = Mastodon(
access_token="key/username-updater-usercred.secret",
api_base_url="https://mstdn.maud.io"
)
with open(PATH + "/mstdn-id.txt", "r") as f:
id = int(f.read())
display_name = mastodon.account(id)["display_name"][:-2]
mastodon.account_update_credentials(display_name = display_name + status)
if __name__ == "__main__":
client_id, client_secret, access_token, refresh_token = read_fitbit_key()
client = fitbit_client(client_id, client_secret, access_token, refresh_token)
status = get_user_status(client)
mstdn_name_update(status)