Action

Beemind

Posted by chronologos, Last update over 4 years ago

Post to Beeminder

Update multiple data points simultaneously using a drafts note. First line must be # Beeminder Template. Each line after that represents a single goal to update in the format goal datapoint comment. Comment may be left out. e.g. sleepmore 1.

Steps

  • script

    base_url = "https://www.beeminder.com/api/v1/"
    
    var credential = Credential.create("Beeminder", "Salvation for akratic data nerds.");
    
    credential.addTextField("username", "Username");
    credential.addPasswordField("auth_token", "Auth Token");
    
    // Populate from https://www.beeminder.com/api/v1/auth_token.json
    credential.authorize();
    
    function main() {
    	text = editor.getText()
    	var lines = text.split("\n");
    	if (lines[0] != "# Beeminder Template") {
    		alert("not the correct template")
    		return
    	}
    	for (let line of lines.slice(1)) {
    		dp = parseLine(line)
    		updateOne(dp[0], dp[1], dp[2])
    	}
    }
    
    // returns datapoint which is array[3] of (string goal, int datapoint, string comment)
    function parseLine(l) {
    	token = l.split(" ")
    	if (token.length == 2) {
    		token.push("") // Empty comment
    	}
    	if (token.length != 3){
    		throw "Malformatted line, stopping here"
    	}
    	return [token[0], token[1], token[2]]
    }
    
    function exclude(goal) {
      return goal.charAt(0) == "#"
    }
    
    function includeOne(goal) {
    	return goal.charAt(0) == "*"
    }
    
    
    function updateOne(goal, val, comment) {
       if (exclude(goal)) {
       	return
       }
       req = {
    		"url": base_url+"/users/"+ credential.getValue("username")+"/goals/" + goal + "/datapoints.json",
    		"method": "POST",
    		"data": {
    		"value": val,
    		"comment": comment,
    		"auth_token": credential.getValue("auth_token")
    		},
    		"headers": {
    		// "auth_token": extra_headers["auth_token"]
    		}
    	}
    	http = HTTP.create();
    	var response = http.request(req);
    	if (response.success) {}
    	else {
    	  alert("req " + JSON.stringify(req) + " failed with status: " + response.statusCode + " error msg: " + response.error);
    	}
    }
    main()

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.