Skip to content

Commit 9698231

Browse files
authored
Merge pull request sanscript-tech#242 from TaniaMalhotra/news-assistant-nodejs
News assistant in node
2 parents 39f5157 + 8cee3a8 commit 9698231

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

JavaScript/news-assistant/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package-lock.json
2+
node_modules

JavaScript/news-assistant/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# News assistant
2+
- - - - - - - - -
3+
## Aim
4+
5+
The aim of this script is to read out the top headlines of your favourite news category</br>
6+
Categories you can choose from are:</br>
7+
- business
8+
- entertainment
9+
- general
10+
- health
11+
- science
12+
- sports
13+
- technology
14+
15+
## Requirements
16+
```npm install newsapi --save```</br>
17+
```npm install prompt-sync```</br>
18+
```npm install say```</br>
19+
20+
## To use:
21+
- ```node .```
22+
- Get and enter your API key by registering at https://newsapi.org/register
23+
- Enter the category from the above listed categories
24+
- Sit back and listen to the latest news headlines :)

JavaScript/news-assistant/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const NewsAPI = require('newsapi');
2+
const prompt = require('prompt-sync')();
3+
// automatically pick platform
4+
const say = require('say')
5+
6+
const api = prompt('Please enter your API key after registering at https://newsapi.org/register : ')
7+
const newsapi = new NewsAPI(api);
8+
9+
// To query top headlines
10+
// All options passed to topHeadlines are optional, but you need to include at least one of them
11+
str = ""
12+
const category = prompt('Please enter the category of news from the following: business, entertainment, general, health, science, sports and technology: ');
13+
newsapi.v2.topHeadlines({
14+
category: category,
15+
language: 'en',
16+
country: 'us'
17+
}).then(response => {
18+
//if number of rsponses are less than 5, we set l to that number otherwise it's 5
19+
l = response["articles"].length>5?5:response["articles"].length;
20+
for (i = 0; i < l; i++) {
21+
console.log(response["articles"][i]["title"])
22+
str = str + response["articles"][i]["title"] + ". "
23+
}
24+
say.speak(str)
25+
}
26+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const NewsAPI = require('newsapi');
2+
const prompt = require('prompt-sync')();
3+
// automatically pick platform
4+
const say = require('say')
5+
6+
const api = prompt('Please enter your API key after registering at https://newsapi.org/register : ')
7+
const newsapi = new NewsAPI(api);
8+
9+
// To query top headlines
10+
// All options passed to topHeadlines are optional, but you need to include at least one of them
11+
str = ""
12+
const category = prompt('Please enter the category of news from the following: business, entertainment, general, health, science, sports and technology: ');
13+
newsapi.v2.topHeadlines({
14+
category: category,
15+
language: 'en',
16+
country: 'us'
17+
}).then(response => {
18+
//if number of rsponses are less than 5, we set l to that number otherwise it's 5
19+
l = response["articles"].length>5?5:response["articles"].length;
20+
for (i = 0; i < 5; i++) {
21+
console.log(response["articles"][i]["title"])
22+
str = str + response["articles"][i]["title"] + ". "
23+
}
24+
say.speak(str)
25+
}
26+
);
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "news-assistant",
3+
"version": "1.0.0",
4+
"description": "A news assistant to read out your favourite news",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "Tania Malhotra",
10+
"license": "ISC",
11+
"dependencies": {
12+
"json-query": "^2.2.2",
13+
"prompt-sync": "^4.2.0",
14+
"say": "^0.16.0"
15+
}
16+
}

0 commit comments

Comments
 (0)