├── .babelrc ├── .editorconfig ├── .eslintrc ├── .flowconfig ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── index.js ├── package.json ├── src ├── api.js ├── constants.js └── core.js └── test └── test.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | "es2015" 4 | ], 5 | "plugins": ["add-module-exports", "transform-flow-strip-types"] 6 | } 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.{json,js,jsx,html,css}] 11 | indent_style = space 12 | indent_size = 2 13 | 14 | [.eslintrc] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [*.md] 19 | trim_trailing_whitespace = false 20 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "extends": "airbnb-base", 4 | "env": { 5 | "browser": true, 6 | "mocha": true, 7 | "node": true 8 | }, 9 | "rules": { 10 | "arrow-parens": ["off"], 11 | "compat/compat": 2, 12 | "consistent-return": "off", 13 | "comma-dangle": "off", 14 | "generator-star-spacing": "off", 15 | "no-console": 0, 16 | "no-use-before-define": "off", 17 | "promise/param-names": 2, 18 | "promise/always-return": 0, 19 | "promise/catch-or-return": 0, 20 | "promise/no-native": 0, 21 | "flowtype-errors/show-errors": 2, 22 | "class-methods-use-this": 0, 23 | "no-bitwise": 0, 24 | }, 25 | "plugins": [ 26 | "flowtype", 27 | "flowtype-errors", 28 | "promise", 29 | "compat", 30 | "import" 31 | ] 32 | } 33 | -------------------------------------------------------------------------------- /.flowconfig: -------------------------------------------------------------------------------- 1 | [ignore] 2 | 3 | [include] 4 | ./src/* 5 | 6 | [libs] 7 | 8 | [options] 9 | esproposal.class_static_fields=enable 10 | esproposal.class_instance_fields=enable 11 | esproposal.export_star_as=enable 12 | suppress_comment=\\(.\\|\n\\)*\\$FlowFixMe 13 | suppress_comment=\\(.\\|\n\\)*\\$FlowIssue 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | /lib/ 40 | /node_modules/ 41 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /src/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Ryan, Yoon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tistory API for node.js 2 | [![NPM](https://nodei.co/npm/tistory-api.png)](https://nodei.co/npm/tistory-api/) 3 | 4 | **Tistory API**는 nodejs 환경에서 티스토리 블로그 API를 쉽게 사용할 수 있도록 제공합니다. 5 | 6 | ## Installation 7 | 8 | ```bash 9 | npm install tistory-api 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```javascript 15 | import Tistory from 'tistory-api'; 16 | const params = { 17 | access_token: 'abc...XYZ', // put tistory's access token. 18 | output: 'json' 19 | }; 20 | const tistory = new Tistory(params); 21 | 22 | // get user's blogs 23 | tistory.blog.info().then(res => { 24 | // handle the blog information here 25 | }).catch(err => { 26 | // handle the exception here 27 | console.log(err); 28 | }); 29 | 30 | // get posts list from A blog 31 | tistory.post.list({ 32 | // required 33 | blogName: 'A', 34 | // optional 35 | page: 1, 36 | // optional 37 | count: 10, 38 | // optional 39 | categoryId: 0, 40 | // optional 41 | sort: 'id' 42 | }).then(res => { 43 | // handle the post list here 44 | }).catch(err => { 45 | // handle the exception here 46 | console.log(err); 47 | }); 48 | 49 | ``` 50 | 51 | ## Features 52 | 53 | 모든 `API`에 대한 정의는 [`src/api.js`](./src/api.js) 파일을 참고 하면 됩니다. 그리고 모든 메소드는 `Promise` 객체를 반환하게 됩니다. 54 | 55 | > API 응답을 포함한 더 자세한 사항은 공식 Tistory API에서 확인하세요. 56 | > [https://tistory.github.io/document-tistory-apis](https://tistory.github.io/document-tistory-apis) 57 | 58 | ## Build 59 | ```bash 60 | npm run build 61 | ``` 62 | 63 | ## Test 64 | ```bash 65 | npm run test 66 | ``` 67 | 68 | ## License 69 | [MIT](https://opensource.org/licenses/MIT) 70 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const core = require('./lib/core'); 2 | 3 | module.exports = core; 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tistory-api", 3 | "version": "0.1.4", 4 | "description": "tistory-api는 nodejs 환경에서 티스토리 블로그 API를 쉽게 사용할 수 있도록 제공합니다.", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "eslint src/ *.js test/ && echo Lint passed.", 8 | "test": "cross-env NODE_ENV=production node -r babel-register ./test/test", 9 | "build": "cross-env NODE_ENV=production node -r babel-register ./node_modules/babel-cli/bin/babel src --out-dir lib" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/ryan0822/tistory-api.git" 14 | }, 15 | "keywords": [ 16 | "blog", 17 | "tistory", 18 | "api" 19 | ], 20 | "author": "ryan0822 (https://github.com/ryan0822)", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/ryan0822/tistory-api/issues" 24 | }, 25 | "homepage": "https://github.com/ryan0822/tistory-api#readme", 26 | "devEngines": { 27 | "node": ">=6.x", 28 | "npm": ">=3.x" 29 | }, 30 | "dependencies": { 31 | "request": "^2.79.0" 32 | }, 33 | "devDependencies": { 34 | "babel-cli": "^6.22.2", 35 | "babel-core": "^6.21.0", 36 | "babel-eslint": "^7.1.1", 37 | "babel-plugin-add-module-exports": "^0.2.1", 38 | "babel-plugin-transform-flow-strip-types": "^6.22.0", 39 | "babel-preset-es2015": "^6.22.0", 40 | "babel-register": "^6.22.0", 41 | "cross-env": "^3.1.4", 42 | "eslint": "^3.13.1", 43 | "eslint-config-airbnb-base": "^11.0.1", 44 | "eslint-loader": "^1.6.1", 45 | "eslint-plugin-compat": "^1.0.0", 46 | "eslint-plugin-flowtype": "^2.29.2", 47 | "eslint-plugin-flowtype-errors": "^2.0.3", 48 | "eslint-plugin-import": "^2.2.0", 49 | "eslint-plugin-promise": "^3.4.0", 50 | "flow-bin": "^0.37.4" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/api.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | import { REQUIRED, OPTIONAL, MULTIPART } from './constants'; 3 | 4 | const defaultParamTypes = { 5 | access_token: REQUIRED, 6 | output: OPTIONAL 7 | }; 8 | 9 | const makeParamType = (obj = {}) => Object.assign({}, defaultParamTypes, obj); 10 | 11 | const Api:{ 12 | [key: string]: { 13 | method: string, 14 | paramTypes: Object 15 | } 16 | } = { 17 | 18 | 'blog.info': { 19 | method: 'POST', 20 | paramTypes: makeParamType() 21 | }, 22 | 23 | 'category.list': { 24 | method: 'POST', 25 | paramTypes: makeParamType({ 26 | blogName: REQUIRED 27 | }) 28 | }, 29 | 30 | 'post.list': { 31 | method: 'POST', 32 | paramTypes: makeParamType({ 33 | blogName: REQUIRED, 34 | page: OPTIONAL, 35 | count: OPTIONAL, 36 | categoryId: OPTIONAL, 37 | sort: OPTIONAL 38 | }) 39 | }, 40 | 41 | 'post.read': { 42 | method: 'POST', 43 | paramTypes: makeParamType({ 44 | blogName: REQUIRED, 45 | postId: REQUIRED 46 | }) 47 | }, 48 | 49 | 'post.write': { 50 | method: 'POST', 51 | paramTypes: makeParamType({ 52 | blogName: REQUIRED, 53 | title: REQUIRED, 54 | visibility: OPTIONAL, 55 | published: OPTIONAL, 56 | category: OPTIONAL, 57 | content: OPTIONAL, 58 | slogan: OPTIONAL, 59 | tag: OPTIONAL 60 | }) 61 | }, 62 | 63 | 'post.modify': { 64 | method: 'POST', 65 | paramTypes: makeParamType({ 66 | blogName: REQUIRED, 67 | title: REQUIRED, 68 | postId: REQUIRED, 69 | visibility: OPTIONAL, 70 | category: OPTIONAL, 71 | content: OPTIONAL, 72 | slogan: OPTIONAL, 73 | tag: OPTIONAL 74 | }) 75 | }, 76 | 77 | 'post.attach': { 78 | method: 'POST', 79 | paramTypes: makeParamType({ 80 | blogName: REQUIRED, 81 | uploadedfile: REQUIRED | MULTIPART 82 | }) 83 | }, 84 | 85 | 'post.delete': { 86 | method: 'POST', 87 | paramTypes: makeParamType({ 88 | blogName: REQUIRED, 89 | postId: REQUIRED 90 | }) 91 | }, 92 | 93 | 'comment.list': { 94 | method: 'POST', 95 | paramTypes: makeParamType({ 96 | blogName: REQUIRED, 97 | postId: REQUIRED 98 | }) 99 | }, 100 | 101 | 'comment.newest': { 102 | method: 'POST', 103 | paramTypes: makeParamType({ 104 | blogName: REQUIRED, 105 | postId: REQUIRED, 106 | page: OPTIONAL, 107 | count: OPTIONAL 108 | }) 109 | }, 110 | 111 | 'comment.write': { 112 | method: 'POST', 113 | paramTypes: makeParamType({ 114 | blogName: REQUIRED, 115 | postId: REQUIRED, 116 | content: REQUIRED, 117 | parentId: OPTIONAL, 118 | secret: OPTIONAL 119 | }) 120 | }, 121 | 122 | 'comment.modify': { 123 | method: 'POST', 124 | paramTypes: makeParamType({ 125 | blogName: REQUIRED, 126 | postId: REQUIRED, 127 | commentId: REQUIRED, 128 | content: REQUIRED, 129 | parentId: OPTIONAL, 130 | secret: OPTIONAL 131 | }) 132 | }, 133 | 134 | 'comment.delete': { 135 | method: 'POST', 136 | paramTypes: makeParamType({ 137 | blogName: REQUIRED, 138 | postId: REQUIRED, 139 | commentId: REQUIRED 140 | }) 141 | }, 142 | 143 | 'guestbook.list': { 144 | method: 'POST', 145 | paramTypes: makeParamType({ 146 | blogName: REQUIRED 147 | }) 148 | }, 149 | 150 | 'guestbook.write': { 151 | method: 'POST', 152 | paramTypes: makeParamType({ 153 | blogName: REQUIRED, 154 | content: REQUIRED, 155 | parentId: OPTIONAL, 156 | secret: OPTIONAL 157 | }) 158 | }, 159 | 160 | 'guestbook.modify': { 161 | method: 'POST', 162 | paramTypes: makeParamType({ 163 | blogName: REQUIRED, 164 | guestbookId: REQUIRED, 165 | content: REQUIRED, 166 | parentId: OPTIONAL, 167 | secret: OPTIONAL 168 | }) 169 | }, 170 | 171 | 'guestbook.delete': { 172 | method: 'POST', 173 | paramTypes: makeParamType({ 174 | blogName: REQUIRED, 175 | guestbookId: REQUIRED 176 | }) 177 | } 178 | }; 179 | 180 | export default Api; 181 | -------------------------------------------------------------------------------- /src/constants.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | export const REQUIRED:number = 1; 3 | export const OPTIONAL:number = 2; 4 | export const MULTIPART:number = 4; 5 | export const BASE_URL:string = 'https://www.tistory.com/apis'; 6 | -------------------------------------------------------------------------------- /src/core.js: -------------------------------------------------------------------------------- 1 | // @flow 2 | import request from 'request'; 3 | import fs from 'fs'; 4 | import { REQUIRED, MULTIPART, BASE_URL } from './constants'; 5 | import Api from './api'; 6 | 7 | const create = (key, define, p) => ( 8 | (params = {}) => { 9 | const mergedParams = Object.assign({}, p, params); 10 | let isMultipart = false; 11 | Object.keys(define.paramTypes).forEach((paramName) => { 12 | const required = REQUIRED & define.paramTypes[paramName]; 13 | if (required && !Object.prototype.hasOwnProperty.call(mergedParams, paramName)) { 14 | throw new Error(`check params!! ${paramName}`); 15 | } 16 | 17 | if (MULTIPART & define.paramTypes[paramName]) { 18 | if (fs.existsSync(mergedParams[paramName])) { 19 | mergedParams[paramName] = fs.createReadStream(mergedParams[paramName]); 20 | isMultipart = true; 21 | } else { 22 | throw new Error(`file not exist!! ${mergedParams[paramName]}`); 23 | } 24 | } 25 | }); 26 | 27 | return new Promise((resolve, reject) => { 28 | const opt:{[key: string]: any } = { 29 | method: define.method || 'POST', 30 | url: `${BASE_URL}/${key.replace(/\./g, '/')}` 31 | }; 32 | if (isMultipart) { 33 | opt.formData = mergedParams; 34 | } else { 35 | opt.form = mergedParams; 36 | } 37 | 38 | request(opt, (err, res, body) => { 39 | if (err) { 40 | reject(err); 41 | } else if (res.statusCode !== 200) { 42 | let msg = res.statusCode; 43 | if (body) { 44 | msg = `[${res.statusCode}] ${JSON.parse(body).tistory.error_message}`; 45 | } 46 | reject(new Error(msg)); 47 | } else { 48 | resolve(JSON.parse(body).tistory); 49 | } 50 | }); 51 | }); 52 | } 53 | ); 54 | 55 | export default class Tistory extends Object { 56 | constructor(params:{ [path: string]: any } = {}) { 57 | super(); 58 | Object.keys(Api).forEach(key => { 59 | key.split('.').reduce((p, c, i, a) => { 60 | const tmp = p; 61 | if (i !== a.length - 1) { 62 | if (!tmp[c]) tmp[c] = {}; 63 | } else { 64 | tmp[c] = create(key, Api[key], params); 65 | } 66 | return tmp[c]; 67 | }, this); 68 | }); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import Tistory from '../index'; 2 | 3 | const params = { 4 | access_token: '', // put tistory's access token. 5 | output: 'json' 6 | }; 7 | 8 | const tistory = new Tistory(params); 9 | 10 | tistory.blog.info().then(res => { 11 | console.log(res); 12 | return res.item.blogs[0].name; 13 | }).catch(err => { 14 | console.log(err); 15 | }).then(blogName => tistory.post.list({ 16 | blogName, 17 | page: 1, 18 | count: 10, 19 | categoryId: 0, 20 | sort: 'id' 21 | }).then(res => { 22 | console.log(res); 23 | }).catch(err => { 24 | console.log(err); 25 | })); 26 | --------------------------------------------------------------------------------