Last active
March 24, 2016 10:53
-
-
Save lkLeonov/f3fe142d3583ed418180 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const Firebase = require('firebase'); | |
const FB_ROOT = new Firebase('https://scrapyard.firebaseio.com/'); | |
const VK = { | |
wallGet: { | |
// translating vk api to node http api | |
reqOpts: function(opts) { | |
return({ | |
host: 'api.vk.com', | |
path: `/method/wall.get?domain=${opts.domain}&count=${opts.count}` | |
}); | |
}, | |
// Selecting only new Posts | |
getUpds:function(prevJson, newJson){ | |
var prevPosts = prevJson ? JSON.parse(prevJson).response : null; // [] | |
var newPosts = JSON.parse(newJson).response; // [] | |
const prevCounter = prevPosts ? prevPosts.splice(0,1) : null; //remove first (counter) | |
const newCounter = newPosts.splice(0,1); //remove first (counter) | |
// pinned post is ignored | |
const prevDate = prevPosts ? ( prevPosts[0].is_pinned ? prevPosts[1].date : prevPosts[0].date ) : null; | |
const newDate = newPosts[0].is_pinned ? newPosts[1].date : newPosts[0].date; | |
const updsArr = []; | |
updsArr.push(newCounter) | |
for (var i = 0, len = newPosts.length; i < len; i++) { | |
if ( newPosts[i].date > prevDate ) updsArr.push(newPosts[i]); | |
} | |
var updatedData = { | |
response: updsArr | |
} | |
if (!updatedData.response[1]) return null; // no actual data | |
return updatedData; | |
}, | |
// What data to set in firebase | |
jsonHandler: function(json){ | |
var fbObj = {}; | |
JSON.parse(json).response.forEach(function(post) { | |
if (post.text) { | |
fbObj[post.id] = {}; | |
fbObj[post.id].date = post.date; | |
fbObj[post.id].text = post.text; | |
} | |
}); | |
return fbObj; | |
} | |
} | |
} | |
//---------------- Poller ----------------- | |
const vkPoller = function(vkMethod, vkMethodOpts, fbRef, interval) { | |
const that = this; | |
var prevJson = null; //for cheking updates; | |
this.data = ''; | |
var canIterate = true; | |
const iterator = setInterval(() => { | |
if (canIterate) { | |
var vkResJson = ''; | |
(function(vkReq){ | |
vkReq.on('response', (vkRes) => { | |
canIterate = false; | |
console.log(`VK Res Status: ${vkRes.statusCode}`); | |
console.log(`VK Res Headers: ${JSON.stringify(vkRes.headers)}`); | |
vkRes.setEncoding('utf8'); | |
vkRes.on('data', (chunk) => { | |
vkResJson += chunk; // collecting data from chunks | |
}); | |
vkRes.on('end', () => { // when all data is collected handling JSON for Fb and setting it | |
var newData; | |
if (vkResJson !== prevJson) { | |
newData = VK[vkMethod].getUpds(prevJson, vkResJson); | |
if (newData) fbRef.push(newData); | |
prevJson = vkResJson; | |
} | |
//var fbJson = VK.wallGet.jsonHandler(vkResJSON); | |
that.data = newData; // for Web-Server | |
canIterate = true; | |
}); | |
}); | |
vkReq.on('error', (e) => { | |
console.log(`problem with request: ${e.message}`); | |
}); | |
vkReq.end(); | |
})( http.request(VK[vkMethod].reqOpts(vkMethodOpts)) ); | |
} | |
}, interval); | |
} | |
//var puritymeth = new vkPoller('wallGet', {domain: 'puritymeth', count: 2}, FB_ROOT, 5 * 1000); | |
var pikabu = new vkPoller('wallGet', {domain: 'pikabu', count: 2}, FB_ROOT, 5 * 1000); | |
/* | |
setTimeout(function(){ | |
clearInterval(iterator); | |
console.log('Polling aborted by timeout'); | |
}, 20 * 1000);*/ | |
//------------ Web-Server (for testing) ---------------- | |
/* | |
const server = http.createServer(); | |
server.on('request', function(req, res){ | |
res.writeHead(200, { | |
'Content-type': 'text/json; charset=utf-8' // json, cyrillic | |
}); | |
res.write(pikabu.data); | |
res.end(); | |
}); | |
server.listen(80); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment