Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Last active December 21, 2015 19:49
Show Gist options
  • Save max-mapper/6356857 to your computer and use it in GitHub Desktop.
Save max-mapper/6356857 to your computer and use it in GitHub Desktop.
download all youtubes from a subreddit
var request = require('request').defaults({json: true})
var url = require('url')
var qs = require('querystring')
var fs = require('fs')
var async = require('async')
var path = require('path')
var ytdl = require('ytdl')
var subredditURL = "http://www.reddit.com/r/Documentaries/search.json?q=bbc+OR+attenborough+OR+pbs+OR+cats&restrict_sr=on&sort=relevance&t=all"
var last = ""
var next
var q = async.queue(function (vid, cb) {
console.log('downloading', vid.url)
var dl = ytdl(vid.url, { filter: function(format) { return format.container === 'mp4' } })
dl.pipe(fs.createWriteStream(path.join(__dirname, vid.title) + '.mp4'))
dl.once('end', function() {
console.log('finished', vid.url)
cb()
})
dl.once('error', function(e) {
console.log('error', vid.url, e)
cb()
})
}, 1)
q.drain = function() {
next()
}
function importSubredditYoutubes(subreddit, cb) {
request(subreddit + "&after=" + last, function(e, r, body) {
if (e) cb(e)
last = body.data.after
console.log('on page', last)
var youtubes = []
body.data.children.map(function(item) {
if (!item.data.media) return
var oembed = item.data.media.oembed
if (!oembed || oembed.provider_name !== "YouTube") return
if (!oembed.url) return
var title = item.data.title.replace(/[^a-zA-Z0-9-\s]/g, '').substr(0, 40)
q.push({url: item.data.url, title: title})
})
next = cb
})
}
// reddit doesnt want more than 1 request per 2 seconds
(function loop() {
importSubredditYoutubes(subredditURL, function(err, data) {
if (err) return console.error(err)
loop()
})
})()
{
"name": "youtuber",
"version": "0.0.0",
"description": "",
"main": "youtubez.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "BSD",
"dependencies": {
"request": "~2.27.0",
"ytdl": "~0.1.20",
"async": "~0.2.9"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment