youtube-comments-stream
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:
get(VIDEO_ID)
: Get a readable stream of all comments from the videolimit(MAX_COMMENTS)
: Get a transform stream to limit the number of items in a comments streamfilter(FILTER_FN)
: Get a transform stream to filter a comments streammock(COMMENTS)
: Get a mock comments stream (for testing)
Examples
Read all comments from a video
const commentsStream = ; const VIDEO_ID = 'HVv-oBN6AWA'; const stream = ; stream; stream; stream;
Read only the first 5 comments from a video
const commentsStream = ; const VIDEO_ID = 'HVv-oBN6AWA';const MAX_COMMENTS = 5; const limit = commentsStream;const stream = commentsStream; stream; /* ... */
Read comments by author
(that appear in the first 1000 video comments)
const commentsStream = ; const VIDEO_ID = 'kpaFizGUJg8';const MAX_COMMENTS = 1000;const AUTHOR = 'nokomis mn'; const limit = commentsStream;const filter = commentsStream;const stream = commentsStream; stream; /* ... */
Read the first 8 comments that contain the text "NASA"
const commentsStream = ; const VIDEO_ID = 'ZuToYSYdJS0';const REGEX = /nasa/i;const MAX_COMMENTS = 8; const filter = commentsStream;const limit = commentsStream;const stream = commentsStream; stream; /* ... */
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 = ; const stream = ;// ...is exactly the same asconst stream = commentsStream;
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
{ return commentauthor === 'john';} const filter = commentsStream;const stream = commentsStream;
or, using ES6, we can write this much more succinctly as
const filter = commentsStream;const stream = commentsStream;
Order of filter/limit transforms
It should be clear that the order of transforms is significant, ie
const limit = commentsStream;const filter = commentsStream; // get first 10 comments by johnconst stream1 = commentsStream; // only get comments by john that are within the first 10 commentsconst stream2 = commentsStream;
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;
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;