SHARE
TWEET

Untitled

a guest Oct 3rd, 2017 43 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-bbcc-242e09461cd7",
  9.     password: "akDWkHEJ4ne4",
  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.         for(var i in posts){
  24.             if(posts[i].message != null){
  25.                 var tone = getSentiment(posts[i].message);
  26.                 console.log(tone);
  27.                 var newPost = {
  28.                     id              :   posts[i].id,
  29.                     message         :   posts[i].message,
  30.                     messageSentiment:   "this should take json",
  31.                     from_id         :   posts[i].from.id,
  32.                     from_name       :   posts[i].from.name,
  33.                     created_time    :   posts[i].created_time,
  34.                     permalink_url   :   posts[i].permalink_url
  35.                 }
  36.                 //console.log(newPost);
  37.                 postsenti.insert(newPost, function(err,result){
  38.                     if(err){
  39.                     console.log(err);
  40.                     }
  41.                 });
  42.             }
  43.         }        
  44.     });
  45.  
  46.     function getSentiment(message){    
  47.         tone_analyzer.tone(
  48.             {text: message},
  49.             function(err,tone){
  50.                 if(err) console.log(err);
  51.                 documentTone = tone.document_tone;
  52.                 console.log(message);
  53.                 console.log("---------------");
  54.                 console.log(documentTone.tone_categories[0].tones);
  55.                 return documentTone.tone_categories[0].tones;
  56.             }
  57.         );  
  58.     }
  59.  
  60. // });
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top