-
Notifications
You must be signed in to change notification settings - Fork 0
/
feedlyCalls.js
94 lines (83 loc) · 2.52 KB
/
feedlyCalls.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
const request = require('request')
const FAT = process.env.FEEDLY_ACCESS_TOKEN
const NEWS = process.env.NEWS
const TECH = process.env.TECH
const SPORTS = process.env.SPORTS
var News = require('./models/models').News;
var categories = {'news':"user/INSERT YOUR TOKEN HERE/category/News",
"tech":"user/INSERT YOUR TOKEN HERE/category/Tech",
"sports":"user/INSERT YOUR TOKEN HERE/category/sports"
}
function feedlyOptions(path){
return {
url: "https://cloud.feedly.com/v3/streams/contents?streamId="+path,
auth: {
'bearer': FAT
}
}
}
function getArticles(path){
return new Promise(function(resolve, reject){
console.log("here")
request.get(feedlyOptions(path), function(err, response, body){
if(response.statusCode!==200){
console.log("error requesting news~~~", err)
}
else{
body = JSON.parse(body);
resolve(
body.items
.filter(article => article.engagementRate > .1 || article.engagement > 500)
.map((filteredNews)=>{
if(filteredNews.originId.indexOf('http') === -1){
return false;
}
console.log("news", filteredNews)
return {title: filteredNews.title, url: filteredNews.originId}
})
)
}
})
})
.catch(err=>{
console.log("error retrieving articles")
Promise.reject(err);
})
}
Promise.all([getArticles(TECH), getArticles(NEWS), getArticles(SPORTS)])
.then(newsArrs=>{ //happening before getArticles is finished.
News.remove(function(err, sucess){
if(err){
console.log("ERROR REMOVING", err)
}
else{
new News({
tech: newsArrs[0],
news: newsArrs[1],
sports: newsArrs[2]
}).save(function(err){
if(err){
console.log("error saving news records", err)
}
else{
console.log("saved")
}
})
console.log("success")
}
})
})
.catch(
function(err){
console.log('error', err)
Promise.resolve(err)
}
)
News.find(function(err, body){
if(err){
console.log("error finding", err)
}
else{
console.log("body yo", body[0])
}
})