-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.js
100 lines (84 loc) · 3.29 KB
/
news.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const moment = require("moment");
const config = require("config");
const NewsAPI = require("newsapi");
const axios = require('axios');
const api_key = process.env.NEWS_API_KEY || config.get("news_api_key");
const newsapi = new NewsAPI(api_key);
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI || config.get("mongoDB"), {
useNewUrlParser: true
});
const db = mongoose.connection;
db.on('connected', () => {console.log('mongo db successfully connected');});
db.on("disconnected", () => {console.log('mongo db successfully disconnected');});
db.on("error", () => {console.log('Error connecting to mongo db');});
// Use mongoose to save data to local database there by allowing an API
// that retrieves data from previous days
let fetch_date = null;
const searchNews = async term => {
let results = {status:false,payload:[],error:{}};
let today = moment().format("YYYY-MM-DD");
// checking if results is already saved in redis if yes then use the results
await newsapi.v2.everything({
q: term,
from: today,
language: "en",
sortBy: "relevancy",
}).then(response_json => {
results.payload = [...response_json.articles];
results.status = true;
}).catch(error => {
console.log(error.message);
results.payload = [];
results.error = {...error};
results.status = false;
});
console.log('Results Returned from search news api',results.payload.length);
return results.payload;
};
const articles_api = {
entertainment: `https://newsapi.org/v2/top-headlines?country=za&category=entertainment&apiKey=${api_key}`,
sports: `https://newsapi.org/v2/top-headlines?country=za&category=sports&apiKey=${api_key}`,
business: `https://newsapi.org/v2/top-headlines?country=za&category=business&apiKey=${api_key}`,
tech: `https://newsapi.org/v2/top-headlines?country=za&category=technology&apiKey=${api_key}`,
science: `https://newsapi.org/v2/top-headlines?country=za&category=science&apiKey=${api_key}`,
health: `https://newsapi.org/v2/top-headlines?country=za&category=health&apiKey=${api_key}`
};
const category_memory ={
fetch_date : null,
entertainment : [],
sports : [],
business : [],
tech : [],
science : [],
health : [],
};
async function get_blog_articles(category){
let results = null;
let apiRequest = null;
const today = moment().format("YYYY-MM-DD");
if ((fetch_date !== today) && (category_memory[category] && (category_memory[category].length < 1))){
apiRequest = articles_api[category];
await axios.get(apiRequest).then(result => {
if (result.status === 200){
return result.data;
}else{
throw new Error('there was an error fetching data');
}
}).then(json_articles => {
results = json_articles.articles;
category_memory[category] = results;
}).catch(error => {
console.log(error);
});
category_memory.fetch_date = today;
} else {
results = category_memory[category];
}
console.log('results returned from fetch blog articles',category);
return results;
};
module.exports = {
search: searchNews,
refine: get_blog_articles,
};