1 | const LibraryError = require('../globals/LibraryError');
|
2 | const request = require('request');
|
3 | const xmlJS = require('xml-js');
|
4 |
|
5 | /**
|
6 | * Represents an individual news article. Static functions retrieve News objects.
|
7 | */
|
8 | class News {
|
9 |
|
10 | /**
|
11 | * Creates a new News instance.
|
12 | * @author Torrey Leonard <https://github.com/Ladinn>
|
13 | * @constructor
|
14 | * @param object
|
15 | * @property {String} symbol
|
16 | * @property {String} description
|
17 | * @property {Date} date
|
18 | * @property {String} source
|
19 | * @property {String} author
|
20 | * @property {String} url
|
21 | */
|
22 | constructor(object) {
|
23 | this.title = object.title;
|
24 | this.description = object.description;
|
25 | this.date = object.date;
|
26 | this.source = object.source;
|
27 | this.author = object.author;
|
28 | this.url = object.url;
|
29 | }
|
30 |
|
31 | /**
|
32 | * Returns an array of News objects for the given symbol.
|
33 | * @author Torrey Leonard <https://github.com/Ladinn>
|
34 | * @param {String} symbol
|
35 | * @returns {Promise<Array>}
|
36 | */
|
37 | static getFromYahoo(symbol) {
|
38 | return new Promise((resolve, reject) => {
|
39 | request({
|
40 | uri: 'http://feeds.finance.yahoo.com/rss/2.0/headline?s=' + symbol + '®ion=US&lang=en-US'
|
41 | }, (error, response, body) => {
|
42 | if (error) reject(error);
|
43 | else if (response.statusCode !== 200) reject(new LibraryError(body));
|
44 | else {
|
45 | const result = JSON.parse(xmlJS.xml2json(body, {compact: true, spaces: 0})).rss.channel.item;
|
46 | let array = [];
|
47 | result.forEach(o => {
|
48 | array.push(new News({
|
49 | title: o.title._text,
|
50 | description: o.description._text,
|
51 | date: new Date(o.pubDate._text),
|
52 | url: o.link._text
|
53 | }))
|
54 | });
|
55 | resolve(array);
|
56 | }
|
57 | })
|
58 | })
|
59 | }
|
60 |
|
61 | /**
|
62 | * Search through millions of articles from over 30,000 large and small news sources and blogs. This includes breaking news as well as lesser articles.
|
63 | * Returns an array of News objects for the given symbol from News API.
|
64 | * @author Torrey Leonard <https://github.com/Ladinn>
|
65 | * @param {String} apiKey - Your News API key, found for free here: https://newsapi.org/register
|
66 | * @param {Object} object - Further documentation can be found here: https://newsapi.org/docs/endpoints/everything
|
67 | * @property {String} q - Keywords or phrases to search for.
|
68 | * @property {String} sources - A comma-separated string of identifiers (maximum 20) for the news sources or blogs you want headlines from.
|
69 | * @property {String} domains - A comma-separated string of domains (eg bbc.co.uk, techcrunch.com, engadget.com) to restrict the search to.
|
70 | * @property {Date} from - A date and optional time for the oldest article allowed.
|
71 | * @property {Date} to - A date and optional time for the newest article allowed.
|
72 | * @property {String} language - Possible options: 'ar,' 'de,' 'en,' 'es,' 'fr,' 'he,' 'it,' 'nl,' 'no,' 'pt,' 'ru,' 'se,' 'ud,' 'zh'
|
73 | * @property {String} sortBy - Possible options: 'relevancy,' 'popularity,' 'publishedAt'
|
74 | * @property {Number} pageSize - The number of results to return per page. 20 is the default, 100 is the maximum.
|
75 | * @property {Number} page - Use this to page through the results.
|
76 | */
|
77 | static getAll(apiKey, object) {
|
78 | return new Promise((resolve, reject) => {
|
79 | if (!object) reject(new Error("You must pass an object containing your query to News API functions.\nVisit https://github.com/Ladinn/algotrader for more information."));
|
80 | else {
|
81 | object.apiKey = apiKey;
|
82 | request({
|
83 | uri: 'https://newsapi.org/v2/everything',
|
84 | qs: object
|
85 | }, (error, response, body) => {
|
86 | if (error) reject(error);
|
87 | else if (response.statusCode !== 200) reject(new LibraryError(body));
|
88 | else {
|
89 | const json = JSON.parse(body).articles;
|
90 | let array = [];
|
91 | json.forEach(o => {
|
92 | array.push(new News({
|
93 | title: o.title,
|
94 | description: o.description,
|
95 | author: o.author,
|
96 | source: o.source.name,
|
97 | url: o.url,
|
98 | date: new Date(o.publishedAt)
|
99 | }))
|
100 | });
|
101 | resolve(array);
|
102 | }
|
103 | })
|
104 | }
|
105 | })
|
106 | }
|
107 |
|
108 | /**
|
109 | * This endpoint provides live top and breaking headlines for a country, specific category in a country, single source, or multiple sources. You can also search with keywords. Articles are sorted by the earliest date published first.
|
110 | * Returns an array of News objects for the given symbol from News API.
|
111 | * @author Torrey Leonard <https://github.com/Ladinn>
|
112 | * @param {String} apiKey - Your News API key, found for free here: https://newsapi.org/register
|
113 | * @param {Object} object - Further documentation can be found here: https://newsapi.org/docs/endpoints/top-headlines
|
114 | * @property {String} q - Keywords or phrases to search for.
|
115 | * @property {String} category - Possible options: 'business,' 'entertainment,' 'general,' 'health,' 'science,' 'sports,' 'technology' (Cannot be mixed with sources parameter)
|
116 | * @property {String} country - The 2-letter ISO 3166-1 code of the country you want to get headlines for. (Cannot be mixed with sources parameter)
|
117 | * @property {String} sources - A comma-separated string of identifiers (maximum 20) for the news sources or blogs you want headlines from. (Cannot be mixed with country parameter)
|
118 | * @property {Number} pageSize - The number of results to return per page. 20 is the default, 100 is the maximum.
|
119 | * @property {Number} page - Use this to page through the results.
|
120 | */
|
121 | static getHeadlines(apiKey, object) {
|
122 | return new Promise((resolve, reject) => {
|
123 | if (!object) reject(new Error("You must pass an object containing your query to News API functions.\nVisit https://github.com/Ladinn/algotrader for more information."));
|
124 | else {
|
125 | object.apiKey = apiKey;
|
126 | request({
|
127 | uri: 'https://newsapi.org/v2/top-headlines',
|
128 | qs: object
|
129 | }, (error, response, body) => {
|
130 | if (error) reject(error);
|
131 | else if (response.statusCode !== 200) reject(new LibraryError(body));
|
132 | else {
|
133 | const json = JSON.parse(body).articles;
|
134 | let array = [];
|
135 | json.forEach(o => {
|
136 | array.push(new News({
|
137 | title: o.title,
|
138 | description: o.description,
|
139 | author: o.author,
|
140 | source: o.source.name,
|
141 | url: o.url,
|
142 | date: new Date(o.publishedAt)
|
143 | }))
|
144 | });
|
145 | resolve(array);
|
146 | }
|
147 | })
|
148 | }
|
149 | })
|
150 | }
|
151 |
|
152 | // GET
|
153 |
|
154 | /**
|
155 | * Using the URL provided for the news article, this will return the contents of that page.
|
156 | * @author Torrey Leonard <https://github.com/Ladinn>
|
157 | * @returns {Promise<String>}
|
158 | */
|
159 | getArticle() {
|
160 | const _this = this;
|
161 | return new Promise((resolve, reject) => {
|
162 | request({
|
163 | uri: _this.url
|
164 | }, (error, response, body) => {
|
165 | if (error) reject(error);
|
166 | else if (response.statusCode !== 200) reject(new LibraryError(body));
|
167 | else {
|
168 | _this.article = body;
|
169 | resolve(body);
|
170 | }
|
171 | })
|
172 | })
|
173 | }
|
174 |
|
175 | /**
|
176 | * @author Torrey Leonard <https://github.com/Ladinn>
|
177 | * @returns {String}
|
178 | */
|
179 | getTitle() {
|
180 | return this.title;
|
181 | }
|
182 |
|
183 | /**
|
184 | * @author Torrey Leonard <https://github.com/Ladinn>
|
185 | * @returns {String}
|
186 | */
|
187 | getDescription() {
|
188 | return this.description;
|
189 | }
|
190 |
|
191 | /**
|
192 | * @author Torrey Leonard <https://github.com/Ladinn>
|
193 | * @returns {Date}
|
194 | */
|
195 | getDate() {
|
196 | return this.date;
|
197 | }
|
198 |
|
199 | /**
|
200 | * @author Torrey Leonard <https://github.com/Ladinn>
|
201 | * @returns {String}
|
202 | */
|
203 | getSource() {
|
204 | return this.source;
|
205 | }
|
206 |
|
207 | /**
|
208 | * @author Torrey Leonard <https://github.com/Ladinn>
|
209 | * @returns {String}
|
210 | */
|
211 | getAuthor() {
|
212 | return this.author;
|
213 | }
|
214 |
|
215 | /**
|
216 | * @author Torrey Leonard <https://github.com/Ladinn>
|
217 | * @returns {String}
|
218 | */
|
219 | getURL() {
|
220 | return this.url;
|
221 | }
|
222 |
|
223 | }
|
224 |
|
225 | module.exports = News;
|