Skip to content

Instantly share code, notes, and snippets.

@nowelium
Created March 23, 2012 19:34
Show Gist options
  • Save nowelium/2174155 to your computer and use it in GitHub Desktop.
Save nowelium/2174155 to your computer and use it in GitHub Desktop.
posterous REST API. using 'request' module
var async = require('async');
var mysql = require('mysql');
var request = require('request');
var qs = require('querystring');
var Posterous = function(user, password, apiToken){
this.user = user;
this.password = password;
this.token = apiToken;
};
Posterous.prototype.ifAbsent = function(value, validate){
if(!validate(value)){
throw new Error('invalid type:' + value);
}
};
Posterous.prototype.createNewPost = function(siteId, title, body, tags, autoPost, displayDate, isPrivate, callback){
this.ifAbsent(title, function(v){ return typeof v == 'string' });
this.ifAbsent(tags, Array.isArray);
//this.ifAbsent(displayDate, function(v){ return v.constructor == Date });
var url = 'http://posterous.com/api/2/sites/' + siteId + '/posts';
var parameter = {
url: url,
headers: {
'Host': 'posterous.com',
// basic auth
'Authorization': new Buffer(this.user + ':' + this.password).toString('base64')
},
body: qs.stringify({
'api_token': this.token,
'site_id': siteId,
'post[title]': title,
'post[body]': body,
'post[tags]': tags.join(','),
'post[autopost]': autoPost.toString(),
'post[display_date]': displayDate,
'post[source]': 'node-posterous',
'post[is_private]': isPrivate.toString()
})
};
return request.post(parameter, function(err, response, body){
if(err){
return callback(err);
}
if(response.statusCode == 201){
// 201 Created
return callback(null, body);
}
return callback('error: ' + response.statusCode + ', ' + body);
});
};
var posterous = new Posterous('[email protected]', 'password', 'authtoken');
/* example::
posterous.createNewPost(
'nowelium',
'hello world',
'test test',
['nodejs', 'request'],
false,
'2013-03-24 00:00:09',
false,
function (err, results){
console.log(err);
}
);
*/
// p_blog_log backup example::
var client = mysql.createClient({
host: 'localhost',
user: 'user',
password: 'pass',
database: 'myblog'
});
var query = [];
query.push('SELECT id, name, category, comment, CAST(`mod` AS CHAR) AS modified_at');
query.push('FROM p_blog_log');
query.push('WHERE draft = 0');
query.push('ORDER BY id DESC');
query.push('LIMIT 1')
var parallel = [];
parallel.push(function(next){
return client.query(query.join(' '), [], function(err, results){
if(err){
return next(err);
}
var serial = [];
results.forEach(function(result){
serial.push(function(subNext){
return posterous.createNewPost(
'nowelium',
result['name'],
result['comment'],
result['category'].split(',').map(function(v){ return v.trim() }),
false,
result['modified_at'],
false, function (err, results){
if(err){
return subNext(err);
}
return subNext(null);
});
});
});
return next(null, serial);
});
});
async.parallel(parallel, function(err, results){
if(err){
console.error(err);
return process.exit(1);
}
var serial = results.shift();
return async.series(serial, function(err, results){
if(err){
console.error(err);
return process.exit(1);
}
console.log('done');
return process.exit(0);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment