Permalink
Browse files

More simplifications (no more API options).

  • Loading branch information...
1 parent 19546d4 commit 84084620e69852d33b429d7949109d8d494d4634 @chbrown committed Aug 10, 2012
Showing with 18 additions and 31 deletions.
  1. +11 −15 README.md
  2. +6 −15 lib/flickr.js
  3. +1 −1 package.json
View
@@ -1,12 +1,12 @@
# flickr-with-uploads
-124-line Node.js wrapper for the Flickr API, using oAuth authentication, supporting uploads.
+115-line Node.js wrapper for the Flickr API, using oAuth authentication, supporting uploads.
It currently supports only Flickr's "API Methods" (on the right side of the page [Flickr API Documentation](http://www.flickr.com/services/api/)), and [Uploading](http://www.flickr.com/services/api/upload.api.html).
A pull request is in the process, to ciaranj's `node-oauth`, for some required functionality to allow signing but not GET/POSTing with that oAuth library. For the time being, this package requires my `node-oauth` fork, which includes that functionality.
-The library is forked from [node-flickr](https://github.com/sujal/node-flickr), but I pretty much rewrote the whole flickr.js file. I fixed the formatting, simplified the signing or not-signing handling, and **most importantly** now support uploading. Even though I added a pretty big feature, I decreased the line count from 158 to 124.
+The library is forked from [node-flickr](https://github.com/sujal/node-flickr), but I pretty much rewrote the whole flickr.js file. I fixed the formatting, simplified the signing or not-signing handling, and **most importantly** now support uploading. Even though I added a pretty big feature, I decreased the line count from 158 to 115.
[node-flickr](https://github.com/sujal/node-flickr), in turn, is heavily inspired by [the flickrnode library by Ciaran Jessup](https://github.com/ciaranj/flickrnode).
## Initialization
@@ -52,9 +52,9 @@ And my .env file (all my values are fake, obviously--actual credentials are all
And then since all my calls are signed, I wrote a helper function, `api`:
````javascript
-function api(method_name, data, options, callback) {
+function api(method_name, data, callback) {
// overloaded as (method_name, data, callback)
- return client.createRequest(method_name, data, true, options, callback).send();
+ return client.createRequest(method_name, data, true, callback).send();
}
````
@@ -66,23 +66,19 @@ Using my `api` function from above:
var fullpath = '/Users/chbrown/Pictures/Seaworld - The Heist/orca_019.jpg';
var params = {
title: 'My new pet: baby orca', description: "Don't tell Seaworld!",
- is_public: 0, is_friend: 1, is_family: 1, hidden: 2
-};
-var options = {
- method: 'POST',
- file: fs.createReadStream(fullpath, {flags: 'r'})
+ is_public: 0, is_friend: 1, is_family: 1, hidden: 2,
+ photo: fs.createReadStream(fullpath, {flags: 'r'})
};
// the method_name gets the special value of "upload" for uploads.
-api('upload', params, options, function(err, response) {
+api('upload', params, function(err, response) {
if (err) {
console.error("Could not upload photo: ", self.toString() + ". Error message:");
console.error();
}
else {
- var photo_id = response.photoid;
// usually, the method name is precisely the name of the API method, as they are here:
- api('flickr.photosets.addPhoto', {photoset_id: 1272356126, photo_id: photo_id}, function(err, response) {
- api('flickr.photos.getInfo', {photo_id: photo_id}, function(err, response) {
+ api('flickr.photos.getInfo', {photo_id: response.photoid}, function(err, response) {
+ api('flickr.photosets.addPhoto', {photoset_id: 1272356126, photo_id: response.photo.id}, function(err) {
console.log("Full photo info:", response.photo);
});
});
@@ -96,11 +92,11 @@ Fixes are totally welcome! In the master branch, even! Just use sane formatting
## Dependencies
-Just one dependency: [form-data](https://github.com/felixge/node-form-data). This is just for the uploads. It works awesome, only takes about three lines to use. felixge is the author of (node-formidable)[https://github.com/felixge/node-formidable], the awesome form parsing library.
+Just one dependency: [form-data](https://github.com/felixge/node-form-data). This is just for the uploads. It works awesomely, only takes about three lines to use. felixge is the author of (node-formidable)[https://github.com/felixge/node-formidable], which is another great form parsing library.
## Related
-The node-flickr rewrite was all just to support my (Flickr Backup Script)[https://github.com/chbrown/flickr-backup]. There are lots more examples in that code, too.
+The node-flickr rewrite was all just to support my (Flickr Backup Script)[https://github.com/chbrown/flickr-backup], which is a script to automatically backup a directory of directories full of pictures as sets of photos to Flickr (since Pro accounts have unlimited storage). There are lots more examples in that code, too.
## License
View
@@ -14,22 +14,15 @@ function Flickr(consumer_key, consumer_secret, oauth_token, oauth_token_secret,
this.oauth_client = new OAuth('http://www.flickr.com/services/oauth/request_token',
'http://www.flickr.com/services/oauth/access_token', consumer_key, consumer_secret, '1.0A', null, 'HMAC-SHA1');
}
-Flickr.prototype.createRequest = function(method, params, signed_in, options, callback) {
- return new FlickrRequest(this, method, params, signed_in, options, callback);
+Flickr.prototype.createRequest = function(method, params, signed_in, callback) {
+ return new FlickrRequest(this, method, params, signed_in, callback);
};
-function FlickrRequest(client, method, params, signed_in, options, callback) {
- // overload as (method, params, signed_in, callback)
- if (callback === undefined) {
- callback = options;
- options = {};
- }
-
+function FlickrRequest(client, method, params, signed_in, callback) {
this.client = client;
this.method = method;
this.params = params;
this.signed_in = signed_in;
- this.options = options;
this.callback = callback;
}
FlickrRequest.prototype.queryString = function() {
@@ -40,6 +33,8 @@ FlickrRequest.prototype.queryString = function() {
if (this.signed_in) params.api_key = this.client.consumer_key;
if (this.method !== 'upload')
params.method = this.method;
+ else
+ delete params.photo;
return '?' + querystring.stringify(params);
};
FlickrRequest.prototype.send = function() {
@@ -58,7 +53,6 @@ FlickrRequest.prototype.sendGET = function() {
var api_url_parts = url.parse(api_url);
var payload = {host: api_url_parts.host, path: api_url_parts.path};
- // console.log("GET", payload);
var req = http.request(payload, function(res) { self.handleResponseStream(res); });
req.end();
};
@@ -73,10 +67,9 @@ FlickrRequest.prototype.sendPOST = function() {
for (var key in querystring_parts) {
form.append(key, querystring_parts[key]);
}
- form.append('photo', this.options.file);
+ form.append('photo', this.params.photo);
var payload = {host: api_url_parts.host, path: '/services/upload/?format=json', headers: form.getHeaders(), method: 'POST'};
- // console.log("POST", payload, querystring_parts);
var req = http.request(payload, function(res) { self.handleResponseStream(res); });
form.pipe(req);
};
@@ -107,8 +100,6 @@ FlickrRequest.prototype.processResponse = function(response_body) {
try {
var res = JSON.parse(response_body);
if (res.stat === 'ok') {
- if (this.options.result_mapper)
- res = this.options.result_mapper(res);
this.callback(null, res);
}
else {
View
@@ -1,7 +1,7 @@
{
"name": "flickr-with-uploads",
"description": "Simple Flickr API for Node.js using OAuth 1.0a, supporting uploads.",
- "version": "0.2.1",
+ "version": "0.2.2",
"authors": [
"Christopher Brown <audiere@gmail.com>",
"Sujal Shah <codesujal@gmail.com>"

0 comments on commit 8408462

Please sign in to comment.