better-youtube-api
Advanced tools
Comparing version 0.3.7 to 0.4.0
@@ -16,2 +16,3 @@ "use strict"; | ||
const util_1 = require("./util"); | ||
const caching_1 = require("./util/caching"); | ||
__export(require("./entities")); | ||
@@ -25,6 +26,19 @@ /** | ||
* @param token Your YouTube Data API v3 token. Don't share this with anybody. | ||
* @param options Caching options. Recommended to change. | ||
*/ | ||
constructor(token) { | ||
constructor(token, options = { cache: true, cacheTTL: 600, cacheCheckInterval: 600, cacheSearches: false }) { | ||
this.token = token; | ||
this._shouldCache = options.cache; | ||
this._cacheSearches = options.cacheSearches; | ||
this._cacheTTL = options.cacheTTL; | ||
if (options.cacheCheckInterval > 0) { | ||
setInterval(caching_1.Cache.checkTTLs, options.cacheCheckInterval * 1000); | ||
} | ||
} | ||
_cache(id, value) { | ||
if (!this._shouldCache) { | ||
return; | ||
} | ||
caching_1.Cache.set(id, value, this._cacheTTL > 0 ? this._cacheTTL * 1000 + new Date().getTime() : 0); | ||
} | ||
/** | ||
@@ -142,2 +156,6 @@ * Search videos on YouTube. | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cached = caching_1.Cache.get(`search://${type}/"${searchTerm}"/${maxResults}`); | ||
if (cached) { | ||
return cached; | ||
} | ||
if (maxResults < 1 || maxResults > 50) { | ||
@@ -169,2 +187,5 @@ return Promise.reject('Max results must be greater than 0 and less than or equal to 50'); | ||
}); | ||
if (this._shouldCache && this._cacheSearches) { | ||
this._cache(`search://${type}/"${searchTerm}"/${maxResults}`, items); | ||
} | ||
return items; | ||
@@ -175,2 +196,6 @@ }); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cached = caching_1.Cache.get(`get://${type}/${id}`); | ||
if (cached) { | ||
return cached; | ||
} | ||
let result; | ||
@@ -208,14 +233,23 @@ if (type === 'video') { | ||
} | ||
let endResult; | ||
switch (type) { | ||
case 'video': | ||
return new entities_1.Video(this, result.items[0]); | ||
endResult = new entities_1.Video(this, result.items[0]); | ||
break; | ||
case 'playlist': | ||
return new entities_1.Playlist(this, result.items[0]); | ||
endResult = new entities_1.Playlist(this, result.items[0]); | ||
break; | ||
case 'channel': | ||
return new entities_1.Channel(this, result.items[0]); | ||
endResult = new entities_1.Channel(this, result.items[0]); | ||
break; | ||
case 'comment': | ||
return new entities_1.YTComment(this, result.items[0]); | ||
endResult = new entities_1.YTComment(this, result.items[0]); | ||
break; | ||
default: | ||
return Promise.reject('Type must be a video, channel, or playlist'); | ||
} | ||
if (this._shouldCache) { | ||
this._cache(`get://${type}/${id}`, endResult); | ||
} | ||
return endResult; | ||
}); | ||
@@ -225,2 +259,6 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const cached = caching_1.Cache.get(`get://${type}/${id}/${maxResults}`); | ||
if (cached) { | ||
return cached; | ||
} | ||
let full; | ||
@@ -325,2 +363,5 @@ let items = []; | ||
} | ||
if (this._shouldCache) { | ||
this._cache(`get://${type}/${id}/${maxResults}`, items); | ||
} | ||
return items; | ||
@@ -327,0 +368,0 @@ }); |
@@ -11,2 +11,5 @@ "use strict"; | ||
}; | ||
/** | ||
* @ignore | ||
*/ | ||
function get(url) { | ||
@@ -21,2 +24,5 @@ const options = parseUrlToOptions(url, 'GET'); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function post(url, data) { | ||
@@ -26,2 +32,5 @@ const options = parseUrlToOptions(url, 'POST'); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function put(url, data) { | ||
@@ -31,2 +40,5 @@ const options = parseUrlToOptions(url, 'PUT'); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function parseUrlToOptions(url, type) { | ||
@@ -44,2 +56,5 @@ const parsed = url_1.parse(url); | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function req(options, reqFunction) { | ||
@@ -67,2 +82,5 @@ return new Promise((resolve, reject) => { | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function reqCallback(req, data) { | ||
@@ -76,2 +94,5 @@ req.on('error', error => { | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function parseParams(params) { | ||
@@ -78,0 +99,0 @@ let url = ''; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const url_1 = require("url"); | ||
const numbers = '\\d+(?:[\\.,]\\d{0,3})?'; | ||
const weekPattern = `(${numbers}W)`; | ||
const datePattern = `(${numbers}Y)?(${numbers}M)?(${numbers}D)?`; | ||
const timePattern = `T(${numbers}H)?(${numbers}M)?(${numbers}S)?`; | ||
const iso8601 = `P(?:${weekPattern}|${datePattern}(?:${timePattern})?)`; | ||
const timeArray = ['weeks', 'years', 'months', 'days', 'hours', 'minutes', 'seconds']; | ||
const pattern = new RegExp(iso8601); | ||
function parseUrl(url) { | ||
@@ -51,2 +44,9 @@ const parsed = url_1.parse(url, true); | ||
function parseIsoDuration(duration) { | ||
const numbers = '\\d+(?:[\\.,]\\d{0,3})?'; | ||
const weekPattern = `(${numbers}W)`; | ||
const datePattern = `(${numbers}Y)?(${numbers}M)?(${numbers}D)?`; | ||
const timePattern = `T(${numbers}H)?(${numbers}M)?(${numbers}S)?`; | ||
const iso8601 = `P(?:${weekPattern}|${datePattern}(?:${timePattern})?)`; | ||
const timeArray = ['weeks', 'years', 'months', 'days', 'hours', 'minutes', 'seconds']; | ||
const pattern = new RegExp(iso8601); | ||
return duration.match(pattern).slice(1).reduce((prev, current, index) => { | ||
@@ -53,0 +53,0 @@ prev[timeArray[index]] = parseFloat(current) || 0; |
{ | ||
"name": "better-youtube-api", | ||
"version": "0.3.7", | ||
"version": "0.4.0", | ||
"description": "A very easy to use promise-based Youtube Data v3 API.", | ||
@@ -5,0 +5,0 @@ "main": "out/index.js", |
@@ -8,2 +8,5 @@ import { Video, Channel, Playlist, YTComment } from './entities'; | ||
export declare class YouTube { | ||
private _shouldCache; | ||
private _cacheSearches; | ||
private _cacheTTL; | ||
token: string; | ||
@@ -13,4 +16,6 @@ /** | ||
* @param token Your YouTube Data API v3 token. Don't share this with anybody. | ||
* @param options Caching options. Recommended to change. | ||
*/ | ||
constructor(token: string); | ||
constructor(token: string, options?: YouTubeOptions); | ||
_cache(id: string, value: any): void; | ||
/** | ||
@@ -92,3 +97,21 @@ * Search videos on YouTube. | ||
} | ||
declare type YouTubeOptions = { | ||
/** | ||
* Whether or not to cache entities. | ||
*/ | ||
cache?: boolean; | ||
/** | ||
* How long to cache entities in seconds (0 = unlimited). | ||
*/ | ||
cacheTTL?: number; | ||
/** | ||
* How often to check for and delete expired cached items in seconds. | ||
*/ | ||
cacheCheckInterval?: number; | ||
/** | ||
* Whether or not we should cache searches | ||
*/ | ||
cacheSearches?: boolean; | ||
}; | ||
export default YouTube; | ||
//# sourceMappingURL=index.d.ts.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
41
1358
107110