-
-
Save felipc/51353 to your computer and use it in GitHub Desktop.
Commands for your.flowingdata.com
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
/*** | |
Felipe Gomes -- felipe.wordpress.com / felipc.com | |
2009 GPL/MPL | |
Ubiquity command feed for Your.FlowingData personal data tracking experiment | |
***/ | |
/******************************** | |
* Main function that comunicates with Twitter | |
* Code from https://ubiquity.mozilla.com/standard-feeds/social.js | |
* Little modification to always send direct messages to @yfd | |
*********************************/ | |
function __send_yfd_dm_msg(msg, successMsg) { | |
var statusText = "d yfd " + msg; | |
const TWITTER_STATUS_MAXLEN = 140; | |
if(statusText.length < 1) { | |
displayMessage("Twitter requires a status to be entered"); | |
return; | |
} | |
var updateUrl = "https://twitter.com/statuses/update.json"; | |
var updateParams = { | |
source: "ubiquity", | |
status: statusText.slice(0, TWITTER_STATUS_MAXLEN) | |
}; | |
function make_basic_auth(user, password){ | |
var tok = user + ":" + password; | |
var hash = CmdUtils.getWindow().btoa(tok); //Base64 encode. | |
return "Basic " + hash; | |
}; | |
var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); | |
var logins = passwordManager.findLogins({}, "http://twitter.com", "", null); | |
if( logins.length > 0 ){ | |
var login = logins[0]; | |
var auth = make_basic_auth(login.username, login.password); | |
} else { | |
var auth = null; | |
} | |
displayMessage("Log: msg to be sent: '" + statusText + "'"); | |
jQuery.ajax({ | |
type: "POST", | |
url: updateUrl, | |
data: updateParams, | |
dataType: "json", | |
beforeSend: function(req) { | |
if( auth ) req.setRequestHeader("Authorization", auth); | |
}, | |
error: function() { | |
displayMessage("Error - your status was not updated"); | |
}, | |
success: function() { | |
displayMessage(successMsg); | |
} | |
}); | |
} | |
/***** | |
* Custom Ubiquity noun type to accept a number followed by lbs or kgs | |
* When submitting the value (done by the execute function of the command), kgs are converted to lbs | |
*****/ | |
var noun_type_yfd_weight = { | |
_name: "weight", | |
suggest: function(text, html) { | |
if (!text) return []; | |
var suggestions = new Array(); | |
var regRes = text.match(/^\s*([0-9]+)\s*(lb?s?|kg?s?)?/); | |
CmdUtils.log(regRes); | |
if (regRes == null) return []; | |
var value = regRes[1]; | |
if ("kgs".indexOf(regRes[2]) == 0) { | |
return [CmdUtils.makeSugg(value + " kgs"),CmdUtils.makeSugg(value + " lbs")]; | |
} else { | |
return [CmdUtils.makeSugg(value + " lbs"),CmdUtils.makeSugg(value + " kgs")]; | |
} | |
} | |
} | |
/***** | |
* Custom Ubiquity noun type to select the page to open | |
*****/ | |
var noun_type_yfd_page = new CmdUtils.NounType("page", ['home', 'weight', 'sleep', 'feeling', 'potty']); | |
/*** | |
* List of the simple commands that will be all auto-generated by creating a new YFD_default_command | |
***/ | |
var YFDCommands = [ | |
{ verb: 'ate', argument: 'food', what: 'about your eating habits', synonyms: ['eat'], realCmd: null}, | |
{ verb: 'drank', argument: 'liquid', what: 'about your driking habits', synonyms: ['drink'], realCmd: null}, | |
{ verb: 'feel', argument: 'feeling', what: 'how you feel right now', synonyms: ['feeling'], realCmd: 'feeling'}, | |
{ verb: 'peed', argument: null, what: 'that you just peed', synonyms: null, realCmd: null}, | |
{ verb: 'pooped', argument: null, what: 'that you just peed', synonyms: null, realCmd: null}, | |
{ verb: 'slept', argument: null, what: 'that you\'re going to sleep', synonyms: ['sleep', 'gnight'], realCmd: 'gnite'}, | |
{ verb: 'woke up', argument: null, what: 'that you just woke up', synonyms: ['wake-up', 'gmorning'], realCmd: 'gmorning'}, | |
{ verb: 'weight motivation', argument: 'motivation', what: 'your motivation for tracking your weight', synonyms: null, realCmd: 'weight_bc'}, | |
{ verb: 'sleep motivation', argument: 'motivation', what: 'your motivation for tracking your sleeping habits', synonyms: null, realCmd: 'sleep_bc'}]; | |
/***** READ HERE ****/ | |
/**** | |
* YFD_default_command is the object that will be passed to Ubiquity. | |
* This default implementation accomodates for most of YFD commands. | |
* | |
* Note some magic going on to: | |
--- be more natural language | |
commands are named as verbs, for example, yfd-ate, yfd-drank. The idea was to think yfd as "I": I ate, I drank, I woke up | |
-- work as expected by Ubiquity | |
the command name is originally "woke up", but then there is the spaces-to-dashes substitution | |
-- submit the right command to @yfd | |
yfd-slept (I slept) is converted to gnight, for example. Note that there is the synonym, so yfd-gnight still works | |
*****/ | |
function YFD_default_command(verb, arg, what, syns, realCmd, cmdprefix) { | |
var cmdName = realCmd || verb; | |
var prefix = (cmdprefix == null) ? "yfd-" : cmdprefix; | |
this.name = prefix + verb.replace(' ','-'); | |
if (arg) { | |
this.takes = new Object(); | |
this.takes[arg] = noun_arb_text; | |
} | |
if (syns) { | |
this.synonyms = new Array(); | |
var asyn; | |
for (asyn in syns) { | |
this.synonyms.push(prefix + syns[asyn]); | |
} | |
} | |
this.preview = function (pblock, input) { | |
if (!input || !input.text) { | |
pblock.innerHTML = "Tells @yfd " + what; | |
} else { | |
var tmp = "Tells @yfd that you ${did} <strong>${what}</strong>"; | |
pblock.innerHTML = CmdUtils.renderTemplate(tmp, {did:verb,what:input.text}); | |
} | |
} | |
this.execute = function(input) { | |
if (input && input.text) { | |
input.text = ' ' + input.text; | |
} else { | |
input = {text:''}; | |
} | |
__send_yfd_dm_msg(cmdName + input.text, "You just " + verb + input.text); | |
} | |
return this; | |
} | |
/* Default values set on the Object prototype */ | |
YFD_default_command.prototype.homepage = "http://yfd.yourflowingdata.com"; | |
YFD_default_command.prototype.icon = "http://flowingdata.com/wp-content/themes/flowingdata-2-1/images/favicon.ico"; | |
//Please promise you won't change the icon location... or pick some better, like flowingdata.com/favicon.ico | |
YFD_default_command.prototype.author = { name: "Felipe Gomes", email: "[email protected]"}; | |
YFD_default_command.prototype.license = "GPL"; | |
YFD_default_command.prototype.description = "Updates @yfd information"; | |
YFD_default_command.prototype.help = "Just type your info and press enter, it will be sent to @yfd tracker as a Twitter direct message."; | |
/* Create all the default commands */ | |
YFDCommands.forEach( function(cmd) { | |
var k = new YFD_default_command(cmd.verb, cmd.argument, cmd.what, cmd.synonyms, cmd.realCmd); | |
CmdUtils.CreateCommand( k ); | |
}); | |
/* Create more customized commands: weight and weight goal, which takes noun_type_yfd_weight instead of noun_arb_text, | |
* and does the lbs -> kgs conversion if necessary */ | |
var __yfd_weigh = new YFD_default_command('weigh', 'weight', "how much you weight", ['weight'], null); | |
__yfd_weigh.takes['weight'] = noun_type_yfd_weight; | |
__yfd_weigh.execute = function(input) { | |
var regRes = input.text.match(/^([0-9]+) (lbs|kgs)/); | |
if (regRes == null) { | |
displayMessage("Error in parsing"); | |
return; | |
} | |
value = regRes[1]; | |
if (regRes[2] == "kgs") { | |
value = parseInt(value, 10) * 2.20462; | |
value = value.toString(); | |
} | |
__send_yfd_dm_msg("weigh " + value, "Your weight was updated to " + input.text); | |
}; | |
CmdUtils.CreateCommand(__yfd_weigh); | |
var __yfd_weight_goal = new YFD_default_command('weight goal', 'weight', 'your weight goal', ['weigh-goal'], null); | |
__yfd_weight_goal.takes['weight'] = noun_type_yfd_weight; | |
__yfd_weight_goal.execute = function(input) { | |
var regRes = input.text.match(/^([0-9]+) (lbs|kgs)/); | |
if (regRes == null) { | |
displayMessage("Error in parsing"); | |
return; | |
} | |
value = regRes[1]; | |
if (regRes[2] == "kgs") { | |
value = parseInt(value, 10) * 2.20462; | |
value = value.toString(); | |
} | |
__send_yfd_dm_msg("weight_goal " + value, "Your weight goal was updated to " + input.text); | |
}; | |
CmdUtils.CreateCommand(__yfd_weight_goal); | |
var __yfd_view = new YFD_default_command('view','page','view page',null,null); | |
__yfd_view.takes['page'] = noun_type_yfd_page; | |
__yfd_view.preview = function(pblock, input) { | |
var txt = "Open your track information for <strong>weight</strong>, <strong>sleep</strong>, <strong>feeling</strong> or <strong>potty</strong>"; | |
txt = txt.replace(input.text, "<u>" + input.text + "</u>"); | |
pblock.innerHTML = txt; | |
}; | |
__yfd_view.execute = function(input) { | |
Utils.openUrlInBrowser("http://your.flowingdata.com/" + input.text + "/"); | |
}; | |
CmdUtils.CreateCommand(__yfd_view); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment