Created
July 9, 2014 19:52
-
-
Save brunoziie/4dcd584eb19eb3455420 to your computer and use it in GitHub Desktop.
youtube-search.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define('app.youtube.search', ['app.youtube'], { | |
init: function () {}, | |
/** | |
* Faz a requisição de uma busca | |
* @param {Mixed} query String com o termo a ser pesquisado ou um Objeto com os parametros (vide Youtube API Docs) | |
* @param {Function} callback Função de callback | |
* @param {Boolean} callback.error Verdadeiro quando a requisição retorna um erro | |
* @param {Function} callback.response Dados que retornaram na resposta da requisição | |
* @return {void} | |
*/ | |
request: function (query, callback) { | |
var that = this, | |
configs = this.__import('app.configs'), | |
params = (typeof query == 'string') ? {q: query}: query; | |
params.key = configs.get('youtube.PUBLIC_KEY'); | |
params.maxResults = configs.get('youtube.MAX_RESULTS'); | |
params.part = 'snippet'; | |
params.type = 'video'; | |
params.fields = 'items(id,snippet),pageInfo,prevPageToken,nextPageToken'; | |
$.get('https://content.googleapis.com/youtube/v3/search', params, function (data) { | |
var response = {data: data}, | |
error = (typeof data.error != 'undefined') ? true : false; | |
if (!error & typeof data.nextPageToken != 'undefined') { | |
response.getNext = function (callback) { | |
params.pageToken = data.nextPageToken; | |
that.request(params, callback); | |
} | |
} | |
if (!error && typeof data.prevPageToken != 'undefined') { | |
response.getPrev = function (callback) { | |
params.pageToken = data.prevPageToken; | |
that.request(params, callback); | |
} | |
} | |
callback(error, response); | |
}, 'json'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment