Skip to content

Instantly share code, notes, and snippets.


maxogden/index.js Secret

Created Mar 15, 2013
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 =
var youtubes = {
if (! return
var 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._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',
{ domain: '',
banned_by: null,
{ content: '<iframe width="600" height="338" src="" 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: '', oembed: [Object] },
score: 12,
approved_by: null,
over_18: false,
hidden: false,
thumbnail: '',
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: '',
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