Tidy up old snapchat node

This commit is contained in:
dceejay 2015-04-08 09:56:39 +01:00
parent 4dae7ece06
commit 6158aa2b73
2 changed files with 121 additions and 84 deletions

View File

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

View File

@ -1,68 +1,74 @@
// Require main module /**
var RED = require(process.env.NODE_RED_HOME+"/red/red"); * Copyright 2013 Chris Mobberly
*
* 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.
**/
module.exports = function(RED) {
"use strict";
var snapchat = require('snapchat'), var snapchat = require('snapchat'),
client = new snapchat.Client(), client = new snapchat.Client(),
fs = require('fs'); fs = require('fs');
function SnapChatAccountNode(n) { function SnapChatAccountNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.username = n.username; this.username = n.username;
this.password = n.password; var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("password"))) { this.password = credentials.password; }
else { this.error("No password set"); }
} }
function SnapChatNode(n) { function SnapChatNode(n) {
// Create a RED node
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var node = this; var node = this;
this.account = n.account; this.account = n.account;
this.path = n.path; this.path = n.path;
this.accountConfig = RED.nodes.getNode(this.account); this.accountConfig = RED.nodes.getNode(this.account);
this.username = this.accountConfig.username; this.username = this.accountConfig.username;
this.password = this.accountConfig.password; this.password = this.accountConfig.password;
this.on("input",function(){ this.on("input",function(){
// Make sure the images folder exists
if (!fs.existsSync(this.path)) { if (!fs.existsSync(this.path)) {
fs.mkdirSync(this.path); fs.mkdirSync(this.path);
} }
var path = this.path; var path = this.path;
var msg ={}; var msg ={};
msg.snaps =[]; msg.snaps =[];
client.login(this.username, this.password).then(function(data) { client.login(this.username, this.password).then(function(data) {
msg.payload = data.snaps.length; msg.payload = data.snaps.length;
data.snaps.forEach(function(snap) { data.snaps.forEach(function(snap) {
if (snap.st == 1 && typeof snap.t !== 'undefined') if (snap.st == 1 && typeof snap.t !== 'undefined') {
{
var full_path = path + snap.id + '.jpg'; var full_path = path + snap.id + '.jpg';
var snapObject = {Sender:snap.sn,Path:full_path,SnapId:snap.id}; var snapObject = {Sender:snap.sn,Path:full_path,SnapId:snap.id};
msg.snaps.push(snapObject); msg.snaps.push(snapObject);
var stream = fs.createWriteStream(full_path, { flags: 'w', encoding: null, mode: '0666' });
var stream = fs.createWriteStream(full_path, { flags: 'w', encoding: null, mode: 0666});
client.getBlob(snap.id).then(function(blob) { client.getBlob(snap.id).then(function(blob) {
blob.pipe(stream); blob.pipe(stream);
blob.resume(); blob.resume();
}); });
} }
}); });
client.clear(); client.clear();
node.send(msg); 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("Snap Chat",SnapChatNode);
RED.nodes.registerType("snapchat-account",SnapChatAccountNode); RED.nodes.registerType("snapchat-account",SnapChatAccountNode,{
credentials: {
password: {type: "password"}
}
});
}