This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (80 loc) · 2.75 KB
/
index.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
const rp = require('request-promise-native')
const parse = require('xml2js').parseString
const sequential = require('promise-sequential');
const throat = require('throat')
const config = require('./config.json')
const max = 100
function getReadUrl (base, feed) {
return `${base}?l=${max}&n=${feed}`
}
function getWriteUrl(base, feed, url) {
return `${base}add?n=${feed}&u=${url}`
}
let nbError = 0
function getXmlFeed (base, feed) {
const url = getReadUrl(base, feed)
return rp(url)
.then(xml => {
console.log(`< fetching ${url}`)
return new Promise((resolve, reject) => {
parse(xml, (err, result) => {
if (err) return reject(err)
return resolve(result.rss.channel && result.rss.channel.length && result.rss.channel[0].item || [])
})
})
})
}
function alreadyInTarget(array, link) {
return array.indexOf(link) !== -1
}
function cleanUrl (url) {
return url.replace(/&/g, '&').replace(/&?utm_.+?(&|$)/g, '')
}
sequential(config.feeds.map(feed => {
let itemsInTarget
return () => {
return getXmlFeed(config.target, feed)
.then(items => {
itemsInTarget = items.map(item => item.link[0])
})
.then(() => getXmlFeed(config.source, feed))
.then(items => {
return items.map(item => {
return item.link[0].replace('\n', '')
})
})
.then(links => {
return Promise.all(links.map(throat(2, rawLink => {
const link = cleanUrl(rawLink)
if (alreadyInTarget(itemsInTarget, link)) {
console.log(`= skipping link already in target : ${link}`)
return Promise.resolve()
} else {
const url = getWriteUrl(config.target, feed, link)
console.log(`> sending ${url}`)
return rp({
method: 'GET',
uri: url,
resolveWithFullResponse: true,
})
.then(response => {
console.log(`< reponse: ${response.statusCode} - ${url}`)
})
.catch(e => {
nbError++
console.error(`< reponse: ${e} - ${url}`)
if (nbError > 100) throw new Error('too much errors')
})
.then(_ => {
return new Promise(resolve => {
setTimeout(resolve, 500)
})
})
}
})))
})
.catch(e => {
console.error(e)
process.exit(1)
})
}}))