better-youtube-api
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -52,4 +52,13 @@ "use strict"; | ||
} | ||
getVideos() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.data.contentDetails) { | ||
const videos = yield this.youtube.getPlaylist(this.data.contentDetails.relatedPlaylists.uploads); | ||
this.videos = videos; | ||
return videos; | ||
} | ||
}); | ||
} | ||
} | ||
exports.Channel = Channel; | ||
//# sourceMappingURL=Channel.js.map |
@@ -17,5 +17,7 @@ import { youtube_v3 } from "googleapis" | ||
public getVideo(id: string): Promise<Video> | ||
public getVideoByUrl(url: string): Promise<Video> | ||
public searchChannels(searchTerm: string, maxResults: number): Promise<Channel[] | Channel> | ||
public getChannel(id: string): Promise<Channel> | ||
public getChannelByUrl(url: string): Promise<Channel> | ||
@@ -29,2 +31,3 @@ /** | ||
public getPlaylist(id: string): Promise<Playlist> | ||
public getPlaylistByUrl(url: string): Promise<Playlist> | ||
public searchPlaylists(searchTerm: string, maxResults: number): Promise<Playlist[] | Playlist> | ||
@@ -195,2 +198,7 @@ public getPlaylistItems(playlistId: string): Promise<Video[]> | ||
/** | ||
* The channel's uploads. Only available after called `Channel#getVideos()` | ||
*/ | ||
public videos: Playlist | ||
constructor (youtube: YouTube, data: youtube_v3.Schema$Channel | youtube_v3.Schema$SearchResult) | ||
@@ -203,2 +211,7 @@ | ||
public fetch (): Promise<this & Channel> | ||
/** | ||
* Fetches the channel's videos and assigns them to the `Channel#videos` property. | ||
*/ | ||
public getVideos (): Promise<Playlist> | ||
} | ||
@@ -205,0 +218,0 @@ |
@@ -16,2 +16,3 @@ "use strict"; | ||
const entities_1 = require("./entities"); | ||
const util_1 = require("./util"); | ||
const axios_1 = require("axios"); | ||
@@ -57,5 +58,25 @@ __export(require("./entities")); | ||
}); | ||
if (video.items.length === 0) { | ||
Promise.reject('Video not found.'); | ||
} | ||
return new entities_1.Video(this, video.items[0]); | ||
}); | ||
} | ||
getVideoByUrl(url) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const id = util_1.parseUrl(url); | ||
if (!id.video) { | ||
return Promise.reject('Not a valid video url.'); | ||
} | ||
const { data: video } = yield youtube.videos.list({ | ||
id: id.video, | ||
part: 'snippet,contentDetails', | ||
auth: this.token | ||
}); | ||
if (video.items.length === 0) { | ||
Promise.reject('Video not found.'); | ||
} | ||
return new entities_1.Video(this, video.items[0]); | ||
}); | ||
} | ||
searchChannels(searchTerm, maxResults = 10) { | ||
@@ -87,8 +108,28 @@ return __awaiter(this, void 0, void 0, function* () { | ||
id, | ||
part: 'snippet,statistics,status', | ||
part: 'snippet,statistics,status,contentDetails', | ||
auth: this.token | ||
}); | ||
if (channel.items.length === 0) { | ||
Promise.reject('Channel not found.'); | ||
} | ||
return new entities_1.Channel(this, channel.items[0]); | ||
}); | ||
} | ||
getChannelByUrl(url) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const id = util_1.parseUrl(url); | ||
if (!id.channel) { | ||
return Promise.reject('Not a valid channel url.'); | ||
} | ||
const { data: channel } = yield youtube.channels.list({ | ||
id: id.channel, | ||
part: 'snippet,statistics,status,contentDetails', | ||
auth: this.token | ||
}); | ||
if (channel.items.length === 0) { | ||
Promise.reject('Channel not found.'); | ||
} | ||
return new entities_1.Channel(this, channel.items[0]); | ||
}); | ||
} | ||
/** | ||
@@ -102,2 +143,5 @@ * Gets the 50 latest videos from a channel. | ||
const { data: results } = yield axios_1.default.get(`https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${id}&order=date&key=${this.token}&maxResults=50`); | ||
if (results.items.length === 0) { | ||
Promise.reject('Channel not found.'); | ||
} | ||
for (let i = 0; i < results.items.length; i++) { | ||
@@ -116,5 +160,25 @@ videos.push(new entities_1.Video(this, results.items[i])); | ||
}); | ||
if (playlist.items.length === 0) { | ||
Promise.reject('Playlist not found.'); | ||
} | ||
return new entities_1.Playlist(this, playlist.items[0]); | ||
}); | ||
} | ||
getPlaylistByUrl(url) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const id = util_1.parseUrl(url); | ||
if (!id.playlist) { | ||
return Promise.reject('Not a valid playlist url.'); | ||
} | ||
const { data: playlist } = yield youtube.playlists.list({ | ||
id: id.playlist, | ||
part: 'snippet,contentDetails,player', | ||
auth: this.token | ||
}); | ||
if (playlist.items.length === 0) { | ||
Promise.reject('Playlist not found.'); | ||
} | ||
return new entities_1.Playlist(this, playlist.items[0]); | ||
}); | ||
} | ||
searchPlaylists(searchTerm, maxResults = 10) { | ||
@@ -147,2 +211,5 @@ return __awaiter(this, void 0, void 0, function* () { | ||
}); | ||
if (results.items.length === 0) { | ||
Promise.reject('Playlist not found.'); | ||
} | ||
let oldRes; | ||
@@ -153,5 +220,5 @@ let videos = []; | ||
}); | ||
while (true) { | ||
if (!results.nextPageToken) { | ||
break; | ||
const interval = setInterval(() => __awaiter(this, void 0, void 0, function* () { | ||
if (!results.nextPageToken || !oldRes.nextPageToken) { | ||
clearInterval(interval); | ||
} | ||
@@ -181,6 +248,3 @@ let newResults; | ||
}); | ||
if (!oldRes.nextPageToken) { | ||
break; | ||
} | ||
} | ||
}), 100); | ||
return videos; | ||
@@ -187,0 +251,0 @@ }); |
{ | ||
"name": "better-youtube-api", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Better than simple ones.", | ||
@@ -5,0 +5,0 @@ "main": "out/index.js", |
import { YouTube } from '..' | ||
import { youtube_v3 } from 'googleapis' | ||
import { Playlist } from '.' | ||
@@ -78,2 +79,4 @@ /** | ||
public videos: Playlist | ||
constructor (youtube: YouTube, data: youtube_v3.Schema$Channel | youtube_v3.Schema$SearchResult) { | ||
@@ -118,2 +121,11 @@ this.youtube = youtube | ||
} | ||
public async getVideos () { | ||
if ((this.data as youtube_v3.Schema$Channel).contentDetails) { | ||
const videos = await this.youtube.getPlaylist((this.data as youtube_v3.Schema$Channel).contentDetails.relatedPlaylists.uploads) | ||
this.videos = videos | ||
return videos | ||
} | ||
} | ||
} |
119
src/index.ts
import { google, youtube_v3 } from 'googleapis' | ||
import { Video, Channel, Playlist } from './entities' | ||
import { parseUrl } from './util' | ||
import axios from 'axios' | ||
@@ -51,5 +52,29 @@ export * from './entities' | ||
if (video.items.length === 0) { | ||
Promise.reject('Video not found.') | ||
} | ||
return new Video(this, video.items[0]) | ||
} | ||
public async getVideoByUrl (url: string) { | ||
const id = parseUrl(url) | ||
if (!id.video) { | ||
return Promise.reject('Not a valid video url.') | ||
} | ||
const { data: video } = await youtube.videos.list({ | ||
id: id.video, | ||
part: 'snippet,contentDetails', | ||
auth: this.token | ||
}) | ||
if (video.items.length === 0) { | ||
Promise.reject('Video not found.') | ||
} | ||
return new Video(this, video.items[0]) | ||
} | ||
public async searchChannels (searchTerm: string, maxResults: number = 10) { | ||
@@ -84,9 +109,33 @@ if (maxResults < 1) { | ||
id, | ||
part: 'snippet,statistics,status', | ||
part: 'snippet,statistics,status,contentDetails', | ||
auth: this.token | ||
}) | ||
if (channel.items.length === 0) { | ||
Promise.reject('Channel not found.') | ||
} | ||
return new Channel(this, channel.items[0]) | ||
} | ||
public async getChannelByUrl (url: string) { | ||
const id = parseUrl(url) | ||
if (!id.channel) { | ||
return Promise.reject('Not a valid channel url.') | ||
} | ||
const { data: channel } = await youtube.channels.list({ | ||
id: id.channel, | ||
part: 'snippet,statistics,status,contentDetails', | ||
auth: this.token | ||
}) | ||
if (channel.items.length === 0) { | ||
Promise.reject('Channel not found.') | ||
} | ||
return new Channel(this, channel.items[0]) | ||
} | ||
/** | ||
@@ -100,2 +149,6 @@ * Gets the 50 latest videos from a channel. | ||
if (results.items.length === 0) { | ||
Promise.reject('Channel not found.') | ||
} | ||
for (let i = 0; i < results.items.length; i++) { | ||
@@ -115,5 +168,29 @@ videos.push(new Video(this, results.items[i])) | ||
if (playlist.items.length === 0) { | ||
Promise.reject('Playlist not found.') | ||
} | ||
return new Playlist(this, playlist.items[0]) | ||
} | ||
public async getPlaylistByUrl (url: string) { | ||
const id = parseUrl(url) | ||
if (!id.playlist) { | ||
return Promise.reject('Not a valid playlist url.') | ||
} | ||
const { data: playlist } = await youtube.playlists.list({ | ||
id: id.playlist, | ||
part: 'snippet,contentDetails,player', | ||
auth: this.token | ||
}) | ||
if (playlist.items.length === 0) { | ||
Promise.reject('Playlist not found.') | ||
} | ||
return new Playlist(this, playlist.items[0]) | ||
} | ||
public async searchPlaylists (searchTerm: string, maxResults: number = 10) { | ||
@@ -149,2 +226,6 @@ const { data: results } = await youtube.search.list({ | ||
if (results.items.length === 0) { | ||
Promise.reject('Playlist not found.') | ||
} | ||
let oldRes: youtube_v3.Schema$PlaylistItemListResponse | ||
@@ -157,5 +238,5 @@ let videos: Video[] = [] | ||
while (true) { | ||
if (!results.nextPageToken) { | ||
break | ||
const interval = setInterval(async () => { | ||
if (!results.nextPageToken || !oldRes.nextPageToken) { | ||
clearInterval(interval) | ||
} | ||
@@ -167,16 +248,16 @@ | ||
newResults = (await youtube.playlistItems.list({ | ||
playlistId, | ||
part: 'snippet', | ||
auth: this.token, | ||
maxResults: 50, | ||
pageToken: results.nextPageToken | ||
})).data | ||
playlistId, | ||
part: 'snippet', | ||
auth: this.token, | ||
maxResults: 50, | ||
pageToken: results.nextPageToken | ||
})).data | ||
} else { | ||
newResults = (await youtube.playlistItems.list({ | ||
playlistId, | ||
part: 'snippet', | ||
auth: this.token, | ||
maxResults: 50, | ||
pageToken: oldRes.nextPageToken | ||
})).data | ||
playlistId, | ||
part: 'snippet', | ||
auth: this.token, | ||
maxResults: 50, | ||
pageToken: oldRes.nextPageToken | ||
})).data | ||
} | ||
@@ -188,10 +269,6 @@ | ||
}) | ||
}, 100) | ||
if (!oldRes.nextPageToken) { | ||
break | ||
} | ||
} | ||
return videos | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59910
26
1301