Skip to content

Commit

Permalink
create simple post and category functions using with Tistory API
Browse files Browse the repository at this point in the history
  • Loading branch information
sungkwangsong committed Jul 15, 2014
1 parent 34beaad commit 391aafe
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ build/Release
# Deployed apps should consider commenting this line out:
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules

.idea
examples/example-options.js
23 changes: 23 additions & 0 deletions examples/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Created by saltfactory on 7/15/14.
*/
var Tistory = require('../lib/tistory'),
logger = require('./logger'),
options = require('./example-options')

var tistory = new Tistory(options);
//tistory.category.list(null,
// function (err, body) {
// var json = JSON.parse(body);
// logger.debug(json.tistory.item.categories)
// }, {targetUrl: 'rubygalaxy'});


tistory.category.findCategoryId('Ruby', function(err, categoryId){
if(err){
logger.error(err.message);
} else {
logger.info("categoryId: " + categoryId);
}

});
19 changes: 19 additions & 0 deletions examples/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Created by saltfactory on 7/15/14.
*/
var tracer = require('tracer')
.colorConsole(
{
format: [
"{{timestamp}} (in {{file}}:{{line}}) <{{title}}> {{message}} ", //default format
{
error: "{{timestamp}} (in {{file}}:{{line}}) <{{title}}> {{message}} \nCall Stack:\n{{stack}}" // error format
}
],
dateformat: "HH:MM:ss.L",
preprocess: function (data) {
data.title = data.title.toUpperCase();
}
});

module.exports = tracer;
50 changes: 50 additions & 0 deletions examples/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var Tistory = require('../lib/tistory'),
logger = require('./logger'),
options = require('./example-options');

var tistory = new Tistory(options);

var callback = function (err, body) {
if (err) {
logger.error(err);
} else {
logger.debug(body);
var json = JSON.parse(body);
logger.info(json.tistory);
}
};

//tistory.post.list({
// page: 1, //default:1
// count: 10, //default:10, max:30
//// categoryId: //default all categories
// sort: 'id' //default:id(글번호) , date(날짜)
//}, callback);
tistory.post.list({page: 1}, callback);

//tistory.post.read({postId: 12}, callback);
//
//tistory.post.write({
// visibility: 0, //default: 0(비공개), 1(보호), 2(공개), 3(발행), 생략시 비공개
//// published: //UNIX_TIMESTAMP() 값을 넣을 경우, 예약발행
// categoryId: 0, //default:0 (생략시 분류없음)
// tag: 'nodejs, node-tistory',
// title: 'test',
// content: '본문'
//}, callback);
//
//tistory.post.update({
// postId: 11,
// visibility: 0, //default: 0(비공개), 1(보호), 2(공개), 3(발행), 생략시 비공개
// categoryId: 0, //default:0 (생략시 분류없음)
// tag: 'nodejs, node-tistory',
// title: 'test',
// content: '본문 수정'
//}, callback);
//
//tistory.post.attach({uploadedfile: '/Users/saltfactory/Downloads/square.png'}, callback);
//
//tistory.post.delete({postId: 11}, callback);



50 changes: 50 additions & 0 deletions lib/tistory/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Created by saltfactory on 7/15/14.
*/
var request = require('./request'),
util = require('util');

/**
* Category
*
* @param options
* @constructor
*/
function Category(options) {
this.options = options || {};
}

Category.prototype.list = function (params, callback, options) {

var options = util._extend(this.options, options);
var url = options.baseUrl + '/category/list';

request.post(url, options, params, callback);
};

Category.prototype.findCategoryId = function (name, callback, options) {
this.list(null, function (err, body) {
var json = JSON.parse(body);
var categories = json.tistory.item.categories.category;

var categoryId;

categories.forEach(function (category) {
if (category.name == name) {
categoryId = category.id;
}
});

if (categoryId == undefined){
callback(new Error('Not found category'));
} else {
callback(err, categoryId);
}



}, options);
}


module.exports = Category;
7 changes: 7 additions & 0 deletions lib/tistory/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Created by saltfactory on 7/15/14.
*/
var Tistory = require('./tistory');

exports = module.exports = Tistory;
exports.Strategy = Tistory;
116 changes: 116 additions & 0 deletions lib/tistory/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/**
* Created by saltfactory on 7/15/14.
*/
var request = require('./request'),
util = require('util');
/**
* Post
*
* @param options
* @constructor
*/
function Post(options) {
this.options = options || {};
}

/**
* list
*
* @param options
* @param callback
*/
Post.prototype.list = function (params, callback, options) {

options = util._extend(this.options, options);

var url = options.baseUrl + '/post/list';

request.post(url, options, params, callback);
};

/**
* write
*
* @param params
* @param callback
* @param options
*/
Post.prototype.write = function (params, callback, options) {
var options = extend(this.options, options);
var url = options.baseUrl + '/post/write';

request.post(url, options, params, callback);
};

/**
* update
*
* @param params
* @param callback
* @param options
*/
Post.prototype.update = function (params, callback, options) {
var options = extend(this.options, options);
var url = options.baseUrl + '/post/modify';

request.post(url, options, params, callback);
};

/**
* delete
*
* @param params
* @param callback
* @param options
*/
Post.prototype.delete = function (params, callback, options) {
var options = extend(this.options, options);
var url = options.baseUrl + '/post/delete';

request.post(url, options, params, callback);
};

/**
* attach
*
* @param params
* @param callback
* @param option
*/
Post.prototype.attach = function (params, callback, options) {
var options = extend(this.options, options);
var url = options.baseUrl + '/post/attach';

request.form(url, options, params, callback);

//// params.uploadedfile = fs.createReadStream(params.uploadedfile);
//// _post(url, options, params, callback);
//
// var r = request.post(url, function (err, reponse, body) {
// callback(err, body);
// });
//
// var form = r.form()
// form.append('access_token', options.accessToken);
// form.append('targetUrl', options.targetUrl);
// form.append('output', options.output);
// form.append('uploadedfile', fs.createReadStream(params.uploadedfile));
//// form.append('remote_file', request('http://google.com/doodle.png'))
//
};

/**
* read
*
* @param params
* @param callback
* @param options
*/
Post.prototype.read = function (params, callback, options) {
var options = extend(this.options, options);
var url = options.baseUrl + '/post/read';

request.post(url, options, params, callback);
};

module.exports = Post;
50 changes: 50 additions & 0 deletions lib/tistory/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Created by saltfactory on 7/15/14.
*/
var request = require('request'),
util = require('util'),
fs = require('fs');


exports.post = function(url, options, params, callback) {

var form = {
access_token: options.accessToken,
targetUrl: options.targetUrl,
output: options.output
};

form = util._extend(form, params);

request.post(url, {form: form}, function (err, res, body) {
callback(err, body)
});
};

/**
* form
*
* @param url
* @param options
* @param params
* @param callback
*/
exports.form = function(url, options, params, callback) {
var r = request.post(url, function (err, reponse, body) {
callback(err, body);
});

var form = r.form()
form.append('access_token', options.accessToken);
form.append('targetUrl', options.targetUrl);
form.append('output', options.output);

var keys = Object.keys(params);
keys.forEach(function (key) {
form.append(key, params[key]);
if (key == 'uploadedfile'){
form.append('uploadedfile', fs.createReadStream(params.uploadedfile));
}
});
};

16 changes: 16 additions & 0 deletions lib/tistory/tistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Created by saltfactory on 7/15/14.
*/
var util = require('util'),
Post = require('./post'),
Category = require('./category');

function Tistory(options) {
this.options = util._extend({}, options);
this.options.output = options.output || 'json'
this.options.baseUrl = 'https://www.tistory.com/apis/';
this.post = new Post(this.options);
this.category = new Category(this.options);
}

module.exports = Tistory;
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "node-tistory",
"version": "0.1.0",
"description": "Tistory Client module",
"main": "lib/tistory",
"scripts": {
"test": "tistory-test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/saltfactory/node-tistory.git"
},
"keywords": [
"node",
"nodejs",
"api",
"tistory",
"saltfactory"
],
"author": "SungKwang Song",
"license": "MIT",
"bugs": {
"url": "https://github.com/saltfactory/node-tistory/issues"
},
"homepage": "https://github.com/saltfactory/node-tistory",
"devDependencies":{
"tracer":"latest"
},
"dependencies":{
"request":"latest"
}

}

0 comments on commit 391aafe

Please sign in to comment.