-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
64 lines (59 loc) · 2.02 KB
/
server.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
// server.js
// where your node app starts
// init project
var express = require('express');
var bodyParser = require('body-parser');
var moment = require('moment');
var chrono = require('chrono-node');
var app = express();
var GitHubApi = require('github');
var moment = require('moment');
var github = new GitHubApi();
app.use(bodyParser.text());
app.post("/", (req, res) => {
if (req.query.token !== process.env.WEBHOOK_TOKEN)
res.sendStatus(400);
var body = req.body.replace(/\|\|\|/g, '\n');
body = body.replace('INSERT_DATE_HERE', moment(new Date()).subtract(10, 'hours').format("MMMM D, YYYY [at] HH:mmA"));
console.log(body)
if (body.includes('post via ifttt-ghpages')) {
// don't post commits from this script, causing an infinite post loop
res.sendStatus(200)
} else {
var title;
var titleSearch = body.match(/title: (.*?)\n/);
if (titleSearch && titleSearch.length > 0) {
title = titleSearch[1];
}
var dateSearch = body.match(/date: (.*?)\n/);
var date = dateSearch[1] ? chrono.parseDate(dateSearch[1]) : new Date();
var networkSearch = body.match(/network: (.*?)\n/);
if (networkSearch && networkSearch[1].includes('github_rss')) {
var regex = /href="\/webrender/gi;
body = body.replace(regex, 'href="https://github.com/webrender')
}
github.authenticate({
type: 'oauth',
token: process.env.GH_TOKEN
})
github.repos.createFile({
owner: process.env.GH_USER,
repo: process.env.GH_REPO,
path: `_posts/${moment(date).format('YYYY-MM-DD-HH-mm-ss-')}${title}.html`,
message: 'post via ifttt-ghpages',
content: new Buffer(body).toString('base64')
}, function(err, resp) {
if (err) {
console.log('error: ' + err);
res.sendStatus(500);
} else {
console.log('success: ' + resp);
res.sendStatus(200);
}
});
}
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function () {
console.log('Your app is listening on port ' + listener.address().port);
});