Skip to content

Instantly share code, notes, and snippets.

@ShogunPanda
Created April 22, 2015 18:14
Show Gist options
  • Save ShogunPanda/d031c6a7763c506649a1 to your computer and use it in GitHub Desktop.
Save ShogunPanda/d031c6a7763c506649a1 to your computer and use it in GitHub Desktop.
fb-comments-stealer
#!/usr/bin/env babel-node
import request from "request";
import chalkbars from "chalkbars";
process.on("uncaughtException", e =>{
console.log(e);
process.exit(2);
});
let run = function(){
let count = "";
request({
url: `https://graph.facebook.com/v2.3/${configuration.post}?fields=comments.summary(true)`,
headers: {
"Authorization": `Bearer ${configuration.token}`
}
}, (error, response, body) =>{
if(error)
throw(chalkbars.format("{{BF}} {{#C error}}Cannot poll comments count from Facebook:{{/C}} {{error}} {{EF}}", {error: error.message}));
if(response.statusCode != 200)
throw(chalkbars.format(
"{{BF}} {{#C error}}Facebook returned a non successful response while polling {{#C white}}(HTTP {{status}}){{/C}}:{{/C}} {{{body}}} {{EF}}",
{status: response.statusCode, body: body}
));
try{
count = JSON.parse(body).comments.summary.total_count.toString();
}catch(e){
throw(chalkbars.format("{{BF}} {{#C error}}Cannot read comments count from Facebook:{{/C}} {{error}} {{EF}}", {error: e.message}));
}
chalkbars.log("{{BI}} Facebook reported {{#C highlight}}{{count}}{{/C}} on the post.", {count: count});
let message = configuration.triggers[count];
if(message){
process.stdout.write(chalkbars.format(
"{{BI}} Sending the message for the trigger {{#C warn}}{{count}}{{/C}}: {{#C highlight}}{{{message}}}{{/C}} ...", {count: count, message: message}
));
request({
url: `https://graph.facebook.com/v2.3/${configuration.post}/comments`,
method: "POST",
headers: {
"Authorization": `Bearer ${configuration.token}`
},
json: true,
body: {
message: message
}
}, (error, response, body) =>{
if(error)
throw(chalkbars.format("{{EF}}\n{{BF}} {{#C error}}Cannot publish the message to Facebook:{{/C}} {{error}} {{EF}}", {error: error.message}));
if(response.statusCode != 200)
throw(chalkbars.format(
"{{EF}}\n{{BF}} {{#C error}}Facebook returned a non successful response while publishing {{#C white}}(HTTP {{status}}){{/C}}:{{/C}} {{{body}}} {{EF}}",
{status: response.statusCode, body: body}
));
chalkbars.log("{{EO}}");
configuration.triggers[count] = null;
});
}
});
};
let configuration = {};
try{
configuration = require("./fb-comments-stealer.json");
}catch(e){
chalkbars.log("{{BE}} {{#C error}}Please provide a valid JSON configuration file.{{/C}} {{EF}}");
process.exit(1);
}
chalkbars.log("{{BI}} Polling Facebook ...");
setInterval(run, configuration.pollInterval);
run();
{
"pollInterval": 15000,
"token": "TOKEN",
"post": "ID",
"triggers": {
"COUNT": "MESSAGE"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment