0.0.2 • Published 4 years ago

yt-scraper-badfix v0.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

yt-scraper

Modern YouTube scraper capable of retrieving video and channel info.

Install

First install through NPM:

npm install yt-scraper

Then import into your project:

const ytScraper = require("yt-scraper")

Usage

While the scraper will be updated as the YouTube video/channel page changes, all returned keys in objects described below should be treated as optional to avoid any errors causes by page changes.

Video Info

ytScraper.videoInfo(videoUrl, options = {})

ArgumentTypeDescription
videoUrlString(www.)youtube.com and youtu.be links are accepted alongside simply providing the video id without a url.
options.detailedChannelDataBoolShould make a second request to fetch channel data, is true by default.
options.includeRawDataBoolraw.player and raw.page objects returned with other data. If detailedChannelData then channel will also provide raw.page. More info on raw objects found below.
options.bypassIdCheckBoolMake the video request without verifying the id, only works with a url provided. Is false by default.

A promise will be returned, the resolved promise returns the object described in the table below and the rejected promise returns a custom error documented below.

KeyTypeDescription
idStringID of the video.
urlStringURL of the video (https://www.youtube.com/watch?v=ID).
titleStringTitle of the video.
descriptionStringDescription of the video, will contain \n.
categoryStringCategory of the video.
lengthNumberLength of the video in seconds.
thumbnailsArray\<Thumbnail>Array of thumbnails, most likely contains at least one. Documentation can be found below.
liveBoolIs video a live stream
rating.averageNumberYouTube provided average rating. Note that it is optional as rating can be disallowed.
rating.allowedNumberIs rating allowed.
rating.approx.likesNumberNumber of video likes. Note that the page may not provide detailed numbers and therefore may be rounded numbers. Use rating.average for a detailed average if needed.
rating.approx.dislikesNumberNumber of video dislikes. Note that the page may not provide detailed numbers and therefore may be rounded numbers. Use rating.average for a detailed average if needed.
privacy.privateBoolIs video private.
privacy.unlistedBoolIs video unlisted.
privacy.familySafeBoolIs video family safe (deemed by YouTube).
privacy.availableCountriesArray\<String>Array of two letter capitalized country coded where video can be viewed (e.g. US). Note it can contain 150+ items.
dates.publishedDateVideo published video. May vary from upload date.
dates.uploadedDateVideo uploaded date. May vary from publish date.
channelChannel objectAnother request will be made, this can be prevented by providing { detailedChannelData: false } option as the second argument. Channel object documentation can be found below.

Channel Info

ytScraper.channelInfo(channelUrl, options = {})

ArgumentTypeDescription
videoUrlStringAccepts (www.)youtube.com urls.
options.includeRawDataBoolraw.page objects returned with other data. More info on raw objects found below.

A promise will be returned, the resolved promise returns the object described in the table below and the rejected promise returns a custom error documented below.

KeyTypeDescription
idStringID of the channel.
urlStringVanity URL of the channel
nameStringName of the channel.
descriptionStringDescription of the channel. Can be optional is none is given.
locationStringCountry that the channel is based. Can be optional is channel doesn't set info.
joinedDateCreation date of channel
keywordsArray\<String>Array of keywords set by the channel.
approx.viewsNumberNumber of channel total views. Note that the page does not provide live numbers and may be outdated by a few hours.
approx.subscribersNumberNumber of channel subscribers. Note that the page does not provide detailed numbers and therefore will be rounded numbers (e.g. 728000).
images.avatarArray\<Thumbnail>Avatar for the channel provided in different sizes. Thumbnail documentation found below.
images.bannerArray\<Thumbnail>Banner art for the channel provided in different sizes. Can be undefined is none is provided.
privacy.familySafeBoolIs video family safe (deemed by YouTube).
privacy.availableCountriesArray\<String>Array of two letter capitalized country coded where video can be viewed (e.g. US). Note it can contain 150+ items.

Thumbnail

Thumbnails are provided by the YouTube API. Each thumbnail may represent the same image at a different size.

KeyTypeDescription
urlStringURL of the thumbnail, most likely a jpg.
widthNumberWidth of thumbnail.
heightNumberHeight of thumbnail.

Errors

Error NameDescription
YTScraperInvalidVideoURLAn invalid video url was provided.
YTScraperInvalidVideoIDAn invalid video id was provided. Can be bypassed for videoInfo method, check documentation.
YTScraperInvalidChannelIDAn invalid channel id was provided.
YTScraperInvalidChannelURLAn invalid channel url was provided.
YTScraperMissingDataThe webpage provided missing data and the scrape could not be completed.

Raw Objects

This module works by scraping two sources of JSON data embedded in a YouTube HTML page. The first is named ytInitialData and contains components that make the page work, e.g. strings, related videos, keyboard shortcuts. The second is from the YouTube player which contains technical info about a video, e.g. runtime, name, privacy settings.

This data is long and mostly human unreadable which is why the module collects it all together and returns a single object. However if you need more information it is worth checking if that info is present in the raw data YouTube provides. This is possible by passing the includeRawData option to either video or channel info methods. When passed a new raw property is present in the returned object which contains both the page (raw.page) and player (raw.player, only present for video scraping) info.

As previously mentioned raw objects are long and ever changing therefore making it a futile task to document them, however it only takes a few minutes to look through an object if you need to.

Contributing

We heavily value contributions, if you would like to contribute please feel free to put in a pull request.

Contributors

  • Thanks to elBarkey for bug fixes and stability contributions in v1.
0.0.2

4 years ago

0.0.1

4 years ago