youtube-feeds
Advanced tools
Comparing version 2.2.1 to 2.3.0
@@ -8,4 +8,4 @@ { | ||
"name": "youtube-feeds", | ||
"description": "Access public YouTube API feeds, mostly with the clean JSON-C results", | ||
"version": "2.2.1", | ||
"description": "Access public YouTube Data API v2 feeds, mostly with the clean JSON-C results", | ||
"version": "2.3.0", | ||
"repository": { | ||
@@ -27,3 +27,3 @@ "type": "git", | ||
}, | ||
"keywords": ["youtube", "video", "feeds", "api"], | ||
"keywords": ["youtube", "youtubev2", "video", "feeds", "api"], | ||
"license": { | ||
@@ -30,0 +30,0 @@ "type": "Public Domain", |
157
README.md
@@ -8,11 +8,21 @@ nodejs-youtube | ||
## Installation | ||
End of life notice | ||
------------------ | ||
[![Build Status](https://secure.travis-ci.org/fvdm/nodejs-youtube.png?branch=master)](http://travis-ci.org/fvdm/nodejs-youtube) | ||
This module relies heavily on YouTube Data API v2 which will be deprecated on **April 20th 2015** | ||
to be replaced by version 3 of their API. Maintenance will continue on this module until it | ||
stops functioning on the above mentioned date. | ||
It is recommended for all users to switch to a YouTube module with API v3 support. | ||
### From NPM registry | ||
Suggestion: [youtube-api](https://www.npmjs.org/package/youtube-api) on npm ([Github](https://github.com/IonicaBizau/youtube-api)) | ||
The NPM release is always the recent *stable* version. | ||
Installation | ||
------------ | ||
### From npm registry | ||
The npm release is always the recent *stable* version. | ||
``` | ||
@@ -41,3 +51,4 @@ npm install youtube-feeds | ||
## Usage | ||
Usage | ||
----- | ||
@@ -53,3 +64,4 @@ ```js | ||
## Configuration | ||
Configuration | ||
------------- | ||
@@ -64,3 +76,9 @@ * `httpProtocol` (string) | ||
* `developerKey` (string) | ||
Set your YouTube developer key ([manage](http://code.google.com/apis/youtube/dashboard/)). | ||
This is required for some methods, ie. [user.activity](#useractivity). | ||
You can also temporarily override the global setting with the `key` property in a method's vars. | ||
### Example: | ||
@@ -75,3 +93,4 @@ | ||
## Callbacks | ||
Callbacks | ||
--------- | ||
@@ -92,29 +111,32 @@ Each method takes a `callback` function as last parameter. When everything seems alright `err` is null, otherwise `err` will be `instanceof Error` for tracing. | ||
``` | ||
err.message : the error message | ||
err.stack : stack trace | ||
err.origin : what it relates to (api, method, request) | ||
err.details : api response or other information when available, or `null` | ||
``` | ||
err.message : the error message | ||
err.stack : stack trace | ||
err.origin : what it relates to (api, method, request) | ||
err.details : api response or other information when available, or `null` | ||
Messages: | ||
``` | ||
Error: invalid response api API response can't be parsed | ||
Error: not json api Expected JSON, received something else | ||
Error: not found method Requested data was not found | ||
Error: not allowed method No permission to requested data | ||
Error: invalid id method Requested video ID is invalid | ||
Error: connection closed api Connection dropped early | ||
Error: connection error request Can't connect to API | ||
Error: request timeout request The request took too long to connect or process | ||
Error: error api API returned an error, see err.details | ||
``` | ||
Error: invalid response api API response can't be parsed | ||
Error: not json api Expected JSON, received something else | ||
Error: not found method Requested data was not found | ||
Error: not allowed method No permission to requested data | ||
Error: invalid id method Requested video ID is invalid | ||
Error: connection closed api Connection dropped early | ||
Error: connection error request Can't connect to API | ||
Error: request timeout request The request took too long to connect or process | ||
Error: error api API returned an error, see err.details | ||
Error: developer key missing api developerKey is not set, see Configuration. | ||
=========================================================================================== | ||
Feeds | ||
===== | ||
----- | ||
Retrieve lists, search videos, related material. | ||
## feeds.videos | ||
feeds.videos | ||
------------ | ||
### ( [vars], callback ) | ||
@@ -186,3 +208,4 @@ | ||
## feeds.related | ||
feeds.related | ||
------------- | ||
### ( videoid, [vars], callback ) | ||
@@ -193,3 +216,4 @@ | ||
## feeds.responses | ||
feeds.responses | ||
--------------- | ||
### ( videoid, [vars], callback ) | ||
@@ -200,3 +224,4 @@ | ||
## feeds.comments | ||
feeds.comments | ||
-------------- | ||
### ( videoid, [vars], callback ) | ||
@@ -207,3 +232,4 @@ | ||
## feeds.standard | ||
feeds.standard | ||
-------------- | ||
### ( feed, [vars], callback ) | ||
@@ -230,3 +256,4 @@ | ||
## feeds.playlist | ||
feeds.playlist | ||
-------------- | ||
### ( playlistid, [vars], callback ) | ||
@@ -237,4 +264,7 @@ | ||
=========================================================================================== | ||
Video | ||
===== | ||
----- | ||
@@ -245,3 +275,4 @@ | ||
## video | ||
video | ||
----- | ||
### ( videoid, [callback] ) | ||
@@ -256,3 +287,4 @@ | ||
## video.details | ||
video.details | ||
------------- | ||
### ( callback ) | ||
@@ -267,3 +299,4 @@ | ||
## video.related | ||
video.related | ||
------------- | ||
### ( [vars], callback ) | ||
@@ -278,3 +311,4 @@ | ||
## video.responses | ||
video.responses | ||
--------------- | ||
### ( [vars], callback ) | ||
@@ -289,3 +323,4 @@ | ||
## videos.comments | ||
videos.comments | ||
--------------- | ||
### ( [vars], callback ) | ||
@@ -300,4 +335,7 @@ | ||
=========================================================================================== | ||
User | ||
==== | ||
---- | ||
@@ -307,3 +345,4 @@ Get (public) feed data for one specific user. | ||
## user | ||
user | ||
---- | ||
### ( userid, [callback] ) | ||
@@ -318,3 +357,4 @@ | ||
## user.profile | ||
user.profile | ||
------------ | ||
### ( callback ) | ||
@@ -329,3 +369,4 @@ | ||
## user.favorites | ||
user.favorites | ||
-------------- | ||
### ( [vars], callback ) | ||
@@ -339,3 +380,4 @@ | ||
## user.playlists | ||
user.playlists | ||
-------------- | ||
### ( [vars], callback ) | ||
@@ -346,3 +388,4 @@ | ||
## user.uploads | ||
user.uploads | ||
------------ | ||
### ( [vars], callback ) | ||
@@ -356,8 +399,7 @@ | ||
=========================================================================================== | ||
Communication | ||
============= | ||
## talk | ||
talk | ||
---- | ||
### ( path, [fields], callback, [oldJsonKey] ) | ||
@@ -367,14 +409,15 @@ | ||
``` | ||
Param Type Description | ||
---------- -------- ---------------------------------------------------- | ||
path string full method path without leading slash | ||
fields object GET parameters | ||
callback function callback function to receive results | ||
oldJsonKey boolean force old XML-to-JSON format instead of clean JSON-C | ||
its value is the key containing the expected results | ||
``` | ||
Param Type Description | ||
---------- -------- ---------------------------------------------------- | ||
path string full method path without leading slash | ||
fields object GET parameters | ||
callback function callback function to receive results | ||
oldJsonKey boolean force old XML-to-JSON format instead of clean JSON-C | ||
its value is the key containing the expected results | ||
Unlicense / Public Domain | ||
========================= | ||
------------------------- | ||
@@ -381,0 +424,0 @@ |
@@ -7,35 +7,12 @@ /* | ||
License: Unlicense / Public Domain | ||
This is free and unencumbered software released into the public domain. | ||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
For more information, please refer to <http://unlicense.org> | ||
(see UNLICENSE file) | ||
*/ | ||
var xml2json = require('node-xml2json'), | ||
querystring = require('querystring') | ||
var xml2json = require('node-xml2json') | ||
var querystring = require('querystring') | ||
var app = { | ||
httpProtocol: 'http', // http, https | ||
timeout: 30000 // max execution time in milliseconds | ||
timeout: 5000, // max execution time in milliseconds | ||
developerKey: null // YouTube developer key | ||
} | ||
@@ -92,3 +69,3 @@ | ||
if( typeof cb == 'function' ) { | ||
if( typeof cb === 'function' ) { | ||
app.talk( 'feeds/api/videos/'+ videoid, cb ) | ||
@@ -128,3 +105,3 @@ } | ||
if( cb && typeof cb == 'function' ) { | ||
if( cb && typeof cb === 'function' ) { | ||
app.user( userid ).profile( cb ) | ||
@@ -152,5 +129,9 @@ } | ||
uploads: function( vars, cb ) { | ||
app.talk( 'feeds/api/users/'+ userid +'/uploads', vars, cb) | ||
app.talk( 'feeds/api/users/'+ userid +'/uploads', vars, cb ) | ||
}, | ||
// New subscription videos | ||
newsubscriptionvideos: function( vars, cb ) { | ||
app.talk( 'feeds/api/users/'+ userid +'/newsubscriptionvideos', vars, cb ) | ||
} | ||
} | ||
@@ -170,3 +151,3 @@ | ||
// fix callback | ||
if( !cb && typeof fields == 'function' ) { | ||
if( !cb && typeof fields === 'function' ) { | ||
var cb = fields | ||
@@ -177,3 +158,3 @@ var fields = {} | ||
// fix fields | ||
if( !fields || typeof fields != 'object' ) { | ||
if( !fields || typeof fields !== 'object' ) { | ||
var fields = {} | ||
@@ -188,11 +169,19 @@ } | ||
var options = { | ||
hostname: 'gdata.youtube.com', | ||
path: '/'+ path +'?'+ querystring.stringify( fields ), | ||
hostname: 'gdata.youtube.com', | ||
path: '/'+ path +'?'+ querystring.stringify( fields ), | ||
headers: { | ||
'User-Agent': 'youtube-feeds.js (https://github.com/fvdm/nodejs-youtube)', | ||
'Accept': 'application/json' | ||
'User-Agent': 'youtube-feeds.js (https://github.com/fvdm/nodejs-youtube)', | ||
'Accept': 'application/json', | ||
'GData-Version': '2' | ||
}, | ||
method: 'GET' | ||
method: 'GET' | ||
} | ||
// use X-GData-Key instead of adding it to the url, as per http://goo.gl/HEiCj | ||
// basically more secure in headers than in query string | ||
if ( fields.key || app.developerKey ) { | ||
options.headers['X-GData-Key'] = 'key=' + fields.key || app.developerKey | ||
delete fields.key | ||
} | ||
if( app.httpProtocol === 'https' ) { | ||
@@ -226,5 +215,5 @@ var request = require('https').request( options ) | ||
for( var d in data ) { | ||
data[d].copy( buf, pos ) | ||
pos += data[d].length | ||
for( var i = 0; i < data.length; ++i ) { | ||
data[i].copy( buf, pos ) | ||
pos += data[i].length | ||
} | ||
@@ -259,3 +248,3 @@ | ||
} else if( data.match( /^<errors .+<\/errors>$/ ) ) { | ||
} else if( data.match( /^<errors .+<\/errors>$/ ) || data.match( /^<\?xml version.+<\/errors>$/ ) ) { | ||
@@ -278,2 +267,15 @@ // xml error response | ||
} else if( ~data.indexOf('<H2>Error ') ) { | ||
// html error response | ||
complete = true | ||
var error = new Error('error') | ||
data.replace( /<H1>([^<]+)<\/H1>\n<H2>Error (\d+)<\/H2>/, function( s, reason, code ) { | ||
error.origin = 'api' | ||
error.details = { | ||
internalReason: reason, | ||
code: code | ||
} | ||
}) | ||
} else { | ||
@@ -289,3 +291,3 @@ | ||
// parse error | ||
if( error && error.origin == 'api' && error.message == 'error' ) { | ||
if( error && error.origin === 'api' && error.message === 'error' ) { | ||
var errorDetails = error.details | ||
@@ -295,3 +297,3 @@ if( | ||
&& error.details[0].code !== undefined | ||
&& error.details[0].code == 'ResourceNotFoundException' | ||
&& error.details[0].code === 'ResourceNotFoundException' | ||
) { | ||
@@ -307,3 +309,3 @@ complete = true | ||
error.details = errorDetails | ||
} else if( error.details.message == 'Invalid id' ) { | ||
} else if( error.details.message === 'Invalid id' ) { | ||
complete = true | ||
@@ -313,2 +315,7 @@ error = new Error('invalid id') | ||
error.details = errorDetails | ||
} else if( error.details[0] && error.details[0].internalReason === 'Developer key required for this operation' ) { | ||
complete = true | ||
error = new Error('developer key missing') | ||
error.origin = 'api' | ||
error.details = errorDetails | ||
} | ||
@@ -315,0 +322,0 @@ } |
23376
296
424