Skip to content

Instantly share code, notes, and snippets.

@ds0nt
Created July 22, 2015 06:40
Show Gist options
  • Save ds0nt/2e61ef7ceaab92a563a6 to your computer and use it in GitHub Desktop.
Save ds0nt/2e61ef7ceaab92a563a6 to your computer and use it in GitHub Desktop.
#!/bin/babel-node
import jsdom from 'jsdom'
import fs from 'fs'
let subreddit = process.argv.length > 2
? process.argv[2]
: 'webware'
function grab(url) {
console.log('fetching', url)
return new Promise((ok, fail) => {
jsdom.env({
url,
done: (err, window) => {
if (err)
fail(err)
else
ok(window)
}
})
})
}
async function reddit(subreddit) {
let { document } = await grab(`http://www.reddit.com/r/${subreddit}/`)
let posts = []
let html = ''
let items = document.querySelectorAll('.thing a.title')
for (var i = items.length - 1; i >= 0; i--) {
posts.push({
title: items[i].innerHTML,
href: items[i].href,
})
html = items[i].outerHTML + html
}
return { posts, html };
}
async function run() {
let { posts, html } = await reddit(subreddit)
console.log(posts)
fs.appendFile(
'reddit.json',
JSON.stringify(posts),
() => console.log('appended file reddit.json'))
fs.appendFile(
'reddit.html',
html,
() => console.log('appended file reddit.html'))
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment