Created
April 2, 2018 13:19
-
-
Save lvturner/ffdb7ac47d1621b16c2dbce7122c81be 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
| 'use strict'; | |
| process.setMaxListeners(100); | |
| let https = require ('https'); | |
| // ********************************************** | |
| // *** Update or verify the following values. *** | |
| // ********************************************** | |
| // Replace the accessKey string value with your valid access key. | |
| let accessKey = '<Microsoft Token>'; // Not used right now | |
| let tiingoKey = 'Token <Tiingo-Token>'; | |
| let tiingoUri = 'api.tiingo.com'; | |
| let tiingoPath = '/tiingo/news'; | |
| const Article = require('newspaperjs').Article; | |
| const language = require('@google-cloud/language'); | |
| const client = new language.LanguageServiceClient(); | |
| // Replace or verify the region. | |
| // You must use the same region in your REST API call as you used to obtain your access keys. | |
| // For example, if you obtained your access keys from the westus region, replace | |
| // "westcentralus" in the URI below with "westus". | |
| // NOTE: Free trial access keys are generated in the westcentralus region, so if you are using | |
| // a free trial access key, you should not need to change this region. | |
| let uri = 'southeastasia.api.cognitive.microsoft.com'; | |
| let path = '/text/analytics/v2.0/sentiment'; | |
| let response_handler = function (response) { | |
| let body = ''; | |
| response.on ('data', function (d) { | |
| body += d; | |
| }); | |
| response.on ('end', function () { | |
| let body_ = JSON.parse (body); | |
| let body__ = JSON.stringify (body_, null, ' '); | |
| let count = 1; // god I'm lazy | |
| let total = 0; | |
| for(let x = 0; x < body_.documents.length; x++) { | |
| total += body_.documents[x].score; | |
| count++; | |
| } | |
| console.log("Average sentiment value of: " + tickers + " is: " + (total/count)); | |
| }); | |
| response.on ('error', function (e) { | |
| console.log ('Error: ' + e.message); | |
| }); | |
| }; | |
| let tiingo_response_handler = function (response) { | |
| let body = ''; | |
| response.on ('data', function (d) { | |
| body += d; | |
| }); | |
| response.on ('end', function () { | |
| let body_ = JSON.parse (body); | |
| let articles = []; | |
| for(let x = 0; x < body_.length; x++) { | |
| let document = { | |
| type: 'PLAIN_TEXT', | |
| content: body_[x].title + "\n" + body_[x].description | |
| }; | |
| // articles.push(client.analyzeSentiment({ document: document})); | |
| articles.push(Article(body_[x].url).then(response => { | |
| let document = { | |
| type: 'PLAIN_TEXT', | |
| content: response.text | |
| }; | |
| return client.analyzeSentiment({document :document}); | |
| }).catch(reason => { /* who needs error handling anyway? */ })); | |
| } | |
| Promise.all(articles).then(values => { | |
| try { | |
| let totMag = 0; | |
| let totSent = 0; | |
| let ranks = { | |
| negative: 0, | |
| neutral: 0, | |
| positive: 0 | |
| }; | |
| let count = 0; | |
| for (let x = 0; x < values.length; x++) { | |
| if(values[x]) { | |
| count++; | |
| let obj = values[x][0]; | |
| let score = obj.documentSentiment.score; | |
| if(score < -0.25) { | |
| ranks.negative++; | |
| } | |
| if(score > -0.25 && score < 0.25) { | |
| ranks.neutral++; | |
| } | |
| if(score > 0.25) { | |
| ranks.positive++; | |
| } | |
| } | |
| } | |
| console.log("Total/Actual: " + values.length + "/" + count); | |
| console.log("Negative articles: " + ranks.negative); | |
| console.log("Neutral articles: " + ranks.neutral); | |
| console.log("Positive articles: " + ranks.positive); | |
| // console.log("Average Magnitude: " + avgMag + " Average Sentiment: " + avgSent); | |
| prompt(); | |
| // prompt(); | |
| // console.log(JSON.stringify(values)); | |
| } catch(error) { | |
| console.log(error); | |
| } | |
| }).catch(reason => { | |
| //who cares about failures! Losers | |
| // console.log(reason); | |
| }); | |
| // get_sentiments (documents); | |
| }); | |
| response.on ('error', function (e) { | |
| console.log ('Error: ' + e.message); | |
| }); | |
| }; | |
| let get_sentiments = function (documents) { | |
| let body = JSON.stringify (documents); | |
| let request_params = { | |
| method : 'POST', | |
| hostname : uri, | |
| path : path, | |
| headers : { | |
| 'Ocp-Apim-Subscription-Key' : accessKey, | |
| } | |
| }; | |
| console.log("Getting sentiment analysis from Microsoft API..."); | |
| let req = https.request (request_params, response_handler); | |
| req.write (body); | |
| req.end (); | |
| }; | |
| let get_tiingo_news = function(tickers) { | |
| let request_params = { | |
| method : 'GET', | |
| hostname : tiingoUri, | |
| path : tiingoPath + '?tickers=' + tickers, | |
| headers : { | |
| 'Content-Type': 'application/json', | |
| 'Authorization': tiingoKey, | |
| } | |
| }; | |
| console.log("Getting Tiingo news for " + tickers); | |
| let req = https.request (request_params, tiingo_response_handler); | |
| // req.write (); | |
| req.end (); | |
| }; | |
| const readline = require('readline'); | |
| const rl = readline.createInterface({ | |
| input: process.stdin, | |
| output: process.stdout | |
| }); | |
| let prompt = () => { | |
| rl.question("What ticker do you want to look up? ", (ans) => { | |
| get_tiingo_news(ans); | |
| }); | |
| }; | |
| prompt(); | |
| // get_tiingo_news("MARK"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment