youtube-data
Version:
Get YouTube data as JSON
103 lines (77 loc) • 1.79 kB
text/coffeescript
qs = require 'querystring'
_ = require('underscore')._
request = require 'request'
fmt = require './formatting'
class Query
return new Query
constructor: (opts={}) ->
results: (r) ->
maxResults = r
maxResults = 50 if @maxResults > 50
orderByViewCount: () ->
orderByPublished: () ->
orderByRelevance: () ->
orderBy: (ordering) ->
page: (page) ->
pages: (start, end) ->
all: (all=true) ->
startAt: (ind) ->
author: (author) ->
type: (type) ->
videos: (author) ->
simple: (simple=true) ->
generateQs: ->
defaultOpts =
alt: 'json'
v: 2
_.extend defaultOpts, @qs
return defaultOpts
run: (cb) ->
qs_ = qs.stringify @generateQs()
{ type, simple } = @opts
unless type
return cb "Query type not selected. eg. query.videos('author')"
url = "http://gdata.youtube.com/feeds/api/#{ type }?#{ qs_ }"
request url, (err, res, body) ->
return cb err if err
try
json = JSON.parse body
catch e
return cb(e)
if simple and type is 'videos'
json.feed.entry = _.map json.feed.entry, fmt.video.entry.simple
return cb null, json
module.exports = Query