Skip to content

Instantly share code, notes, and snippets.

@frankhale
Last active July 7, 2016 18:09
Show Gist options
  • Save frankhale/385d23384b8e738ff6e18c85a9e0f97b to your computer and use it in GitHub Desktop.
Save frankhale/385d23384b8e738ff6e18c85a9e0f97b to your computer and use it in GitHub Desktop.
Proposed fix for issue #15 on YouTube Search
var querystring = require('querystring')
var xhr = require('xhr')
if (!xhr.open) xhr = require('request')
var allowedProperties = [
'fields',
'channelId',
'channelType',
'eventType',
'forContentOwner',
'forDeveloper',
'forMine',
'location',
'locationRadius',
'onBehalfOfContentOwner',
'order',
'pageToken',
'publishedAfter',
'publishedBefore',
'regionCode',
'relatedToVideoId',
'relevanceLanguage',
'safeSearch',
'topicId',
'type',
'videoCaption',
'videoCategoryId',
'videoDefinition',
'videoDimension',
'videoDuration',
'videoEmbeddable',
'videoLicense',
'videoSyndicated',
'videoType',
'key'
]
module.exports = function (term, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
var params = {
q: term,
part: opts.part || 'snippet',
maxResults: opts.maxResults || 30
}
Object.keys(opts).map(function (k) {
if (allowedProperties.indexOf(k) > -1) params[k] = opts[k]
})
xhr({
url: 'https://www.googleapis.com/youtube/v3/search?' + querystring.stringify(params),
method: 'GET'
}, function (err, res, body) {
if (err) return cb(err)
try {
var result = JSON.parse(body)
if (result.error) {
var error = new Error(result.error.errors.shift().message)
return cb(error)
}
var pageInfo = {
totalResults: result.pageInfo.totalResults,
resultsPerPage: result.pageInfo.resultsPerPage
}
var findings = result.items.map(function (item) {
var link = "",
id = ""
switch(item.id.kind) {
case 'youtube#channel':
link = 'https://www.youtube.com/channel/' + item.id.channelId
id = item.id.channelId
break
case 'youtube#playlist':
link = 'https://www.youtube.com/playlist&list=' + item.id.playlistId
id = item.id.playlistId
break
default:
link = 'https://www.youtube.com/watch?v=' + item.id.videoId
id = item.id.videoId
break
}
return {
id: id,
link: link,
kind: item.id.kind,
publishedAt: item.snippet.publishedAt,
channelId: item.snippet.channelId,
title: item.snippet.title,
description: item.snippet.description,
thumbnails: item.snippet.thumbnails
}
})
return cb(null, findings, pageInfo)
} catch(e) {
return cb(e)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment