-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
96 lines (80 loc) · 2.58 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
87
88
89
90
91
92
93
94
95
96
import OpmlParser from 'opmlparser';
import { readdir, createReadStream } from 'fs';
import https from 'https';
var opmlparser = new OpmlParser();
readdir('./import', (err, files) => {
if (err) throw err;
files.forEach(file => {
if (!file.endsWith('.xml') && !file.endsWith('.opml')) return;
console.log(`Parsing ${file}`);
createReadStream(`./import/${file}`).pipe(opmlparser);
});
});
opmlparser.on('readable', function () {
const stream = this;
var feed;
while (feed = stream.read()) {
if (feed.xmlurl === undefined) continue; // Folder
postToOmnivore(feed);
}
});
const postToOmnivore = (feed) => {
const { folder, title, type, xmlurl } = feed;
const validAuth = checkProvidedEnvironmentVariables();
if (!validAuth) throw new Error('Missing required environment variables');
var options = {
hostname: process.env.OMNIVORE_HOST,
port: process.env.OMNIVORE_PORT,
path: process.env.OMNIVORE_GRAPH_QL_PATH,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': process.env.OMNIVORE_API_KEY
}
}
var requestData = buildRequest(title, folder, xmlurl);
const req = https.request(options, (res) => {
res.on('data', (d) => {
console.log(d.toString());
console.log(`:: Success :: Posted ${title} -> ${xmlurl} to Omnivore`);
})
})
req.on('error', (error) => {
console.error(`:: Failed :: Posting ${title} -> ${xmlurl} to Omnivore`)
console.error(error)
})
req.write(requestData)
req.end()
}
const checkProvidedEnvironmentVariables = () => {
if (!process.env.OMNIVORE_HOST) {
console.error('OMNIVORE_HOST not provided');
return false;
}
if (!process.env.OMNIVORE_PORT) {
console.error('OMNIVORE_PORT not provided');
return false;
}
if (!process.env.OMNIVORE_GRAPH_QL_PATH) {
console.error('OMNIVORE_GRAPH_QL_PATH not provided');
return false;
}
if (!process.env.OMNIVORE_API_KEY) {
console.error('OMNIVORE_API_KEY not provided');
return false;
}
return true;
}
const buildRequest = (title, folder, xmlurl) => {
return `
{
"query": "mutation Subscribe($input: SubscribeInput!) { subscribe(input: $input) {... on SubscribeSuccess {subscriptions {id}}... on SubscribeError {errorCodes}}}",
"variables": {
"input": {
"url": "${xmlurl}",
"subscriptionType": "RSS"
}
}
}
`;
}