Initial version
This commit is contained in:
Родитель
15bc5f7156
Коммит
eb491d69a3
|
@ -0,0 +1,2 @@
|
|||
node_modules
|
||||
*.eml
|
|
@ -0,0 +1,11 @@
|
|||
# Discourse + tl;dr newsletter
|
||||
|
||||
Scripts to fetch the [tl;dr newsletter](https://mana.mozilla.org/wiki/pages/viewpage.action?pageId=70485683) and post it to Discourse.
|
||||
|
||||
In operation here: https://discourse.mozilla.org/c/mozillians/tldr
|
||||
|
||||
File issues here: https://github.com/mozilla/discourse/issues
|
||||
|
||||
## Licence
|
||||
|
||||
[MPL 2.0](https://www.mozilla.org/MPL/2.0/)
|
|
@ -0,0 +1,53 @@
|
|||
var fs = require('fs')
|
||||
var mailparser = require('mailparser').simpleParser
|
||||
var request = require('request')
|
||||
var cheerio = require('cheerio')
|
||||
var TurndownService = require('turndown')
|
||||
|
||||
module.exports = class Newsletter {
|
||||
constructor () {
|
||||
this.title
|
||||
this.markdown
|
||||
}
|
||||
|
||||
from_link (link, date) {
|
||||
return new Promise((resolve, reject) => {
|
||||
request(link, (err, res, body) => {
|
||||
var $ = cheerio.load(body)
|
||||
this.title = $('title').text()
|
||||
|
||||
$('html').replaceWith($('tbody'))
|
||||
$('tbody').remove(':nth-child(-n+4)')
|
||||
$('tbody').remove(':last-child')
|
||||
|
||||
var turndownService = new TurndownService()
|
||||
this.markdown = turndownService.turndown($.html())
|
||||
|
||||
resolve(this)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
from_mail (data, date) {
|
||||
return new Promise((resolve, reject) => {
|
||||
mailparser(data, (err, mail) => {
|
||||
var $ = cheerio.load(mail.html)
|
||||
var link = $('a[href^="http://mailchi.mp/"]').attr('href').replace(/\?e\=.*/, '')
|
||||
|
||||
this.from_link(link).then((title, markdown) => {
|
||||
resolve(this)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
from_file (file, date) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(__dirname + file, (err, data) => {
|
||||
this.from_mail(data).then((title, markdown) => {
|
||||
resolve(this)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
var program = require('commander')
|
||||
var request = require('request')
|
||||
var Newsletter = require('./newsletter')
|
||||
|
||||
program
|
||||
.version('0.0.1')
|
||||
.option('--key [value]', 'Discourse API Key')
|
||||
.option('--link [value]', 'Link to newsletter')
|
||||
.option('--date [value]', 'Date of newsletter')
|
||||
.parse(process.argv)
|
||||
|
||||
new Newsletter().from_link(program.link).then(self => {
|
||||
data = {
|
||||
api_key: program.key,
|
||||
api_username: 'tldr',
|
||||
title: self.title,
|
||||
raw: self.markdown,
|
||||
category: 253
|
||||
}
|
||||
if (program.date) data.created_at = program.date
|
||||
request.post('https://discourse.mozilla.org/posts').form(data)
|
||||
})
|
Загрузка…
Ссылка в новой задаче