Merge pull request #12 from christoface/Snap_Chat_Node

Snap Chat Node
This commit is contained in:
Nick O'Leary 2013-12-17 01:50:48 -08:00
commit 072c58b735
2 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,53 @@
<script type="text/x-red" data-template-name="Snap Chat">
<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-account"><i class="icon-tasks"></i> Account</label>
<input type="text" id="node-input-account">
</div> <div class="form-row">
<label for="node-input-path"><i class="icon-tag"></i> Save Path</label>
<input type="text" id="node-input-path" placeholder="./snapchat_images"> </div> </script>
<script type="text/x-red" data-help-name="Snap Chat">
<p>Downloads SnapChat images from the account specified to the path specified. <b>msg.payload</b> contains the count of images downloaded. <b>msg.snaps</b>
contains information on the snap downloaded such as path, sent from and id of the image.
</script> <script type="text/javascript">
RED.nodes.registerType('Snap Chat',{
category: 'social-input',
color:"Yellow",
defaults: {
name:{value:"Snap Chat"},
account: {type:"snapchat-account",required:true},
path: {value:"./snapchat_images/",required:true}
},
inputs:1,
outputs:1,
icon: "arrow-in.png",
label: function() {
return this.name||"Snap Chat";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script> <script type="text/x-red" data-template-name="snapchat-account">
<div class="form-row">
<label for="node-config-input-username"><i class="icon-tasks"></i> Username</label>
<input type="text" id="node-config-input-username" placeholder="billy-bob">
</div>
<div class="form-row">
<label for="node-config-input-password"><i class="icon-tasks"></i> Password</label>
<input type="password" id="node-config-input-password" placeholder="">
</div> </script>
<script type="text/javascript">
RED.nodes.registerType('snapchat-account',{
category: 'config',
defaults: {
username: {value:"",required:true},
password: {value:"",required:true},
},
label: function() {
return this.username;
}
});
</script>

View File

@ -0,0 +1,68 @@
// Require main module
var RED = require(process.env.NODE_RED_HOME+"/red/red");
var snapchat = require('snapchat'),
client = new snapchat.Client(),
fs = require('fs');
function SnapChatAccountNode(n) {
RED.nodes.createNode(this,n);
this.username = n.username;
this.password = n.password;
}
function SnapChatNode(n) {
// Create a RED node
RED.nodes.createNode(this,n);
var node = this;
this.account = n.account;
this.path = n.path;
this.accountConfig = RED.nodes.getNode(this.account);
this.username = this.accountConfig.username;
this.password = this.accountConfig.password;
this.on("input",function(){
// Make sure the images folder exists
if(!fs.existsSync(this.path)) {
fs.mkdirSync(this.path);
}
var path = this.path;
var msg ={};
msg.snaps =[];
client.login(this.username, this.password).then(function(data){
msg.payload = data.snaps.length;
data.snaps.forEach(function(snap){
if (snap.st == 1 && typeof snap.t !== 'undefined')
{
var full_path = path + snap.id + '.jpg';
var snapObject = {Sender:snap.sn,Path:full_path,SnapId:snap.id};
msg.snaps.push(snapObject);
var stream = fs.createWriteStream(full_path, { flags: 'w', encoding: null, mode: 0666});
client.getBlob(snap.id).then(function(blob){
blob.pipe(stream);
blob.resume();
});
}
});
client.clear();
node.send(msg);
});
});
}
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("Snap Chat",SnapChatNode);
RED.nodes.registerType("snapchat-account",SnapChatAccountNode);