npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

youtube-comments-stream

v1.4.6

Published

Scrape comments (including replies) from a YouTube video and present as a stream

Downloads

16

Readme

youtube-comments-stream

npm version

Scrape comments (including replies) from a YouTube video and present as a stream

Purpose

This is a stream wrapper around youtube-comment-api, which itself is a Promise wrapper around youtube-comments-task

It presents a readable object stream returning individual comments as objects. For a full description of the returned comment object see documentation for youtube-comments-task

Installation

npm install --save youtube-comments-stream

API Summary

The module contains four functions:

Examples

Read all comments from a video

const commentsStream = require('youtube-comments-stream');

const VIDEO_ID = 'HVv-oBN6AWA';

const stream = commentsStream(VIDEO_ID);

stream.on('data', function (comment) {
	console.log(comment.text);
});

stream.on('error', function (err) {
	console.error('ERROR READING COMMENTS:', err)
});

stream.on('end', function () {
	console.log('NO MORE COMMENTS');
	process.exit();
});

Read only the first 5 comments from a video

const commentsStream = require('youtube-comments-stream');

const VIDEO_ID = 'HVv-oBN6AWA';
const MAX_COMMENTS = 5;

const limit = commentsStream.limit(MAX_COMMENTS);
const stream = commentsStream.get(VIDEO_ID).pipe(limit);

stream.on('data', function (comment) {
	console.log(comment.text);
});

/* ... */

Read comments by author

(that appear in the first 1000 video comments)

const commentsStream = require('youtube-comments-stream');

const VIDEO_ID = 'kpaFizGUJg8';
const MAX_COMMENTS = 1000;
const AUTHOR = 'nokomis mn';

const limit = commentsStream.limit(MAX_COMMENTS);
const filter = commentsStream.filter(comment => comment.author === AUTHOR);
const stream = commentsStream.get(VIDEO_ID).pipe(limit).pipe(filter);

stream.on('data', function (comment) {
	console.log(comment.text);
});

/* ... */

Read the first 8 comments that contain the text "NASA"

const commentsStream = require('youtube-comments-stream');

const VIDEO_ID = 'ZuToYSYdJS0';
const REGEX = /nasa/i;
const MAX_COMMENTS = 8;

const filter = commentsStream.filter(comment => REGEX.test(comment.text));
const limit = commentsStream.limit(MAX_COMMENTS);
const stream = commentsStream.get(VIDEO_ID).pipe(filter).pipe(limit);

stream.on('data', function (comment) {
	console.log(comment.text);
});

/* ... */

API Details

get(VIDEO_ID)

Get a readable stream of all comments from the video

VIDEO_ID is the "v" parameter of the video URL, eg if the URL is https://www.youtube.com/watch?v=HVv-oBN6AWA then it is "HVv-oBN6AWA"

Module alias

For convenience the module itself is an alias for get(), ie

const commentsStream = require('youtube-comments-stream');

const stream = commentsStream(VIDEO_ID);
// ...is exactly the same as
const stream = commentsStream.get(VIDEO_ID);

limit(MAX_COMMENTS)

Get a transform stream to limit the number of items in a comments stream

MAX_COMMENTS is a numeric value. The stream will end once it has provided this number of comments or if it ends naturally before reaching this amount

filter(FILTER_FN)

Get a transform stream to filter a comments stream

FILTER_FN is a function that receives a comment object as a parameter and returns a boolean, eg

function isCommentByJohn(comment) {
	return comment.author === 'john';
}

const filter = commentsStream.filter(isCommentByJohn);
const stream = commentsStream.get(VIDEO_ID).pipe(filter);

or, using ES6, we can write this much more succinctly as

const filter = commentsStream.filter(comment => comment.author === 'john');
const stream = commentsStream.get(VIDEO_ID).pipe(filter);

Order of filter/limit transforms

It should be clear that the order of transforms is significant, ie

const limit = commentsStream.limit(10);
const filter = commentsStream.filter(comment => comment.author === 'john');

// get first 10 comments by john
const stream1 = commentsStream.get(VIDEO_ID).pipe(filter).pipe(limit);

// only get comments by john that are within the first 10 comments
const stream2 = commentsStream.get(VIDEO_ID).pipe(limit).pipe(filter);

mock(COMMENTS)

Get a mock comments stream (for testing)

COMMENTS is an array of objects that will be returned by the mock stream, eg

const mockComments = [
	{ author: 'john', text: 'Some comment' },
	{ author: 'jane', text: 'Another comment' },
	{ author: 'bob', text: 'An answer by bob' }
];

const mockStream = commentsStream.mock(mockComments);

Mocking errors

If a mock comment object contains a type which contains the word "error" then this will cause the mock stream to emit an error when it reads this comment, eg

const mockComments = [
	{ author: 'john', text: 'Some comment' },
	{ author: 'jane', text: 'Another comment' },
	{ type: 'some error', message: 'whoops, something has gone wrong' }, // will emit an error
	{ author: 'bob', text: 'An answer by bob' }
];

const mockStream = commentsStream.mock(mockComments);