ytsearcher
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -7,3 +7,2 @@ 'use strict'; | ||
'part', | ||
'key', | ||
], | ||
@@ -10,0 +9,0 @@ [ //filters: |
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
//////////////////////////////////// | ||
// Private keystore for searches // | ||
//////////////////////////////////// | ||
const _keystore = new WeakMap(); | ||
/////////////////////// | ||
@@ -41,3 +47,3 @@ // Utility constants // | ||
/** | ||
* constructor - Creates a search object with a given query | ||
* constructor - Creates a search object with a given query (but does not perform the actual search). | ||
* | ||
@@ -62,11 +68,19 @@ * @param {string} query - The Search query | ||
* @instance | ||
* @param {Object=} [options={}] - Optional search options | ||
* @param {Object=} [options={}] - Optional search options | ||
* @param {string=} [apikey] - The api key to use for this search. | ||
* If none is specified the last used apikey will be used in the search. | ||
* If this is the first time search() is invoked on this YTSearch apikey must be given. | ||
* @return {YTSearch} - The YTSearch object populated with search results. | ||
*/ | ||
search ( options ) { | ||
this.options = options||{}; | ||
search ( options, { key: apikey } ) { | ||
return new Promise( (res, rej) => { | ||
this.options = Object.assign( {}, options ); | ||
if(!apikey&&!_keystore.get(this)) rej('No token'); | ||
const theKey = apikey||_keystore.get(this); | ||
_keystore.set(this, theKey); | ||
const searchParams = { | ||
key: _keystore.get(this), | ||
maxResults: this.options.maxResults || 10, | ||
@@ -91,3 +105,3 @@ part: 'snippet', | ||
needle.get( queryUrl, (error, response) => { | ||
if(error) rej(err); | ||
if(error) rej(error); | ||
if(response.statusCode!=200) rej(`Error code: ${response.statusCode}`); | ||
@@ -126,3 +140,3 @@ const body = response.body; | ||
try{ | ||
res( await this.search(Object.assign( newOptions, {pageToken: this.nextPageToken})) ); | ||
res( await this.search(Object.assign(newOptions, {pageToken: this.nextPageToken})) ); | ||
}catch(err){ | ||
@@ -135,3 +149,3 @@ rej(err); | ||
/** | ||
* prevPage - Updates the search object, populating it with data from the next page | ||
* prevPage - Updates the search object, populating it with data from the previous page | ||
* | ||
@@ -138,0 +152,0 @@ * @memberof YTSearch |
@@ -76,3 +76,3 @@ 'use strict'; | ||
return new YTSearch( query ) | ||
.search( Object.assign({}, this.defaultoptions||{}, options||{}, { key: _key.get(this) }) ); | ||
.search( Object.assign({}, this.defaultoptions||{}, options||{}), { key: _key.get(this) } ); | ||
} | ||
@@ -79,0 +79,0 @@ }; |
{ | ||
"name": "ytsearcher", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "A nodejs package that provides an easy-to-use promise-based system of getting youtube search results", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,3 +16,4 @@ # YTSearcher | ||
By default the api key will be irretrievable. To enable access to `searcher.key` create the object like so: | ||
By default the api key will be irretrievable. | ||
To enable access to `searcher.key` create the object like so: | ||
@@ -26,2 +27,5 @@ const searcher2 = new YTSearcher( { | ||
This package interacts directly with google's api. The base url can be retrieved by doing | ||
```const { apiurl } = require('ytsearcher');``` | ||
// result will be a YTSearch object. | ||
@@ -35,19 +39,81 @@ let resultA = await searcher.search('A Search Query'); | ||
Or you can fetch the list via: | ||
```const { validOptions } = require('ytsearcher');``` which will return the array. | ||
**Examples** | ||
// For example, to grab only video results from a search query: | ||
let resultC = await searcher.search('A Third Query', { type: 'video' }); | ||
// This will log the first search result. | ||
// This shortcut will log the first search result (in the active page). | ||
console.log(result.first); | ||
// This will log the url of the first search result. | ||
// This will log the url of the first search result (in the active page). | ||
console.log(result.first.url); | ||
// A YTSearch has a built in page flipper, which will update the properties of YTSearch, including `first`. These will return null when the last and first page have been hit (respectively). | ||
### Pagination | ||
**A YTSearch has a built in page flipper, which will update the properties of YTSearch, including search.first.** | ||
// These will return null when the last and first page have been hit (respectively). | ||
await result.nextPage(); | ||
await result.prevPage(); | ||
// result.currentPage is an array of objects containing the current active page in the search object. | ||
const currentPage = result.currentPage | ||
// To print everything in the current page. | ||
console.log(currentPage); | ||
// You can also get individual elements from it like so: | ||
console.log(currentPage.first()); | ||
console.log(currentPage.last()); | ||
console.log(currentPage[1]); | ||
### Summary example to get the url of the second result on the second page of a video-only search (assuming both the page and the result exist): | ||
**For async methods:** | ||
const APIKEY = "12345"; // replace me | ||
const QUERY = "Anthing you want"; // replace me too | ||
const { YTSearcher } = require( 'ytsearcher' ); | ||
const ytsearcher = new YTSearcher( APIKEY ); | ||
const searchResult = await ytsearcher.search( QUERY, { type: 'video' } ); | ||
const secondPage = await searchResult.nextPage(); | ||
// secondPage is same object as searchResult | ||
const page = secondPage.currentPage; | ||
const videoEntry = page[1]; | ||
console.log( videoEntry.url ); | ||
**For completely non-async methods:** | ||
const APIKEY = "12345"; // replace me | ||
const QUERY = "Anything you want"; // replace me too | ||
const { YTSearcher } = require( 'ytsearcher' ); | ||
const ytsearcher = new YTSearcher( APIKEY ); | ||
ytsearcher.search( QUERY, { type: 'video' } ) | ||
.then( searchResult => { | ||
searchResult.nextPage() | ||
.then( secondPage => { | ||
// secondPage is same object as searchResult | ||
const page = secondPage.currentPage; | ||
const videoEntry = page[1]; | ||
console.log( videoEntry.url ); | ||
}); | ||
}); | ||
The Search Query can be anything, including a youtube link itself. | ||
Searches may error, and if an error code is available it will be in the error. A list of possible errors responses is available here: [https://developers.google.com/analytics/devguides/reporting/core/v3/errors](https://developers.google.com/analytics/devguides/reporting/core/v3/errors) | ||
Full docs are available here: [http://ytsearcher.willyz.cf](https://ytsearcher.willyz.cf) |
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
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
1084992
44
967
117