Advertisement
Guest User

Untitled

a guest
May 27th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Defining the required NodeJS dependencies
  2. var graph = require('fbgraph');
  3. var fs = require('fs');
  4. var config = require('config-ini');
  5.  
  6. // Defining variables needed globally
  7. var oldMessage = "";
  8. var fileLocation = "";
  9.  
  10. // Extanding static access token
  11. function extendAccessToken(){
  12.     graph.extendAccessToken ({
  13.         "client_id":        config.app.client_id,
  14.         "client_secret":    config.app.client_secret
  15.     }, function(err, facebookRes) {
  16.         if (err) {
  17.             console.log(err);
  18.         }
  19.     });
  20. }
  21.  
  22. // Main function that outputs the update to the terminal and writes it to a file
  23. function getFeed(){
  24.     graph.get("me/home?limit=1", function(err, res) {
  25.         if(err) {
  26.             console.log(err);
  27.         }
  28.         if ( typeof JSON.stringify(res).split('"')[5] !== 'undefined') {
  29.             if ( JSON.stringify(res).split('"')[5] != oldMessage ) {
  30.                 console.log(res);
  31.                 fs.appendFile(fileLocation, "\r\n"+JSON.stringify(res), function (err) {
  32.                     if (err) {
  33.                         console.log(err);
  34.                     }
  35.                 });
  36.                 console.log("******************************************");
  37.                 oldMessage = JSON.stringify(res).split('"')[5];;
  38.             }
  39.         }
  40.     });
  41. }
  42.  
  43. // What should be executed when this node is run
  44. config.load(['config.ini'], function(err) {
  45.     if (err) {
  46.         throw new Error(err); //File not found
  47.     }
  48.     graph.setAccessToken(config.app.accessToken);
  49.     fileLocation = config.file.location;
  50.     setInterval(extendAccessToken, 10000);
  51.     setInterval(getFeed, 3000);
  52. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement