forked from cayennes/anki-beemind-maintained-progress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beeminder.py
72 lines (52 loc) · 2.15 KB
/
beeminder.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
import json
import requests
api_url = "https://www.beeminder.com/api/v1/"
def _result_dict(res):
if res.status_code == 200:
return {"success?": True}
else:
return {"success?": False,
"error_message": ("Status: %s\nResponse: %s" %
(res.status_code, res.text))}
def add_datapoint(auth_token, slug, value):
url = api_url + "users/me/goals/" + slug + "/datapoints.json"
res = requests.post(url, data={"value": value,
"auth_token": auth_token})
return _result_dict(res)
def as_datapoint(value, date, comment=None):
if comment is None:
comment = ""
return {"value": value,
"daystamp": date,
"comment": comment,
"requestid": date}
def add_datapoints(auth_token, goal_slug, datapoints):
"""post a list of datapoints to beeminder
each datapoint is a map containing
* "daystamp" a string representing the date
* "value"
* "comment"
* optional "requestid"
"""
url = api_url + "users/me/goals/" + goal_slug + "/datapoints/create_all.json"
res = requests.post(url, data={"datapoints": json.dumps(datapoints),
"auth_token": auth_token})
return _result_dict(res)
def get_goal_details(auth_token, goal_name):
url = api_url + "users/me/goals/" + goal_name + ".json"
params = {"auth_token": auth_token}
res = requests.get(url, params=params)
return res.json()
def doesnt_autosum(auth_token, goal_name):
goal_details = get_goal_details(auth_token, goal_name)
return goal_details["kyoom"] == False
def modify_goal(auth_token, goal_name, new_attributes):
url = api_url + "users/me/goals/" + goal_name + ".json"
data = new_attributes
data["auth_token"] = auth_token
res = requests.put(url, data=data)
return _result_dict(res)
def configure_api_goal(auth_token, goal_name):
"""sets odom to False and datasource to API
This is what we want because this is an API source and zeroes are zeroes, not odometer resets."""
return modify_goal(auth_token, goal_name, {"odom": False, "datasource": "api"})