discourse-tldr/post_newsletter

50 строки
1.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
var program = require('commander')
var request = require('request')
var Newsletter = require('./newsletter')
var fs = require('fs')
program
.version('0.0.1')
.option('--key [value]', 'Discourse API Key')
.option('--link [value]', 'Link to newsletter')
.option('--date [value]', 'Date of newsletter')
.option('--output', 'Print newsletter to output, rather than posting to Discourse')
.option('--input [path]', 'Use content from file')
.option('--discourse [url]', 'Base url of discourse instance, e.g. https://discourse.mozilla.org/')
.parse(process.argv)
new Newsletter().from_link(program.link).then(self => {
data = {
title: self.title,
raw: self.markdown,
category: 253
}
if (program.date) data.created_at = program.date
if (program.output) {
console.log(self.markdown)
} else if (program.input) {
fs.readFile(program.input, 'utf8', (err, file) => {
if (err) throw err
data.raw = file
post_to_discourse(data)
})
} else {
post_to_discourse(data)
}
})
function post_to_discourse (data) {
request.post({
url: `${program.discourse}posts`,
form: data,
headers: {
'Api-Key': program.key,
'Api-Username': 'tldr',
}
}, (err, res, body) => {
if (res.statusCode != 200) throw body
})
}