Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

better-youtube-api

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

better-youtube-api - npm Package Compare versions

Comparing version 0.0.7 to 0.0.8

60

out/index.d.ts

@@ -10,3 +10,2 @@ import { youtube_v3 } from "googleapis"

/**
*
* @param token Your YouTube Data API v3 token. Don't share this with anybody.

@@ -16,8 +15,38 @@ */

/**
* Search videos on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
public searchVideos(searchTerm: string, maxResults: number): Promise<Video[] | Video>
/**
* Get a video object from the ID of a video.
* @param id The ID of the video.
*/
public getVideo(id: string): Promise<Video>
/**
* Get a video object from the url of a video.
* @param url The url of the video.
*/
public getVideoByUrl(url: string): Promise<Video>
/**
* Search channels on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
public searchChannels(searchTerm: string, maxResults: number): Promise<Channel[] | Channel>
/**
* Get a channel object from the ID of a channel.
* @param id The url of the channel.
*/
public getChannel(id: string): Promise<Channel>
/**
* Get a channel object from the url of a channel.
* @param url The url of the channel.
*/
public getChannelByUrl(url: string): Promise<Channel>

@@ -31,5 +60,25 @@

/**
* Get a playlist object from the ID of a playlist.
* @param id The url of the playlist.
*/
public getPlaylist(id: string): Promise<Playlist>
/**
* Get a playlist object from the url of a playlist.
* @param url The url of the playlist.
*/
public getPlaylistByUrl(url: string): Promise<Playlist>
/**
* Search playlists on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
public searchPlaylists(searchTerm: string, maxResults: number): Promise<Playlist[] | Playlist>
/**
* Get every single video in a playlist. Used mostly internally with `Playlist#getVideos`.
* @param playlistId The ID of the playlist.
*/
public getPlaylistItems(playlistId: string): Promise<Video[]>

@@ -288,6 +337,13 @@ }

constructor (youtube: YouTube, data: youtube_v3.Schema$Playlist | youtube_v3.Schema$SearchResult)
/**
* Gets every video in the playlist and assigns them to the `videos` property of it.
*/
public getVideos (): Promise<Video[]>
/**
* Fetches this playlist and reassigns this object to the new playlist object.
* Only useful if `this.full` is false, or if you want updated playlist info.
*/
public fetch (): Promise<this & Playlist>
}

@@ -31,4 +31,12 @@ "use strict";

}
/**
* Search videos on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
searchVideos(searchTerm, maxResults = 10) {
return __awaiter(this, void 0, void 0, function* () {
if (maxResults < 1 || maxResults > 50) {
return Promise.reject('Max results must be greater than 0 and less or equal to 50.');
}
const { data: results } = yield youtube.search.list({

@@ -51,2 +59,6 @@ q: searchTerm,

}
/**
* Get a video object from the ID of a video.
* @param id The ID of the video.
*/
getVideo(id) {

@@ -60,3 +72,3 @@ return __awaiter(this, void 0, void 0, function* () {

if (video.items.length === 0) {
Promise.reject('Video not found.');
return Promise.reject('Video not found.');
}

@@ -66,2 +78,6 @@ return new entities_1.Video(this, video.items[0]);

}
/**
* Get a video object from the url of a video.
* @param url The url of the video.
*/
getVideoByUrl(url) {

@@ -79,3 +95,3 @@ return __awaiter(this, void 0, void 0, function* () {

if (video.items.length === 0) {
Promise.reject('Video not found.');
return Promise.reject('Video not found.');
}

@@ -85,6 +101,11 @@ return new entities_1.Video(this, video.items[0]);

}
/**
* Search channels on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
searchChannels(searchTerm, maxResults = 10) {
return __awaiter(this, void 0, void 0, function* () {
if (maxResults < 1) {
throw new Error('Max results must be greater than 0.');
if (maxResults < 1 || maxResults > 50) {
return Promise.reject('Max results must be greater than 0 and less or equal to 50.');
}

@@ -108,2 +129,6 @@ const { data: results } = yield youtube.search.list({

}
/**
* Get a channel object from the ID of a channel.
* @param id The url of the channel.
*/
getChannel(id) {

@@ -117,3 +142,3 @@ return __awaiter(this, void 0, void 0, function* () {

if (channel.items.length === 0) {
Promise.reject('Channel not found.');
return Promise.reject('Channel not found.');
}

@@ -123,2 +148,6 @@ return new entities_1.Channel(this, channel.items[0]);

}
/**
* Get a channel object from the url of a channel.
* @param url The url of the channel.
*/
getChannelByUrl(url) {

@@ -136,3 +165,3 @@ return __awaiter(this, void 0, void 0, function* () {

if (channel.items.length === 0) {
Promise.reject('Channel not found.');
return Promise.reject('Channel not found.');
}

@@ -151,3 +180,3 @@ return new entities_1.Channel(this, channel.items[0]);

if (results.items.length === 0) {
Promise.reject('Channel not found.');
return Promise.reject('Channel not found/has no videos.');
}

@@ -160,2 +189,6 @@ for (let i = 0; i < results.items.length; i++) {

}
/**
* Get a playlist object from the ID of a playlist.
* @param id The url of the playlist.
*/
getPlaylist(id) {

@@ -169,3 +202,3 @@ return __awaiter(this, void 0, void 0, function* () {

if (playlist.items.length === 0) {
Promise.reject('Playlist not found.');
return Promise.reject('Playlist not found.');
}

@@ -175,2 +208,6 @@ return new entities_1.Playlist(this, playlist.items[0]);

}
/**
* Get a playlist object from the url of a playlist.
* @param url The url of the playlist.
*/
getPlaylistByUrl(url) {

@@ -188,3 +225,3 @@ return __awaiter(this, void 0, void 0, function* () {

if (playlist.items.length === 0) {
Promise.reject('Playlist not found.');
return Promise.reject('Playlist not found.');
}

@@ -194,4 +231,12 @@ return new entities_1.Playlist(this, playlist.items[0]);

}
/**
* Search playlists on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
searchPlaylists(searchTerm, maxResults = 10) {
return __awaiter(this, void 0, void 0, function* () {
if (maxResults > 50 || maxResults < 1) {
return Promise.reject('Max results must be 50 or below, and greater than 0.');
}
const { data: results } = yield youtube.search.list({

@@ -214,4 +259,21 @@ q: searchTerm,

}
getPlaylistItems(playlistId) {
/**
* Get `maxResults` videos in a playlist. Used mostly internally with `Playlist#getVideos`.
* If <= 0 or not included, returns all videos in the playlist.
* @param playlistId The ID of the playlist.
* @param maxResults The maximum amount of videos to get from the playlist.
*/
getPlaylistItems(playlistId, maxResults = -1) {
return __awaiter(this, void 0, void 0, function* () {
let full;
let videos = [];
if (maxResults <= 0) {
full = true;
}
else {
full = false;
}
if (maxResults > 50) {
return Promise.reject('Max results must be 50 or below.');
}
let { data: results } = yield youtube.playlistItems.list({

@@ -221,9 +283,7 @@ playlistId,

auth: this.token,
maxResults: 50
maxResults: full ? 50 : maxResults
});
if (results.items.length === 0) {
Promise.reject('Playlist not found.');
return Promise.reject('Playlist not found.');
}
let oldRes = results;
let videos = [];
const totalResults = results.pageInfo.totalResults;

@@ -235,5 +295,6 @@ const perPage = 50;

});
if (pages === 0) {
if (!full || pages === 0) {
return videos;
}
let oldRes = results;
for (let i = 0; i < pages; i++) {

@@ -240,0 +301,0 @@ const { data: newResults } = yield youtube.playlistItems.list({

2

package.json
{
"name": "better-youtube-api",
"version": "0.0.7",
"version": "0.0.8",
"description": "Better than simple ones.",

@@ -5,0 +5,0 @@ "main": "out/index.js",

@@ -23,3 +23,12 @@ import { google, youtube_v3 } from 'googleapis'

/**
* Search videos on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
public async searchVideos (searchTerm: string, maxResults: number = 10) {
if (maxResults < 1 || maxResults > 50) {
return Promise.reject('Max results must be greater than 0 and less or equal to 50.')
}
const { data: results } = await youtube.search.list({

@@ -46,2 +55,6 @@ q: searchTerm,

/**
* Get a video object from the ID of a video.
* @param id The ID of the video.
*/
public async getVideo (id: string) {

@@ -55,3 +68,3 @@ const { data: video } = await youtube.videos.list({

if (video.items.length === 0) {
Promise.reject('Video not found.')
return Promise.reject('Video not found.')
}

@@ -62,2 +75,6 @@

/**
* Get a video object from the url of a video.
* @param url The url of the video.
*/
public async getVideoByUrl (url: string) {

@@ -77,3 +94,3 @@ const id = parseUrl(url)

if (video.items.length === 0) {
Promise.reject('Video not found.')
return Promise.reject('Video not found.')
}

@@ -84,5 +101,10 @@

/**
* Search channels on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
public async searchChannels (searchTerm: string, maxResults: number = 10) {
if (maxResults < 1) {
throw new Error('Max results must be greater than 0.')
if (maxResults < 1 || maxResults > 50) {
return Promise.reject('Max results must be greater than 0 and less or equal to 50.')
}

@@ -111,2 +133,6 @@

/**
* Get a channel object from the ID of a channel.
* @param id The url of the channel.
*/
public async getChannel (id: string) {

@@ -120,3 +146,3 @@ const { data: channel } = await youtube.channels.list({

if (channel.items.length === 0) {
Promise.reject('Channel not found.')
return Promise.reject('Channel not found.')
}

@@ -127,2 +153,6 @@

/**
* Get a channel object from the url of a channel.
* @param url The url of the channel.
*/
public async getChannelByUrl (url: string) {

@@ -142,3 +172,3 @@ const id = parseUrl(url)

if (channel.items.length === 0) {
Promise.reject('Channel not found.')
return Promise.reject('Channel not found.')
}

@@ -158,3 +188,3 @@

if (results.items.length === 0) {
Promise.reject('Channel not found.')
return Promise.reject('Channel not found/has no videos.')
}

@@ -169,2 +199,6 @@

/**
* Get a playlist object from the ID of a playlist.
* @param id The url of the playlist.
*/
public async getPlaylist (id: string) {

@@ -178,3 +212,3 @@ const { data: playlist } = await youtube.playlists.list({

if (playlist.items.length === 0) {
Promise.reject('Playlist not found.')
return Promise.reject('Playlist not found.')
}

@@ -185,2 +219,6 @@

/**
* Get a playlist object from the url of a playlist.
* @param url The url of the playlist.
*/
public async getPlaylistByUrl (url: string) {

@@ -200,3 +238,3 @@ const id = parseUrl(url)

if (playlist.items.length === 0) {
Promise.reject('Playlist not found.')
return Promise.reject('Playlist not found.')
}

@@ -207,3 +245,12 @@

/**
* Search playlists on YouTube.
* @param searchTerm What to search for on YouTube.
* @param maxResults The maximum amount of results to find. Defaults to 10.
*/
public async searchPlaylists (searchTerm: string, maxResults: number = 10) {
if (maxResults > 50 || maxResults < 1) {
return Promise.reject('Max results must be 50 or below, and greater than 0.')
}
const { data: results } = await youtube.search.list({

@@ -230,3 +277,22 @@ q: searchTerm,

public async getPlaylistItems (playlistId: string) {
/**
* Get `maxResults` videos in a playlist. Used mostly internally with `Playlist#getVideos`.
* If <= 0 or not included, returns all videos in the playlist.
* @param playlistId The ID of the playlist.
* @param maxResults The maximum amount of videos to get from the playlist.
*/
public async getPlaylistItems (playlistId: string, maxResults: number = -1) {
let full
let videos: Video[] = []
if (maxResults <= 0) {
full = true
} else {
full = false
}
if (maxResults > 50) {
return Promise.reject('Max results must be 50 or below.')
}
let { data: results } = await youtube.playlistItems.list({

@@ -236,11 +302,9 @@ playlistId,

auth: this.token,
maxResults: 50
maxResults: full ? 50 : maxResults
})
if (results.items.length === 0) {
Promise.reject('Playlist not found.')
return Promise.reject('Playlist not found.')
}
let oldRes: youtube_v3.Schema$PlaylistItemListResponse = results
let videos: Video[] = []
const totalResults = results.pageInfo.totalResults

@@ -254,6 +318,8 @@ const perPage = 50

if (pages === 0) {
if (!full || pages === 0) {
return videos
}
let oldRes: youtube_v3.Schema$PlaylistItemListResponse = results
for (let i = 0; i < pages; i++) {

@@ -260,0 +326,0 @@ const { data: newResults } = await youtube.playlistItems.list({

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc