├── images └── cookie.png ├── README.md ├── package.json ├── LICENSE ├── .gitignore └── weibo-post.js /images/cookie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itibbers/weibo-post/HEAD/images/cookie.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # weibo-post 2 | 3 | Post weibo text from https based on node. 4 | 5 | ## Usage 6 | 7 | Using npm: 8 | ```shell 9 | $ npm i --save-dev weibo-post 10 | ``` 11 | 12 | In Node.js: 13 | ```js 14 | var weiboPost = require('weibo-post'); 15 | 16 | weiboPost.setCookie('your weibo login cookie'); 17 | weiboPost.post('your post content'); 18 | ``` 19 | 20 | ## How to get *weibo cookie*? 21 | 22 | ![How to get cookie](./images/cookie.png) 23 | 24 | 1. Open weibo.com by Chrome. 25 | 2. Tap F12 to open Terminal, change to Network tab. 26 | 3. Post a weibo, follow the image indicate. 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "weibo-post", 3 | "version": "1.0.1", 4 | "description": "weibo post.", 5 | "main": "weibo-post.js", 6 | "scripts": { 7 | "test": "node ./test/test.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/itibbers/weibo-post.git" 12 | }, 13 | "keywords": [ 14 | "weibo", 15 | "post" 16 | ], 17 | "author": "iTibbers", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/itibbers/weibo-post/issues" 21 | }, 22 | "homepage": "https://github.com/itibbers/weibo-post#readme", 23 | "devDependencies": { 24 | "https": "^1.0.0", 25 | "querystring": "^0.2.0" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ryan Ji 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 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # System 2 | .DS_Store 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | yarn-debug.log* 9 | yarn-error.log* 10 | 11 | # Runtime data 12 | pids 13 | *.pid 14 | *.seed 15 | *.pid.lock 16 | 17 | # Directory for instrumented libs generated by jscoverage/JSCover 18 | lib-cov 19 | 20 | # Coverage directory used by tools like istanbul 21 | coverage 22 | 23 | # nyc test coverage 24 | .nyc_output 25 | 26 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 27 | .grunt 28 | 29 | # Bower dependency directory (https://bower.io/) 30 | bower_components 31 | 32 | # node-waf configuration 33 | .lock-wscript 34 | 35 | # Compiled binary addons (http://nodejs.org/api/addons.html) 36 | build/Release 37 | 38 | # Dependency directories 39 | node_modules/ 40 | jspm_packages/ 41 | 42 | # Typescript v1 declaration files 43 | typings/ 44 | 45 | # Optional npm cache directory 46 | .npm 47 | 48 | # Optional eslint cache 49 | .eslintcache 50 | 51 | # Optional REPL history 52 | .node_repl_history 53 | 54 | # Output of 'npm pack' 55 | *.tgz 56 | 57 | # Yarn Integrity file 58 | .yarn-integrity 59 | 60 | # dotenv environment variables file 61 | .env 62 | 63 | # test 64 | test/ 65 | -------------------------------------------------------------------------------- /weibo-post.js: -------------------------------------------------------------------------------- 1 | /** 2 | * weibo-post 3 | * Released under MIT license 4 | * Copyright Ryan Ji 5 | */ 6 | var querystring = require('querystring'); 7 | var https = require('https'); 8 | 9 | let weiboPost = { 10 | cookie: '', 11 | 12 | /** 13 | * cook: weibo login cookie. 14 | * How to get your cookie, 15 | * please see . 16 | */ 17 | setCookie(cook) { 18 | this.cookie = cook; 19 | }, 20 | 21 | /** 22 | * str: post text. 23 | * Now only support *String*. 24 | */ 25 | post(str) { 26 | 27 | if (this.cookie === '') { 28 | console.log('Error: Cookie not set!'); 29 | return; 30 | } 31 | 32 | // Build the post string from an object 33 | var post_data = querystring.stringify({ 34 | 'location': 'v6_content_home', 35 | 'text': str, 36 | 'appkey': '', 37 | 'style_type': '1', 38 | 'pic_id': '', 39 | 'tid': '', 40 | 'pdetail': '', 41 | 'mid': '', 42 | 'isReEdit': 'false', 43 | 'rank': '0', 44 | 'rankid': '', 45 | 'module': 'stissue', 46 | 'pub_source': 'main_', 47 | 'pub_type': 'dialog', 48 | 'isPri': '0', 49 | '_t': '0' 50 | }); 51 | 52 | // An object of options to indicate where to post to 53 | var post_options = { 54 | host: 'weibo.com', 55 | port: '443', 56 | path: '/aj/mblog/add?ajwvr=6&__rnd=' + new Date().getTime(), 57 | method: 'POST', 58 | headers: { 59 | 'Accept': '*/*', 60 | 'Accept-Encoding': 'gzip, deflate, br', 61 | 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7', 62 | 'Connection': 'keep-alive', 63 | 'Content-Length': Buffer.byteLength(post_data), 64 | 'Content-Type': 'application/x-www-form-urlencoded', 65 | 'Cookie': this.cookie, 66 | 'Host': 'weibo.com', 67 | 'Origin': 'https://weibo.com', 68 | 'Referer': 'https://weibo.com', 69 | 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36', 70 | 'X-Requested-With': 'XMLHttpRequest' 71 | } 72 | }; 73 | 74 | // Set up the request 75 | var post_req = https.request(post_options, res => { 76 | res.setEncoding('utf8'); 77 | 78 | console.log('Status: ' + res.statusCode); 79 | console.log('headers: ' + JSON.stringify(res.headers)); 80 | 81 | switch (res.statusCode) { 82 | case 200: 83 | console.log('\nSent!'); 84 | break; 85 | default: 86 | console.log('\nError!'); 87 | } 88 | 89 | res.on('data', chunk => { 90 | // console.log(Buffer.isBuffer(chunk)); 91 | // console.log(typeof chunk); 92 | // console.log(chunk); 93 | }); 94 | 95 | res.on('end', () => { 96 | // console.log('\nSent!'); 97 | }); 98 | }); 99 | 100 | post_req.on('error', e => { 101 | console.log(`Error: ${e.message}`); 102 | }); 103 | 104 | // post the data 105 | post_req.write(post_data); 106 | post_req.end(); 107 | 108 | } 109 | }; 110 | 111 | module.exports = weiboPost; --------------------------------------------------------------------------------