Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit eb491d6

Browse files
committed
Initial version
1 parent 15bc5f7 commit eb491d6

File tree

5 files changed

+1092
-0
lines changed

5 files changed

+1092
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
*.eml

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Discourse + tl;dr newsletter
2+
3+
Scripts to fetch the [tl;dr newsletter](https://mana.mozilla.org/wiki/pages/viewpage.action?pageId=70485683) and post it to Discourse.
4+
5+
In operation here: https://discourse.mozilla.org/c/mozillians/tldr
6+
7+
File issues here: https://github.com/mozilla/discourse/issues
8+
9+
## Licence
10+
11+
[MPL 2.0](https://www.mozilla.org/MPL/2.0/)

newsletter.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var fs = require('fs')
2+
var mailparser = require('mailparser').simpleParser
3+
var request = require('request')
4+
var cheerio = require('cheerio')
5+
var TurndownService = require('turndown')
6+
7+
module.exports = class Newsletter {
8+
constructor () {
9+
this.title
10+
this.markdown
11+
}
12+
13+
from_link (link, date) {
14+
return new Promise((resolve, reject) => {
15+
request(link, (err, res, body) => {
16+
var $ = cheerio.load(body)
17+
this.title = $('title').text()
18+
19+
$('html').replaceWith($('tbody'))
20+
$('tbody').remove(':nth-child(-n+4)')
21+
$('tbody').remove(':last-child')
22+
23+
var turndownService = new TurndownService()
24+
this.markdown = turndownService.turndown($.html())
25+
26+
resolve(this)
27+
})
28+
})
29+
}
30+
31+
from_mail (data, date) {
32+
return new Promise((resolve, reject) => {
33+
mailparser(data, (err, mail) => {
34+
var $ = cheerio.load(mail.html)
35+
var link = $('a[href^="http://mailchi.mp/"]').attr('href').replace(/\?e\=.*/, '')
36+
37+
this.from_link(link).then((title, markdown) => {
38+
resolve(this)
39+
})
40+
})
41+
})
42+
}
43+
44+
from_file (file, date) {
45+
return new Promise((resolve, reject) => {
46+
fs.readFile(__dirname + file, (err, data) => {
47+
this.from_mail(data).then((title, markdown) => {
48+
resolve(this)
49+
})
50+
})
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)