├── .editorconfig ├── .eslintrc.js ├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | extends: "airbnb-base" 3 | }; 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # parcel-bundler cache (https://parceljs.org/) 61 | .cache 62 | 63 | # next.js build output 64 | .next 65 | 66 | # nuxt.js build output 67 | .nuxt 68 | 69 | # vuepress build output 70 | .vuepress/dist 71 | 72 | # Serverless directories 73 | .serverless 74 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) shawjia (github.com/shawjia) 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 | # geektime 2 | 3 | > API client for time.geekbang.org (极客时间) 4 | 5 | ## INSTALL 6 | ```bash 7 | npm install geektime 8 | # or 9 | yarn add geektime 10 | ``` 11 | 12 | ## EXAMPLE 13 | ```js 14 | const Geektime = require('geektime'); 15 | const client = new Geektime('phone', 'pass'); 16 | 17 | (async () => { 18 | 19 | try { 20 | const products = await client.products(); 21 | console.log(products); 22 | } catch (error) { 23 | console.error(error); 24 | } 25 | 26 | })(); 27 | ``` 28 | 29 | ## API 30 | 31 | ### init 32 | 33 | `new Geektime([country], phone, password)` 34 | 35 | country 为可选,如果不传,当 86 处理 36 | 37 | ### products() 38 | 39 | 返回产品列表 (专栏/视频课/微课/其他) 40 | 41 | ### intro(cid) 42 | 43 | 返回专栏信息 44 | 45 | ### articles(cid, size = 1000) 46 | 47 | 返回专栏文章列表 48 | 49 | ### article(id) 50 | 51 | 返回单篇文章详情 52 | 53 | ### comments(aid, size = 200, prev = 0) 54 | 55 | 返回专栏文章评论 56 | 57 | ### audios(cid, size = 1000) 58 | 59 | 返回音频列表 60 | 61 | ### NOTE 62 | 63 | | param | note | 64 | | --- | --- | 65 | | cid | 专栏 id | 66 | | id | 文章 id | 67 | 68 | ## License 69 | 70 | MIT 71 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const got = require('got'); 2 | 3 | const host = 'https://time.geekbang.org/serv/v1'; 4 | const links = { 5 | login: 'https://account.geekbang.org/account/ticket/login', 6 | products: `${host}/my/products/all`, 7 | productList: `${host}/my/products/list`, 8 | intro: `${host}/column/intro`, 9 | articles: `${host}/column/articles`, 10 | article: `${host}/article`, 11 | comments: `${host}/comments`, 12 | audios: `${host}/column/audios`, 13 | }; 14 | const CN_CODE = '86'; 15 | 16 | async function request(link, body = {}, cookie = '') { 17 | const headers = { 18 | Referer: 'https://servicewechat.com/wxc4f8a61ef62e6e35/20/page-frame.html', 19 | Cookie: cookie, 20 | }; 21 | 22 | try { 23 | const isLoginLink = link === links.login; 24 | const res = await got(link, { 25 | json: true, headers, body, method: 'post', 26 | }); 27 | const loginCookie = isLoginLink 28 | ? res.headers['set-cookie'].map(v => v.split(';')[0]).join('; ') 29 | : ''; 30 | 31 | if (res.body.code === 0) { 32 | return isLoginLink 33 | ? { ...res.body.data, cookie: loginCookie } 34 | : res.body.data; 35 | } 36 | 37 | throw new Error(`Wrong Code: ${res.body.code}`); 38 | } catch (err) { 39 | throw err; 40 | } 41 | } 42 | 43 | class Geektime { 44 | constructor(country, cellphone, password) { 45 | let countryCode = country; 46 | let phone = cellphone; 47 | let pass = password; 48 | 49 | if (password === undefined) { 50 | [countryCode, phone, pass] = [CN_CODE, country, cellphone]; 51 | } 52 | 53 | if (typeof phone !== 'string' || typeof pass !== 'string') { 54 | throw new TypeError('cellphone/password should be string'); 55 | } 56 | 57 | if (phone === '' || pass === '') { 58 | throw new Error('cellphone/password should not be empty'); 59 | } 60 | 61 | this.country = +countryCode; 62 | this.cellphone = phone; 63 | this.password = pass; 64 | this.cookie = null; 65 | } 66 | 67 | // 产品列表,返回 专栏/视频课/微课/其他 68 | async products() { 69 | const cookie = await this.getCookie(); 70 | 71 | const res = await request(links.products, null, cookie); 72 | 73 | // only return 10, we need more! 74 | const parts = res.filter(v => v.page.more); 75 | const fulls = await Promise.all( 76 | parts.map(v => request( 77 | links.productList, { nav_id: v.id, prev: 0, size: 1000 }, cookie, 78 | )), 79 | ); 80 | fulls.forEach((v, index) => { 81 | res.find(m => parts[index].id === m.id).list = v.list; 82 | }); 83 | 84 | return res; 85 | } 86 | 87 | // 专栏介绍 88 | async intro(cid) { 89 | const cookie = await this.getCookie(); 90 | 91 | return request(links.intro, { cid }, cookie); 92 | } 93 | 94 | // 专栏文章列表 95 | async articles(cid, size = 1000) { 96 | const cookie = await this.getCookie(); 97 | 98 | return request(links.articles, { cid, size }, cookie); 99 | } 100 | 101 | // 单篇文章详情 102 | async article(id) { 103 | const cookie = await this.getCookie(); 104 | 105 | return request(links.article, { id }, cookie); 106 | } 107 | 108 | // 文章评论 109 | async comments(aid, size = 200, prev = 0) { 110 | const cookie = await this.getCookie(); 111 | 112 | return request(links.comments, { aid, size, prev }, cookie); 113 | } 114 | 115 | // 音频列表 116 | async audios(cid, size = 1000) { 117 | const cookie = await this.getCookie(); 118 | 119 | return request(links.audios, { cid, size }, cookie); 120 | } 121 | 122 | async getCookie() { 123 | if (this.cookie) { 124 | return this.cookie; 125 | } 126 | 127 | const { cellphone, password, country } = this; 128 | const body = { 129 | cellphone, 130 | password, 131 | country, 132 | remember: 1, 133 | captcha: '', 134 | platform: 4, 135 | appid: 1, 136 | }; 137 | 138 | const { cookie } = await request(links.login, body); 139 | this.cookie = cookie; 140 | 141 | return this.cookie; 142 | } 143 | } 144 | 145 | module.exports = Geektime; 146 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geektime", 3 | "version": "1.1.1", 4 | "description": "API client for time.geekbang.org", 5 | "main": "index.js", 6 | "repository": "https://github.com/shawjia/geektime", 7 | "author": "shawjia (github.com/shawjia)", 8 | "license": "MIT", 9 | "scripts": { 10 | "test": "ava" 11 | }, 12 | "keywords": [ 13 | "geektime", 14 | "API", 15 | "极客时间" 16 | ], 17 | "devDependencies": { 18 | "ava": "^0.25.0", 19 | "eslint": "^5.5.0", 20 | "eslint-config-airbnb-base": "^13.1.0", 21 | "eslint-plugin-import": "^2.14.0" 22 | }, 23 | "dependencies": { 24 | "got": "^9.2.0" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('ava'); 2 | const Geektime = require('.'); 3 | 4 | const { GEEKTIME_PHONE, GEEKTIME_PASS } = process.env; 5 | const client = new Geektime(GEEKTIME_PHONE, GEEKTIME_PASS); 6 | 7 | const cid = 48; // 左耳听风 8 | const articleId = 14271; // "article_title": "高效学习:端正学习态度", 9 | 10 | test('Geektime()', (t) => { 11 | t.throws(() => new Geektime(), TypeError); 12 | }); 13 | 14 | test('Geektime() init', (t) => { 15 | const CODE = 86; 16 | const phone0 = '176'; 17 | const pass0 = 'pass0'; 18 | const c0 = new Geektime(phone0, pass0); 19 | 20 | t.deepEqual(c0.country, CODE); 21 | t.deepEqual(c0.cellphone, phone0); 22 | t.deepEqual(c0.password, pass0); 23 | 24 | const code1 = 1; 25 | const phone1 = '135'; 26 | const pass1 = 'pass1'; 27 | const c1 = new Geektime(code1, phone1, pass1); 28 | 29 | t.deepEqual(c1.country, code1); 30 | t.deepEqual(c1.cellphone, phone1); 31 | t.deepEqual(c1.password, pass1); 32 | }); 33 | 34 | test('client.products()', async (t) => { 35 | const res = await client.products(); 36 | 37 | t.true(res.length > 0); 38 | }); 39 | 40 | test('client.intro(48)', async (t) => { 41 | const res = await client.intro(cid); 42 | 43 | t.is(res.column_title, '左耳听风'); 44 | }); 45 | 46 | test('client.articles(48)', async (t) => { 47 | const res = await client.articles(cid); 48 | 49 | t.true(res.list.length > 0); 50 | }); 51 | 52 | test('client.article(14271)', async (t) => { 53 | const res = await client.article(articleId); 54 | 55 | t.true(res.article_title.startsWith('95 | 高效学习')); 56 | }); 57 | 58 | test('client.comments(14271)', async (t) => { 59 | const res = await client.comments(articleId, 1); 60 | 61 | t.true(res.list.length > 0); 62 | }); 63 | 64 | test('client.audios(48)', async (t) => { 65 | const res = await client.audios(cid); 66 | 67 | t.true(res.list.length > 0); 68 | }); 69 | --------------------------------------------------------------------------------