Created
September 30, 2012 19:07
-
-
Save tkadlubo/3808168 to your computer and use it in GitHub Desktop.
Beeminder Day Counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
#Beeminder Day Counter by Tadeusz Andrzej KadŇāubowski. | |
#A simple Beeminder utility to increment yesterday goal value by one. | |
import argparse | |
import httplib | |
import json | |
import sys | |
import time | |
parser = argparse.ArgumentParser(description='Increment yesterday Beeminder goal value.') | |
parser.add_argument('--user', dest='username', help='Beeminder username') | |
parser.add_argument('--goal', dest='goal', help='Goal') | |
parser.add_argument('--auth_token', dest='auth_token', help='Authentication token (from https://www.beeminder.com/api/v1/auth_token.json)') | |
args = parser.parse_args() | |
username = args.username | |
goal = args.goal | |
auth_token = args.auth_token | |
if not username or not goal or not auth_token: | |
parser.print_help() | |
sys.exit(1) | |
def entered_yesterday(datapoint): | |
yesterday = time.localtime(time.time() - 24 * 60 * 60) | |
datapoint_time = time.localtime(datapoint["timestamp"]) | |
return yesterday.tm_yday == datapoint_time.tm_yday \ | |
and yesterday.tm_year == datapoint_time.tm_year | |
def get_yesterday_value(): | |
connection = httplib.HTTPSConnection('www.beeminder.com') | |
connection.request("GET", "/api/v1/users/%s/goals/%s/datapoints.json?auth_token=%s"%( | |
username, | |
goal, | |
auth_token | |
)) | |
datapoints = json.loads(connection.getresponse().read()) | |
return min([dp["value"] for dp in datapoints if entered_yesterday(dp)]) | |
def add_today_datapoint(value): | |
connection = httplib.HTTPSConnection('www.beeminder.com') | |
connection.request("POST", "/api/v1/users/%s/goals/%s/datapoints.json?auth_token=%s×tamp=%d&value=%d&comment=%s"%( | |
username, | |
goal, | |
auth_token, | |
int(time.time()), | |
int(value), | |
"created+automatically" | |
)) | |
reply = json.loads(connection.getresponse().read()) | |
print reply | |
add_today_datapoint(get_yesterday_value() + 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment