@saber2pr/baidu-chart-api
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -43,3 +43,3 @@ #!/usr/bin/env node | ||
* @Last Modified by: saber2pr | ||
* @Last Modified time: 2019-06-22 16:41:18 | ||
* @Last Modified time: 2019-06-26 14:46:31 | ||
*/ | ||
@@ -49,2 +49,3 @@ var node_1 = require("@saber2pr/node"); | ||
var path_1 = require("path"); | ||
var utils_1 = require("./core/utils"); | ||
var arg = node_1.Terminal.getParams()[0]; | ||
@@ -55,10 +56,13 @@ /** cookie 缓存目录 **/ | ||
return __awaiter(this, void 0, void 0, function () { | ||
var pkgInfor, cookie, isLogined, inputs, baiduAPI, keywords, results, file_path, error_1; | ||
var id, pkgInfor, cookie, isLogined, inputs, baiduAPI, keywords, pass_days, delta, results, file_path, error_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, node_1.Terminal.getCurrentPkgConfig(__dirname)]; | ||
case 0: | ||
id = utils_1.getID(); | ||
node_1.Terminal.notice("qwq:\u4E3B\u4EBA\uFF01" + id + "\u53F7\u673A\u4E3A\u60A8\u670D\u52A1\uFF01\u8BF7\u591A\u591A\u5173\u7167\uFF01\n"); | ||
return [4 /*yield*/, node_1.Terminal.getCurrentPkgConfig(__dirname)]; | ||
case 1: | ||
pkgInfor = _a.sent(); | ||
if (/-v|--version/.test(arg)) { | ||
node_1.Terminal.tips("version:" + pkgInfor.version + "\n"); | ||
node_1.Terminal.notice("version:" + pkgInfor.version + "\n"); | ||
node_1.Terminal.tips("@saber2pr >> github: https://github.com/Saber2pr/baidu-chart-api\n"); | ||
@@ -76,17 +80,18 @@ return [2 /*return*/]; | ||
case 4: | ||
node_1.Terminal.tips('qwq:输入你的认证信息?\n'); | ||
node_1.Terminal.notice('qwq:主人请先介绍一下自己吧!\n'); | ||
return [4 /*yield*/, node_1.Terminal.getUserInput('百度帐号cookie:\n')]; | ||
case 5: | ||
cookie = _a.sent(); | ||
node_1.Terminal.success('qwq:你以后就是我的master啦.\n'); | ||
_a.label = 6; | ||
case 6: | ||
if (!cookie) return [3 /*break*/, 8]; | ||
node_1.Terminal.success('qwq:认证信息已完整(ok).\n'); | ||
node_1.Terminal.success('qwq:认证信息已完整(ok).'); | ||
return [4 /*yield*/, node_1.FS.writeFile(cookie_temp, cookie)]; | ||
case 7: | ||
_a.sent(); | ||
node_1.Terminal.success("\nqwq:cookie\u5DF2\u7F13\u5B58 >> " + cookie_temp); | ||
node_1.Terminal.success("qwq:cookie\u5DF2\u7F13\u5B58 >> " + cookie_temp + "\n"); | ||
return [3 /*break*/, 10]; | ||
case 8: | ||
node_1.Terminal.error('qwq:出错了呢QAQ!请再试一次?\n'); | ||
node_1.Terminal.error('qwq:诶?!出错了呢QAQ!主人请再试一次?\n'); | ||
return [4 /*yield*/, node_1.FS.remove(cookie_temp)]; | ||
@@ -98,6 +103,10 @@ case 9: | ||
/** 表单 **/ | ||
node_1.Terminal.tips('qwq:输入你要搜的关键词?\n'); | ||
node_1.Terminal.notice('qwq:主人想了解什么呢?(是我的身体吗?\\坏笑)'); | ||
return [4 /*yield*/, node_1.Terminal.getUserInput('关键词(可以多个,逗号分隔):')]; | ||
case 11: | ||
inputs = _a.sent(); | ||
if (!inputs) { | ||
node_1.Terminal.notice("\nqwq: " + id + "\u53F7\u673A\u6CA1\u6709\u542C\u5230\u4E3B\u4EBA\u7684\u547D\u4EE4\uFF0C\u6B64\u6B21\u4EFB\u52A1\u5931\u8D25\uFF01"); | ||
return [2 /*return*/]; | ||
} | ||
baiduAPI = new core_1.BaiduChart(cookie); | ||
@@ -108,29 +117,50 @@ keywords = inputs | ||
.filter(function (_) { return _; }); | ||
node_1.Terminal.tips('\nqwq:正在努力搜索中...\n'); | ||
_a.label = 12; | ||
console.log(); | ||
node_1.Terminal.notice('qwq:主人想知道过去几天的数据呐?'); | ||
return [4 /*yield*/, node_1.Terminal.getUserInput('天数(可跳过,默认收集过去365天):')]; | ||
case 12: | ||
_a.trys.push([12, 15, , 17]); | ||
return [4 /*yield*/, Promise.all(keywords.map(function (keyword) { return baiduAPI.search(keyword); }))]; | ||
pass_days = (_a.sent()) || '365'; | ||
try { | ||
utils_1.verify(pass_days); | ||
} | ||
catch (error) { | ||
console.log('qwq:啊啊啊!主人你的输入不对呢,必须为正整数!'); | ||
} | ||
console.log(); | ||
return [4 /*yield*/, node_1.Terminal.getUserInput('qwq:采样时间间隔?\n频率(单位:天/次)(可跳过,默认1天/次):')]; | ||
case 13: | ||
delta = (_a.sent()) || '1'; | ||
try { | ||
utils_1.verify(delta); | ||
} | ||
catch (error) { | ||
console.log('qwq:啊啊啊!主人你的输入不对呢,必须为正整数!'); | ||
} | ||
node_1.Terminal.notice('\nqwq:主人请先喝口水,正在努力搜索中...\n'); | ||
_a.label = 14; | ||
case 14: | ||
_a.trys.push([14, 17, , 19]); | ||
return [4 /*yield*/, Promise.all(keywords.map(function (keyword) { return baiduAPI.search(keyword, Number(pass_days)); }))]; | ||
case 15: | ||
results = _a.sent(); | ||
console.log(results); | ||
node_1.Terminal.tips("\nqwq:\u641C\u7D22\u6210\u529F\uFF01\u5173\u952E\u8BCD:[" + keywords + "]\n \u4E3B\u4EBA\u8BF7\u6536\u597D\u6570\u636E\uFF01"); | ||
node_1.Terminal.notice("\nqwq:\u641C\u7D22\u6210\u529F\uFF01\u5173\u952E\u8BCD:[" + keywords + "]\n \u4E3B\u4EBA\u8BF7\u6536\u597D\u6570\u636E\uFF01"); | ||
file_path = path_1.join(process.cwd(), "Excel\u6570\u636E_" + keywords.join('-') + "_" + Date.now() + ".csv"); | ||
return [4 /*yield*/, node_1.FS.writeFile(file_path, core_1.searchResultsToCsv(results))]; | ||
case 14: | ||
return [4 /*yield*/, node_1.FS.writeFile(file_path, core_1.searchResultsToCsv(results, Number(delta)))]; | ||
case 16: | ||
_a.sent(); | ||
node_1.Terminal.tips("\nqwq:Excel\u6587\u4EF6\u751F\u6210\u6210\u529F\uFF01>> " + file_path); | ||
return [3 /*break*/, 17]; | ||
case 15: | ||
node_1.Terminal.notice("\nqwq:Excel\u6587\u4EF6\u751F\u6210\u6210\u529F\uFF01>> " + file_path + "\n"); | ||
return [3 /*break*/, 19]; | ||
case 17: | ||
error_1 = _a.sent(); | ||
node_1.Terminal.error('搜索失败!可能是cookie不正确或失效!\n'); | ||
node_1.Terminal.error('qwq:搜索失败啦!可能是cookie不正确或失效!\n'); | ||
return [4 /*yield*/, node_1.FS.remove(cookie_temp)]; | ||
case 16: | ||
case 18: | ||
_a.sent(); | ||
node_1.Terminal.tips('提示:1. 重新复制一次cookie(以BAIDUID开头,注意全选) 2.重新登录百度帐号刷新cookie后,再复制一次!\n'); | ||
node_1.Terminal.notice('提示:1. 重新复制一次cookie(以BAIDUID开头,注意全选) 2.重新登录百度帐号刷新cookie后,再复制一次!\n'); | ||
console.log(error_1); | ||
return [3 /*break*/, 17]; | ||
case 17: | ||
return [3 /*break*/, 19]; | ||
case 19: | ||
/** 结束信息 **/ | ||
node_1.Terminal.tips('qwq小提示: 建议把cookie记在小本本上以便备用哦'); | ||
node_1.Terminal.notice("qwq: " + id + "\u53F7\u673A\u4EFB\u52A1\u5DF2\u5B8C\u6210\uFF01\u4E3B\u4EBA\u6211\u8FD8\u4F1A\u60F3\u4F60\u7684\uFF01"); | ||
return [2 /*return*/]; | ||
@@ -137,0 +167,0 @@ } |
@@ -21,3 +21,3 @@ import { SearchResult } from './result'; | ||
*/ | ||
search(keyword: string): Promise<SearchResult>; | ||
search(keyword: string, days?: number): Promise<SearchResult>; | ||
} |
@@ -45,3 +45,3 @@ "use strict"; | ||
* @Last Modified by: saber2pr | ||
* @Last Modified time: 2019-06-22 16:07:28 | ||
* @Last Modified time: 2019-06-26 13:49:19 | ||
*/ | ||
@@ -98,18 +98,20 @@ var axios_1 = __importDefault(require("axios")); | ||
*/ | ||
BaiduChart.prototype.search = function (keyword) { | ||
BaiduChart.prototype.search = function (keyword, days) { | ||
if (days === void 0) { days = 365; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, uniqid, _b, word, startDate, endDate, data, ptbk; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, this.request.get("http://index.baidu.com/api/SearchApi/thumbnail" /* thumbnail */, { | ||
var _a, uniqid, _b, word, _c, startDate, endDate, data, ptbk; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: return [4 /*yield*/, this.request.get("http://index.baidu.com/api/SearchApi/index" /* Index */, { | ||
params: { | ||
area: 0, | ||
word: keyword | ||
word: keyword, | ||
days: days | ||
} | ||
})]; | ||
case 1: | ||
_a = (_c.sent()).data.data, uniqid = _a.uniqid, _b = _a.all, word = _b.word, startDate = _b.startDate, endDate = _b.endDate, data = _b.data; | ||
_a = (_d.sent()).data.data, uniqid = _a.uniqid, _b = _a.userIndexes[0], word = _b.word, _c = _b.all, startDate = _c.startDate, endDate = _c.endDate, data = _c.data; | ||
return [4 /*yield*/, this.getPTBK(uniqid)]; | ||
case 2: | ||
ptbk = (_c.sent()).data.data; | ||
ptbk = (_d.sent()).data.data; | ||
return [2 /*return*/, { | ||
@@ -116,0 +118,0 @@ word: word, |
@@ -7,2 +7,2 @@ import { SearchResult } from './result'; | ||
export declare const parseData: (data: string) => string[]; | ||
export declare const searchResultsToCsv: (result: SearchResult[]) => string; | ||
export declare const searchResultsToCsv: (result: SearchResult[], delta?: number) => string; |
@@ -34,3 +34,3 @@ "use strict"; | ||
* @Last Modified by: saber2pr | ||
* @Last Modified time: 2019-06-22 16:05:53 | ||
* @Last Modified time: 2019-06-26 14:57:56 | ||
*/ | ||
@@ -64,3 +64,3 @@ var list_1 = require("@saber2pr/fp/lib/list"); | ||
current.setDate(current.getDate() + step); | ||
return [4 /*yield*/, RegTime.exec(new Date(current).toJSON())[0]]; | ||
return [4 /*yield*/, RegTime.exec(new Date(current).toJSON())[0].replace(/-/g, '/')]; | ||
case 2: | ||
@@ -79,5 +79,6 @@ _a.sent(); | ||
exports.parseData = function (data) { return data.split(','); }; | ||
exports.searchResultsToCsv = function (result) { | ||
exports.searchResultsToCsv = function (result, delta) { | ||
if (delta === void 0) { delta = 7; } | ||
var _a = result[0], startDate = _a.startDate, endDate = _a.endDate; | ||
var ts = timeRange(startDate, endDate, 7); | ||
var ts = timeRange(startDate, endDate, delta); | ||
var dss = result.map(function (res) { return exports.parseData(res.data); }); | ||
@@ -84,0 +85,0 @@ var head = ['时间'].concat(result.map(function (res) { return res.word; })).join(','); |
@@ -9,3 +9,7 @@ export declare const enum Baidu { | ||
*/ | ||
thumbnail = "http://index.baidu.com/api/SearchApi/thumbnail" | ||
thumbnail = "http://index.baidu.com/api/SearchApi/thumbnail", | ||
/** | ||
* ?area=0&word=xxx&days=xxx | ||
*/ | ||
Index = "http://index.baidu.com/api/SearchApi/index" | ||
} |
{ | ||
"name": "@saber2pr/baidu-chart-api", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "> 百度指数数据解析", | ||
@@ -34,5 +34,5 @@ "repository": { | ||
"@saber2pr/fp": "0.0.7", | ||
"@saber2pr/node": "^0.2.5", | ||
"@saber2pr/node": "^0.2.6", | ||
"axios": "^0.19.0" | ||
} | ||
} | ||
} |
@@ -51,3 +51,3 @@ # @saber2pr/baidu-chart-api | ||
- cookie 需要抓包获得。([以`BAIDUID`开头的值就可以了, 示例>>](https://github.com/Saber2pr/baidu-chart-api/blob/master/src/test/test.ts#L10)) | ||
- cookie 需要抓包获得。([包含以`BAIDUID`(也可能是 BIDUPSID)开头的值就可以了, 示例>>](https://github.com/Saber2pr/baidu-chart-api/blob/master/src/test/test.ts#L10)) | ||
@@ -58,8 +58,6 @@ - 如果使用命令行工具,cookie 会缓存在命令执行的目录下,不需要再次输入! | ||
> 以下解释为什么我需要手动复制 cookie | ||
## Bug | ||
> document.cookie 拿到的值不行!虽然大部分 cookie 都是 httponly=false 的,但是也有个别有安全策略。所以不如直接抓包来的快。 | ||
0.0.3 及以下版本在 windows 下会报错,最新版已解决。 | ||
> 其实 cookie 是可以服务端模拟登录拿到的,但是由于百度的身份认证加密过于复杂,up 我只能手动复制 cookie 了 T_T | ||
## 免责声明 | ||
@@ -66,0 +64,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24768
18
465
98
Updated@saber2pr/node@^0.2.6