-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
196 lines (182 loc) · 6.55 KB
/
app.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const fs = require('fs');
const parser = require('rss-parser');
const git = require('simple-git');
const _ = require('lodash');
const jsonfile = require('jsonfile');
var dateFormat = require('dateformat');
const spon = require('./scraper/spon.js');
const tagesspiegel = require('./scraper/tagesspiegel.js');
const faz = require('./scraper/faz.js');
const welt = require('./scraper/welt.js');
const config = require('./config.json');
function getGit() {
return new Promise(function(fulfill, reject) {
fs.stat(config.path, function(err, stat) {
if (err === null) {
console.log('pulling git...');
git()
.cwd(config.path)
.pull('origin', config.branch, function(err, result) {
if (err) {
reject(err);
} else {
fulfill();
}
});
} else if (err.code === "ENOENT") {
console.log('cloning git...')
// clone and checkout configure branch
git().clone(config.repo, config.path, function(err, result) {
if (err) {
reject(err)
} else {
console.log('checking out branch ' + config.branch);
git(config.path).checkout(config.branch, function(err) {
if (err) {
reject(err)
} else {
fulfill();
}
});
fulfill();
}
});
} else {
reject(new Error('error checking path'));
}
});
});
}
function performScraping() {
console.log('running scrapers');
let news_provider = config.feeds.map((o) => {
o.scrape = getScraperByType(o.type);
return o;
});
let feedPromises = [];
news_provider.forEach(function (provider) {
feedPromises.push(new Promise(function (fulfill, reject) {
parser.parseURL(provider.feed, function (err, parsed) {
if (err)
reject(err);
else {
console.log(parsed.feed.title);
fulfill(parsed.feed.entries);
}
});
}));
});
return Promise.all(feedPromises)
.then(function (result) {
var entries = result.map(function (entries, index) {
var provider = news_provider[index];
provider['articles_rss'] = entries.map(function (entry) {
return {
'title': entry.title,
'link': entry.link,
'category': entry.category,
'guid': entry.guid,
'pubDate': entry.pubDate,
'description': entry.contentSnippet
}
});
return provider
});
return entries
})
.then(scrapeArticles)
}
function scrapeArticles(providers) {
return new Promise(function (fulfill, reject) {
let promises = [];
providers.forEach(function (provider) {
provider.articles_rss.forEach(function (article_rss) {
promises.push(
new Promise(function(fulfill, reject) {
provider.scrape(article_rss.link)
.then(function(article) {
article.provider_name = provider.name;
article.link = article_rss.link;
article.category = article_rss.category;
article.description = article_rss.description;
article.guid = article_rss.guid;
article.pubDate = article_rss.pubDate;
article.title = article_rss.title;
fulfill(article);
})
}))
})
});
Promise.all(promises)
.then(function (result) {
fulfill(result);
})
});
}
function getScraperByType(type) {
switch (type) {
case "spon":
return spon.scrape;
break;
case "tagesspiegel":
return tagesspiegel.scrape;
break;
case "faz":
return faz.scrape;
break;
case "welt":
return welt.scrape;
break;
default:
console.error("scraper for type " + type + "not found");
}
}
function writeGit(result) {
return new Promise(function(fulfill, reject) {
let writePromises = [];
console.log('write to git folder');
var provider = _.groupBy(result, 'provider_name');
Object.keys(provider).forEach(function(provider_name) {
var current = provider[provider_name];
console.log(provider_name);
if (!fs.existsSync(config.path + '/' + provider_name)){
fs.mkdirSync(config.path + '/' + provider_name);
}
current.forEach(function(article) {
let date = new Date(article.pubDate);
writePromises.push(new Promise(function(fulfill, reject) {
jsonfile.writeFile(config.path + '/'
+ provider_name + '/' +
dateFormat(date, "yyyy-mm-dd") + '-' +
article.title.replace(/[^a-z0-9]/gi, '_').toLowerCase() +
'.json', article, {}, function(err) {
if (err)
reject(err);
else
fulfill()
});
}))
});
});
Promise.all(writePromises)
.then(function() {
git(config.path)
.checkout(config.branch)
.add('.')
.commit('[BOT] add articles')
.push('origin', config.branch, function(err) {
if (err)
reject(err);
else
fulfill()
})
})
})
}
getGit()
.then(performScraping)
.then(writeGit)
.then(function() {
console.log('finished');
})
.catch(console.log);