This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
news_fetch.js
133 lines (112 loc) · 3.91 KB
/
news_fetch.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const cheerio = require('cheerio');
const https = require('https')
const testing = false;
const SOCCER_ETHIOPIA_NEWS_URL = "https://www.soccerethiopia.net/wp-json/wp/v2/posts";
/**
* Entry point for news_fetch api
* @return {Array}
*/
exports.getLatestNews = async function () {
if (testing) {
return new Promise(resolve => resolve(getListOfNewsContent(readNewsFromFile())));
} else {
return await readNewsFromWeb().then(data => getListOfNewsContent(data));
}
};
/**
* Entry point for news_fetch api
* @return {string} - json
*/
exports.getLatestNewsJSON = async function () {
return JSON.stringify(await this.getLatestNews());
};
/**
* Entry point for news_fetch api
* @return {Array}
*/
exports.getSingleNewsContent = async function (newsId) {
if (testing) {
return new Promise(resolve => resolve(getListOfNewsContent(readNewsFromFile())));
} else {
return await readNewsFromWeb().then(data => getListOfNewsContent(data));
}
};
/**
* Entry point for news_fetch api
* @return {string} - json
*/
exports.getSingleNewsContentJSON = async function (newsId) {
return JSON.stringify(await this.getSingleNewsContent(newsId));
};
async function forEach(arr, callback) {
return Promise.all(arr.map(async item => (await callback(item))));
}
async function getListOfNewsContent(news) {
let newsItems = [];
const newsJson = JSON.parse(news);
await forEach(newsJson, await (async newsItem => {
let categoryNames = [];
const { author , featured_media, categories } = newsItem;
let authorData = await requestGETURL(`https://www.soccerethiopia.net/wp-json/wp/v2/users/${author}`).catch(e => {});
authorData = JSON.parse(authorData);
let featuredMediaData = {}
featuredMediaData = await requestGETURL(`https://www.soccerethiopia.net/wp-json/wp/v2/media/${featured_media}`).catch(e => console.log(e));
featuredMediaData = JSON.parse(featuredMediaData)
await forEach(categories, async category => {
let categoryData = await requestGETURL(`https://www.soccerethiopia.net/wp-json/wp/v2/categories/${category}`).catch(e => {})
categoryData = JSON.parse(categoryData)
categoryNames.push(categoryData['name'])
})
let returnableNewsItem = {}
returnableNewsItem['newsAuthorName'] = authorData['name']
returnableNewsItem['newsId'] = newsItem['id']
returnableNewsItem['newsImage'] = getImageLink(featuredMediaData)
returnableNewsItem['newsTitle'] = newsItem['title']['rendered']
returnableNewsItem['newsAuthorLink'] = authorData['link']
returnableNewsItem['newsTags'] = categoryNames
returnableNewsItem['newsPublishedOn'] = newsItem['date']
returnableNewsItem['newsContent'] = newsItem['content']['rendered']
newsItems = newsItems.concat(returnableNewsItem);
})).catch(e => console.log(e));
console.log('WAIT A SEC: ' + newsItems.length)
return newsItems;
}
function getImageLink(holder) {
if (holder['guid']) return holder['guid']['rendered']
else return ""
}
/**
* if (testing) -> read from offline file
* @return {string}
* @api Public
*/
function readNewsFromFile () {
const fx = require('fs');
try {
return fx.readFileSync('./news_dummy.json', 'utf-8');
} catch (e) {
console.error("file_" , e);
return ""
}
}
/**
* if (!testing) -> read data from website
* @return {Promise<String>}
*/
async function readNewsFromWeb () {
return requestGETURL(SOCCER_ETHIOPIA_NEWS_URL)
}
async function requestGETURL(url) {
return new Promise(resolve => https.get(url, res => {
let data = "";
res.on("error", err => {
resolve(err.reason);
});
res.on('data', d => {
data += d;
});
res.on("end" , () => {
resolve(data);
})
})).catch(err => console.log(err));
}