From 68d5e3a0ecfb370ad8d1e8520bfdecd10d0e374a Mon Sep 17 00:00:00 2001 From: MrMobberley Date: Sun, 15 Dec 2013 19:11:46 +0000 Subject: [PATCH] Snap Chat Node Version 1! --- social/snapchat/79-snapchat.html | 53 +++++++++++++++++++++++++ social/snapchat/79-snapchat.js | 68 ++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 social/snapchat/79-snapchat.html create mode 100644 social/snapchat/79-snapchat.js diff --git a/social/snapchat/79-snapchat.html b/social/snapchat/79-snapchat.html new file mode 100644 index 00000000..f1fb9a74 --- /dev/null +++ b/social/snapchat/79-snapchat.html @@ -0,0 +1,53 @@ + + + diff --git a/social/snapchat/79-snapchat.js b/social/snapchat/79-snapchat.js new file mode 100644 index 00000000..f58295da --- /dev/null +++ b/social/snapchat/79-snapchat.js @@ -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);