Skip to content
Browse files

Post photos to Tumblr

The Tumblr API is crazy and this is the only library I can find that manages to
upload a photo. It does it with a bunch of crazy regex substitutions during
OAuth, which are frightening but appear to work.

Something really weird is still happening, and we can't send captions along with
photos, so for now I just ignore any body included with the upload. I also don't
know what effect trying to include Tumblr-specific parameters might have.
  • Loading branch information...
1 parent bd4a918 commit 555f329fc371955c4e2dac013ce7a8a5a7640b03 @kristjan kristjan committed Oct 3, 2012
Showing with 34 additions and 19 deletions.
  1. +33 −19 lib/services/tumblr/posting.js
  2. +1 −0 package.json
View
52 lib/services/tumblr/posting.js
@@ -1,35 +1,49 @@
-var fs = require('fs');
-var path = require('path');
-var request = require('request');
-
-var TUMBLR_API_BASE = 'https://api.tumblr.com/v2';
+var fs = require('fs');
+var tumblrwks = require('tumblrwks');
function post(data, params, callback) {
var blogName = data.profile;
// Tumblogs can't include a dot, so if it does, assume a custom domain
if (blogName.indexOf('.') === -1) blogName = blogName + '.tumblr.com';
- var url = TUMBLR_API_BASE + '/blog/' + blogName + '/post';
-
- params.oauth = {
- consumer_key: data.auth.consumerKey,
- consumer_secret: data.auth.consumerSecret,
- token: data.auth.token.oauth_token,
- token_secret: data.auth.token.oauth_token_secret
- };
- request.post(url, params, function(err, response, body) {
+
+ var tumblr = new tumblrwks({
+ consumerKey: data.auth.consumerKey,
+ consumerSecret: data.auth.consumerSecret,
+ accessToken: data.auth.token.oauth_token,
+ accessSecret: data.auth.token.oauth_token_secret
+ }, blogName);
+
+ tumblr.post('/post', params, function(err, statusCode, body) {
+ if (err) return callback(null, {error: err});
return callback(null, body);
});
}
function postStatus(data, callback) {
- var params = {
+ post(data, {
type: 'text',
+ title: data.title,
body: data.body
- };
- if (data.title) params.title = data.title;
- post(data, { form: params }, callback);
+ }, callback);
+}
+
+function postLink(data, callback) {
+}
+
+function postPhoto(data, callback) {
+ fs.readFile(data.photo.path, function(err, photo) {
+ if (err) return callback(null, {
+ error: 'There was a problem uploading your photo.'}
+ );
+
+ post(data, {
+ type: 'photo',
+ data: photo
+ }, callback);
+ });
}
module.exports = {
- statuses: postStatus
+ statuses: postStatus,
+ photos: postPhoto
};
View
1 package.json
@@ -68,6 +68,7 @@
"should": "",
"socket.io": "",
"temp": ">=0.4.0",
+ "tumblrwks": "https://github.com/Singly/tumblrwks/tarball/master",
"supertest": "",
"underscore": ">=1.3.0",
"winston": "=0.5.2",

0 comments on commit 555f329

Please sign in to comment.
Something went wrong with that request. Please try again.