-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.js
51 lines (50 loc) · 1.35 KB
/
feed.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
const request = require('request'),
config = require('./config'),
md5 = require('md5'),
fs = require('fs'),
async = require('async'),
Parser = require('rss-parser'),
parser = new Parser()
module.exports = {
get: (task, callback) => {
const state_file = 'state/' + md5(task.uri)
parser.parseURL(task.uri, (err, feed) => {
let obj = {
task: task,
obj: null
}
if ( err || !feed.items.length ) callback(null, obj)
else {
const item = feed.items[0],
id = task.id(item, md5),
state_exists = fs.existsSync(state_file),
state = state_exists ? fs.readFileSync(state_file).toString() : ''
if ( !state_exists || !task.exists(state, id) ) {
if ( !fs.existsSync('state') ) {
fs.mkdirSync('state');
}
fs.writeFileSync(
state_file,
task.save_state(state, id)
)
obj.obj = Object.assign({}, task.const)
for ( key in task.map ) {
const value = task.map[key]
if ( typeof(value) == 'string' ) {
obj.obj[key] = item[value]
} else obj.obj[key] = value(item)
}
const mod_tasks = Object.keys(config.modules)
.filter(mod => task[mod])
.map(mod => async.apply(
require(config.modules[mod]).apply,
task[mod], obj, item
))
async.series(mod_tasks, ()=>{
callback(null, obj)
})
} else callback(null, obj)
}
})
}
}