Skip to content

Instantly share code, notes, and snippets.

@maxogden maxogden/index.js Secret
Created Mar 15, 2013

Embed
What would you like to do?
var request = require('request').defaults({json: true})
var stream = require('stream')
var url = require('url')
var qs = require('querystring')
var last = ""
module.exports = function(subreddit) {
var readStream = new stream.Readable({objectMode: true})
function importSubredditYoutubes(subreddit, callback) {
request(subreddit + "?after=" + last, function(e, r, body) {
if (e) return readStream.push(null)
last = body.data.after
var youtubes = body.data.children.forEach(function(item) {
if (!item.data.media) return
var oembed = item.data.media.oembed
if (!oembed || oembed.provider_name !== "YouTube") return
if (!oembed.url) return
item._id = qs.parse(url.parse(oembed.url).query).v
console.log('pushing item', item)
readStream.push(item)
})
})
}
readStream._read = function(n) {
// reddit doesnt want more than 1 request per 2 seconds
(function loop() {
importSubredditYoutubes(subreddit, function(err, data) {
if (err) return console.error(err)
setTimeout(loop, 2000)
})
})()
}
return readStream
}
pizzacats:subreddit-youtubes maxogden$ node test.js
pushing item { kind: 't3',
data:
{ domain: 'youtube.com',
banned_by: null,
media_embed:
{ content: '<iframe width="600" height="338" src="http://www.youtube.com/embed/IpdoRUMeshw?feature=oembed" frameborder="0" allowfullscreen></iframe>',
width: 600,
scrolling: false,
height: 338 },
subreddit: 'Documentaries',
selftext_html: null,
selftext: '',
likes: null,
link_flair_text: null,
id: '1abene',
clicked: false,
title: 'Welcome to India 2012 (BBC doc that shows another world)',
media: { type: 'youtube.com', oembed: [Object] },
score: 12,
approved_by: null,
over_18: false,
hidden: false,
thumbnail: 'http://a.thumbs.redditmedia.com/xxyS8MMvDLO6RHOB.jpg',
subreddit_id: 't5_2qhlh',
edited: false,
link_flair_css_class: null,
author_flair_css_class: null,
downs: 3,
saved: false,
is_self: false,
permalink: '/r/Documentaries/comments/1abene/welcome_to_india_2012_bbc_doc_that_shows_another/',
name: 't3_1abene',
created: 1363332348,
url: 'https://www.youtube.com/watch?v=IpdoRUMeshw',
author_flair_text: null,
author: 'SirSie',
created_utc: 1363303548,
ups: 15,
num_comments: 2,
num_reports: null,
distinguished: null },
_id: 'IpdoRUMeshw' }
TypeError: invalid data
at WriteStream.Socket.write (net.js:580:11)
at write (_stream_readable.js:547:24)
at flow (_stream_readable.js:556:7)
at Readable.pipeOnReadable (_stream_readable.js:588:5)
at Readable.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:382:10)
at emitReadable (_stream_readable.js:377:5)
at readableAddChunk (_stream_readable.js:142:7)
at Readable.push (_stream_readable.js:112:10)
at /Users/maxogden/src/subreddit-youtubes/index.js:21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.