SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cron = require('node-cron');
  2. var mongojs = require('mongojs');
  3. var fbdb = mongojs('facebookdb');
  4. var postsenti = fbdb.collection('postsenti');
  5. var request = require('request');
  6. var ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
  7. var tone_analyzer = new ToneAnalyzerV3({
  8.     username: "79e7456f-1a77-4b9a",
  9.     password: "akDWkHEsdfdsfJ4ne4",
  10.     version_date: '2016-05-19',
  11.     headers: {
  12.     'X-Watson-Learning-Opt-Out': 'true'
  13.   }
  14. });
  15.  
  16. // cron.schedule('*/1 * * * *', function(){
  17. // console.log('running a task every minute');
  18.   var url = 'https://graph.facebook.com/v2.10/143790482303586?fields=id%2Cvisitor_posts.limit(10)%7Bfrom%2Cmessage%2Cpermalink_url%2Ccreated_time%7D&access_token=EAABz99EIvbYBAHUjb1zzKcY5Mnh40uDzmUsuNvyJJJ7VThsc66Dap6uOBi2nzTqcbSH31tdLbLsiZCyfdID5NLtTLKZBdLJKiqQVntHdUBZCUdz2sFlrHZAD0TJRnIMhRg5qVGBZABiZAi748Iwx9nbaGK9R47ZCk10kCbjTrJIiAZDZD"';
  19.     request(url,{json: true}, (err, res, body) => {
  20.         if(err){console.log(err);}
  21.         //console.log(body.posts.data);
  22.         var posts = body.visitor_posts.data;
  23.        
  24.         for(var i in posts){
  25.             if(posts[i].message != null){
  26.                 var tone = getSentiment(posts[i].message);
  27.                 console.log(tone);
  28.                 var newPost = {
  29.                     id              :   posts[i].id,
  30.                     message         :   posts[i].message,
  31.                     messageSentiment:   "100",
  32.                     from_id         :   posts[i].from.id,
  33.                     from_name       :   posts[i].from.name,
  34.                     created_time    :   posts[i].created_time,
  35.                     permalink_url   :   posts[i].permalink_url
  36.  
  37.                 }
  38.  
  39.                 //console.log(newPost);
  40.                 postsenti.insert(newPost, function(err,result){
  41.                     if(err){
  42.                     console.log(err);
  43.                     }
  44.                 });
  45.             }
  46.         }        
  47.     });
  48.  
  49.     function getSentiment(message){    
  50.         tone_analyzer.tone(
  51.             {text: message},
  52.             function(err,tone){
  53.                 if(err) console.log(err);
  54.                 documentTone = tone.document_tone;
  55.                 toneCategories = documentTone.tone_categories;
  56.                 var newpost = {};
  57.                 for(var j in toneCategories ){
  58.                     var tC = toneCategories[j];
  59.                     var tonez = tC.tones;
  60.                     for(var k in tonez){
  61.                         newpost[tonez[k].tone_name] = (tonez[k].score*100).toFixed(2);
  62.                     }
  63.                 }
  64.                 console.log("iuisdfisdgfiugsdfigsdfu")
  65.                 return "iuisdfisdgfiugsdfigsdfu";
  66.             }
  67.         );  
  68.     }
  69.  
  70. // });
  71. //https://graph.facebook.com/v2.10/143790482303586?fields=posts.limit(100)%7Bid%2Cmessage%2Cfrom%2Ccreated_time%2Cpermalink_url%2Ccomments.limit(100)%7Bid%2Cmessage%2Cfrom%2Ccreated_time%2Cpermalink_url%7D%7D&access_token=EAABz99EIvbYBAHUjb1zzKcY5Mnh40uDzmUsuNvyJJJ7VThsc66Dap6uOBi2nzTqcbSH31tdLbLsiZCyfdID5NLtTLKZBdLJKiqQVntHdUBZCUdz2sFlrHZAD0TJRnIMhRg5qVGBZABiZAi748Iwx9nbaGK9R47ZCk10kCbjTrJIiAZDZD
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top