12.31.2008

Look Ma... my first ubiquity script

I have been tracking my calories
A friend has a nice tracking site
A well known site has a lot of nutritional data
I do not do data entry
+I have enough morals not to spider the site :P
--------------------------------------------------
I now know how to write ubiquity scripts :)


CmdUtils.CreateCommand({
name: "king-to-trainer",
author: {name: "Stephen Zabel", homepage: "http://pinkfuzzymud.com/"},
license: "bsd",
calorie_trainer_authenticity_token: "get your own :)",
calorie_trainer_post_uri: "http://caloriescount.stochasticbytes.com/ingredients",
preview: function(previewBlock) {
previewHTML = "posts new ingredient info from the current calorie k*** page
this is just a convience function... you will still likely have to edit the ingredient"
previewBlock.innerHTML = previewHTML;
},

execute: function(statusText) {
var doc = Application.activeWindow.activeTab.document;
var uri = Utils.url(doc.documentURI);

if(this.isCalorieK***(uri)){
this.postToCalorieTrainer(this.parseCurrentPage(doc));
}
else displayMessage('boo! not caloriek***');
},
isCalorieK***: function(uri){
return uri.host.indexOf("caloriek***") != -1
},
parseCurrentPage: function(doc){
var params = {"authenticity_token": this.calorie_trainer_authenticity_token} ;

jQuery(doc.body)
.find(".site_content h1:not(:contains(Guide))")
.filter(':first')
.each(function(i){
params["ingredient[name]"] = jQuery(this)
.find('span')
.remove()
.end().text();
});

params["ingredient[mass_quantity]"] = jQuery('#amount',doc.body).val();
//for poc I'm just going to hard code
params["ingredient[mass_unit]"] = '2';

params["ingredient[calories]"] = jQuery('#calories',doc.body).text();
params["ingredient[total_fat]"] = jQuery('#total_fat',doc.body).text();
params["ingredient[saturated_fat]"] = jQuery('#saturated_fatty_acids',doc.body).text();
params["ingredient[cholesterol]"] = jQuery('#cholesterol',doc.body).text();
params["ingredient[sodium]"] = jQuery('#sodium',doc.body).text();
params["ingredient[carbohydrates]"] = jQuery('#total_carbohydrate',doc.body).text();
params["ingredient[fiber]"] = jQuery('#fiber',doc.body).text();
params["ingredient[sugar]"] = jQuery('#sugars',doc.body).text();
params["ingredient[protein]"] = jQuery('#protein',doc.body).text();

params["ingredient[brand]"] = 'whole paychex';
params["ingredient[description]"] = '';

return params
},
postToCalorieTrainer: function(params){
jQuery.post(this.calorie_trainer_post_uri,params,function(){alert('finished');});
}
});

No comments: