Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Fatsecret Node [WIP] #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
350 changes: 350 additions & 0 deletions fatsecret/fatsecret.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,350 @@
<!--
Copyright 2014 IBM Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="fatsecret-credentials">
<div id="node-config-fatsecret-app-keys">
<div class="form-row key-entry" id="fatsecret-authentication-key-entry">
<p style="margin-top: 10px;"><b>1.</b> Register to get your key and secret at <a href="http://platform.fatsecret.com/api/Default.aspx?screen=r" target="_blank" style="text-decoration:underline;">fatsecret.com</a></p>
<p style="margin-top: 10px;"><b>2.</b> Once registered, find your keys by clicking on your key <a href="http://platform.fatsecret.com/api/Default.aspx?screen=myc" target="_blank" style="text-decoration:underline;">here</a></p>
<p style="margin-top: 10px;"><b>3.</b> Paste the keys here and click the button:</p>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-consumer_key"><i class="fa fa-user"></i> Consumer Key</label>
<input type="text" id="node-config-input-consumer_key">
</div>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-consumer_secret"><i class="fa fa-user"></i> Shared Secret</label>
<input type="text" id="node-config-input-consumer_secret">
</div>
<div class="form-row">
<a class="btn" id="node-config-input-authbutton">Authenticate with Fatsecret</a>
</div>
</div>

<div class="form-row key-entry hidden" id="fatsecret-authentication-code-entry">
<p style="margin-top: 10px;"><b>1.</b> Follow the steps in the newly opened window</a></p>
<p style="margin-top: 10px;"><b>2.</b> Copy the code given at the end and paste it below</a></p>
<p style="margin-top: 10px;"><b>3.</b> Give the credentials a name</p>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-key_identifier"><i class="fa fa-user"></i> Name</label>
<input type="text" id="node-config-input-key_identifier">
</div>
<div class="form-row">
<label style="margin-left: 10px; margin-right: -10px;" for="node-config-input-request_verifier"><i class="fa fa-key"></i> Code</label>
<input type="text" id="node-config-input-request_verifier">
</div>
<div class="form-row">
<a class="btn" id="node-config-input-complete">Complete authentication</a>
</div>
<div class="form-row hidden">
<input type="text" id="node-config-input-access_token">
</div>
<div class="form-row hidden">
<input type="text" id="node-config-input-access_secret">
</div>
</div>

<div class="form-row complete hidden" id="fatsecret-complete">
<p style="margin-top: 10px;"> Completed Authentication successfully. You may now add this configuration</p>
</div>

<div class="form-tips form-errors" id="node-config-errortip">
#
</div>
</script>

<script type="text/javascript">
(function() {
RED.nodes.registerType('fatsecret-credentials',{
category: 'config',
defaults: {
key_identifier: {value:""},
access_token: {value: ""},
access_secret: {value: ""},
consumer_key: {value: ""},
consumer_secret: {value: ""}
},
credentials: {
key_identifier: {type:"text"},
access_token: {type: "text"},
access_secret: {type: "text"},
consumer_key: {value: ""},
consumer_secret: {value: ""}
},

label: function() {
return this.key_identifier;
},
exportable: false,
oneditprepare: function() {
var id = this.id;
var request_token;
var token_secret;

function setWarningInUI(warningMessage) {
if(warningMessage !== ""){
$("#node-config-errortip").text(warningMessage);
$("#node-config-errortip").show();
} else {
$("#node-config-errortip").text("#");
$("#node-config-errortip").hide();
}
}

function getTags() {
var consumer_key = $("#node-config-input-consumer_key").val();
var consumer_secret = $("#node-config-input-consumer_secret").val();

var query = "?consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret;
$.getJSON('fatsecret-credentials/auth' + query, function(data) {
if(data.error){
if(data.error.indexOf("Invalid Signature" > -1)){
setWarningInUI("The API returned an error: Invalid Signature. Are your key and secret entered correctly?");
} else {
setWarningInUI("The API returned an error: " + data.error);
}
} else {
request_token = data.oauth_token;
token_secret = data.oauth_token_secret;
var userAuthUrl = "http://www.fatsecret.com/oauth/authorize?oauth_token=" + data.oauth_token;
var win = window.open(userAuthUrl, '_blank');
setWarningInUI("");
updateAuthUI();
}
});
}

function getAccessTokens() {
var consumer_key = $("#node-config-input-consumer_key").val();
var consumer_secret = $("#node-config-input-consumer_secret").val();
var key_identifier = $("#node-config-input-key_identifier").val();

var request_verifier = $("#node-config-input-request_verifier").val();
var query = "?consumer_key=" + consumer_key + "&consumer_secret=" + consumer_secret + "&request_token=" + request_token + "&token_secret=" + token_secret + "&request_verifier=" + request_verifier + "&id=" + id + "&key_identifier=" + key_identifier;
$.getJSON('fatsecret-credentials/validate' + query, function(data) {
if(data.error){
setWarningInUI("The API returned an error: " + data.error);
} else {
$("#node-config-input-access_secret").val(data.access_secret);
$("#node-config-input-access_token").val(data.access_token);
setWarningInUI("");
updateCompleteUI();
}
});
}

function updateAuthButton() {
var v1 = $("#node-config-input-consumer_secret").val();
var v2 = $("#node-config-input-consumer_key").val();

$("#node-config-input-authbutton").toggleClass("disabled",(v1.length === 0 || v2.length === 0));
}

function updateAuthUI() {
$(".key-entry").hide();
$("#fatsecret-authentication-code-entry").show();
}

function updateCompleteUI() {
$(".key-entry").hide();
$("#fatsecret-complete").show();
}

$("#node-config-input-consumer_key").on('change keydown paste input',updateAuthButton);
$("#node-config-input-consumer_secret").on('change keydown paste input',updateAuthButton);

function updateCompleteButton() {
var v1 = $("#node-config-input-key_identifier").val();
var v2 = $("#node-config-input-request_verifier").val();

$("#node-config-input-complete").toggleClass("disabled",(v1.length === 0 || v2.length === 0));
}

$("#node-config-input-key_identifier").on('change keydown paste input',updateCompleteButton);
$("#node-config-input-request_verifier").on('change keydown paste input',updateCompleteButton);


$("#node-config-input-authbutton").click(function() {
if($("#node-config-input-consumer_key").val() && $("#node-config-input-consumer_secret").val()){
getTags();
}
});

$("#node-config-input-complete").click(function() {
if( $("#node-config-input-key_identifier").val() && $("#node-config-input-request_verifier").val()){
getAccessTokens();
}
});

updateAuthButton();
updateCompleteButton();
setWarningInUI("");
},
oneditsave: function() {
},
oneditcancel: function(adding) {
}
});
})();
</script>

<script type="text/x-red" data-help-name="fatsecret">
</script>

<script type="text/x-red" data-template-name="fatsecret">
<div class="form-row">
<label for="node-input-fatsecret"><i class="fa fa-user"></i> Fatsecret</label>
<input type="text" id="node-input-fatsecret">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="fatsecret-data-time-select"><i class="fa fa-globe"></i> Get diary from</label>
<select id="fatsecret-data-time-select"><option value="today">Today</option><option value="yesterday">Yesterday</option><option value="date">Specific date</option><option value="message">Input defined</option></select>
</div>
<div class="form-row fatsecret-data-time" id="fatsecret-data-time-date">
<label for="fatsecret-data-date"><i class="fa fa-calendar"></i> Date</label>
<input id="node-input-date" class="weather-time-input" type="date" style="width:145px">
</div>
<div class="form-row hidden">
<label for="node-input-mode"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-mode">
</div>
</script>

<script type="text/javascript">
RED.nodes.registerType('fatsecret',{
category: 'fitness',
color: '#32B34C',
defaults: {
fatsecret: {type:"fatsecret-credentials",required:true},
name: {value:""},
mode: {value:"today"},
date: {value:""}
},
inputs:1,
outputs:1,
icon: "heart.png",
label: function() {
return this.name||"fatsecret";
},
oneditprepare: function() {
$("#fatsecret-data-time-select").change(function() {
var id = $("#fatsecret-data-time-select option:selected").val();
$(".fatsecret-data-time").hide();
$("#fatsecret-data-time-"+id).show();
});

var mode = $("#node-input-mode").val();

if(mode == "message"){
$("#fatsecret-data-time-select").val("message");
} else if (mode == "date") {
$("#fatsecret-data-time-select").val("date");
} else if (mode == "yesterday") {
$("#fatsecret-data-time-select").val("yesterday");
} else {
$("#fatsecret-data-time-select").val("today");
}
var id = $("#fatsecret-data-time-select option:selected").val();

$(".fatsecret-data-time").hide();
$("#fatsecret-data-time-"+id).show();
},
oneditsave: function() {
var type = $("#fatsecret-data-time-select option:selected").val();
if (type == "today") {
$("#node-input-date").val("");
$("#node-input-mode").val("today");
} else if (type == "yesterday") {
$("#node-input-date").val("");
$("#node-input-mode").val("yesterday");
} else if(type == "date") {
$("#node-input-mode").val("date");
} else {
$("#node-input-date").val("");
$("#node-input-mode").val("message");
}
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

<script type="text/x-red" data-template-name="fatsecret out">
<div class="form-row">
<label for="node-input-fatsecret"><i class="fa fa-user"></i> Fatsecret</label>
<input type="text" id="node-input-fatsecret">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-row">
<label for="node-input-food"><i class="icon-tag"></i> Food</label>
<input type="text" id="node-input-food" placeholder="Food">
</div>
</script>

<script type="text/javascript">
// RED.nodes.registerType('fatsecret out',{
// category: 'fitness',
// color: '#32B34C',
// defaults: {
// fatsecret: {type:"fatsecret-credentials",required:true},
// name: {value:""},
// food: {value:""}
// },
// inputs:1,
// outputs:0,
// icon: "heart.png",
// label: function() {
// return this.name||"fatsecret";
// },
// oneditprepare: function() {
// var id = this.id;
// function getFood(food) {
// $.ajax({
// type: "GET",
// url: "/fatsecret/food",
// data: {
// "food": food,
// "id" : id
// },
// dataType: "JSON"
// }).done (function (data) {
// var availableTags = [];
// for (var i=0 ; i < data.stations.station.length; i++) {
// availableTags.push(data.stations.station[i].name[0]);
// }
// $( "#node-input-food" ).autocomplete({
// source: availableTags
// });
// });
// }

// $("#node-input-food").on('input', function() {
// getFood($("#node-input-food").val());
// });
// },
// oneditsave: function() {
// },
// labelStyle: function() {
// return this.name?"node_label_italic":"";
// }
// });
</script>
Loading