Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@saber2pr/baidu-chart-api

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@saber2pr/baidu-chart-api - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

lib/core/baidu-chart.d.ts

73

lib/app.js

@@ -42,11 +42,14 @@ #!/usr/bin/env node

* @Date: 2019-06-22 10:46:31
* @Last Modified by: saber2pr
* @Last Modified time: 2019-06-22 10:46:31
* @Last Modified by: saber2pr
* @Last Modified time: 2019-06-22 16:41:18
*/
var node_1 = require("@saber2pr/node");
var core_1 = require("./core");
var path_1 = require("path");
var arg = node_1.Terminal.getParams()[0];
/** cookie 缓存目录 **/
var cookie_temp = path_1.join(process.cwd(), 'cookie_baidu.txt');
function App() {
return __awaiter(this, void 0, void 0, function () {
var pkgInfor, cookie, inputs, baiduAPI, keywords, results, error_1;
var pkgInfor, cookie, isLogined, inputs, baiduAPI, keywords, results, file_path, error_1;
return __generator(this, function (_a) {

@@ -62,16 +65,35 @@ switch (_a.label) {

}
return [4 /*yield*/, node_1.FS.exists(cookie_temp)];
case 2:
isLogined = _a.sent();
if (!isLogined) return [3 /*break*/, 4];
return [4 /*yield*/, node_1.FS.readFile(cookie_temp)];
case 3:
cookie = (_a.sent()).toString();
return [3 /*break*/, 6];
case 4:
node_1.Terminal.tips('qwq:输入你的认证信息?\n');
return [4 /*yield*/, node_1.Terminal.getUserInput('百度帐号cookie:\n')];
case 2:
case 5:
cookie = _a.sent();
if (cookie) {
node_1.Terminal.success('qwq:认证信息已完整(ok).\n');
}
else {
node_1.Terminal.error('qwq:出错了呢QAQ!请再试一次?\n');
return [2 /*return*/];
}
_a.label = 6;
case 6:
if (!cookie) return [3 /*break*/, 8];
node_1.Terminal.success('qwq:认证信息已完整(ok).\n');
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);
return [3 /*break*/, 10];
case 8:
node_1.Terminal.error('qwq:出错了呢QAQ!请再试一次?\n');
return [4 /*yield*/, node_1.FS.remove(cookie_temp)];
case 9:
_a.sent();
return [2 /*return*/];
case 10:
/** 表单 **/
node_1.Terminal.tips('qwq:输入你要搜的关键词?\n');
return [4 /*yield*/, node_1.Terminal.getUserInput('关键词(可以多个,逗号分隔):')];
case 3:
case 11:
inputs = _a.sent();

@@ -84,18 +106,27 @@ baiduAPI = new core_1.BaiduChart(cookie);

node_1.Terminal.tips('\nqwq:正在努力搜索中...\n');
_a.label = 4;
case 4:
_a.trys.push([4, 6, , 7]);
_a.label = 12;
case 12:
_a.trys.push([12, 15, , 17]);
return [4 /*yield*/, Promise.all(keywords.map(function (keyword) { return baiduAPI.search(keyword); }))];
case 5:
case 13:
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");
return [3 /*break*/, 7];
case 6:
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:
_a.sent();
node_1.Terminal.tips("\nqwq:Excel\u6587\u4EF6\u751F\u6210\u6210\u529F\uFF01>> " + file_path);
return [3 /*break*/, 17];
case 15:
error_1 = _a.sent();
node_1.Terminal.error('搜索失败!可能是cookie不正确或失效!\n');
node_1.Terminal.tips('提示:1. 重新赋值一次cookie(以BAIDUID开头) 2.重新登录百度帐号刷新cookie后,再复制一次!\n');
return [4 /*yield*/, node_1.FS.remove(cookie_temp)];
case 16:
_a.sent();
node_1.Terminal.tips('提示:1. 重新复制一次cookie(以BAIDUID开头,注意全选) 2.重新登录百度帐号刷新cookie后,再复制一次!\n');
console.log(error_1);
return [3 /*break*/, 7];
case 7:
return [3 /*break*/, 17];
case 17:
/** 结束信息 **/
node_1.Terminal.tips('qwq小提示: 建议把cookie记在小本本上以便备用哦');

@@ -102,0 +133,0 @@ return [2 /*return*/];

@@ -1,26 +0,4 @@

export declare class BaiduChart {
private readonly Baidu_Cookie;
/**
* # 百度指数数据解析
* * 需要百度帐号Cookie信息
* @param {string} Baidu_Cookie
* @memberof BaiduChart
*/
constructor(Baidu_Cookie: string);
private request;
private getPTBK;
private initInterceptors;
/**
* # 输入搜索关键词
*
* @param {string} keyword
* @returns
* @memberof BaiduChart
*/
search(keyword: string): Promise<{
word: string;
startDate: string;
endDate: string;
data: string;
}>;
}
export * from './baidu-chart';
export * from './parser';
export * from './result';
export * from './url';
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
/*
* @Author: saber2pr
* @Date: 2019-06-21 21:07:23
* @Last Modified by: saber2pr
* @Last Modified time: 2019-06-22 09:58:46
*/
var axios_1 = __importDefault(require("axios"));
var utils_1 = require("./utils");
var BaiduChart = /** @class */ (function () {
/**
* # 百度指数数据解析
* * 需要百度帐号Cookie信息
* @param {string} Baidu_Cookie
* @memberof BaiduChart
*/
function BaiduChart(Baidu_Cookie) {
this.Baidu_Cookie = Baidu_Cookie;
this.request = axios_1.default.create({
headers: {
Cookie: Baidu_Cookie
}
});
this.initInterceptors();
}
BaiduChart.prototype.getPTBK = function (uniqid) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.request.get("http://index.baidu.com/Interface/ptbk" /* PTBK */, {
params: {
uniqid: uniqid
}
})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
BaiduChart.prototype.initInterceptors = function () {
this.request.interceptors.response.use(function (res) {
if (res.status !== 200) {
return Promise.reject({
message: '请求失败!',
data: res.data
});
}
return res;
});
};
/**
* # 输入搜索关键词
*
* @param {string} keyword
* @returns
* @memberof BaiduChart
*/
BaiduChart.prototype.search = function (keyword) {
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 */, {
params: {
area: 0,
word: keyword
}
})];
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;
return [4 /*yield*/, this.getPTBK(uniqid)];
case 2:
ptbk = (_c.sent()).data.data;
return [2 /*return*/, {
word: word,
startDate: startDate,
endDate: endDate,
data: utils_1.transformDataFromPtbk(ptbk, data)
}];
}
});
});
};
return BaiduChart;
}());
exports.BaiduChart = BaiduChart;
__export(require("./baidu-chart"));
__export(require("./parser"));
__export(require("./url"));
{
"name": "@saber2pr/baidu-chart-api",
"version": "0.0.2",
"version": "0.0.3",
"description": "> 百度指数数据解析",

@@ -33,6 +33,6 @@ "repository": {

"dependencies": {
"@saber2pr/fp": "0.0.5",
"@saber2pr/fp": "0.0.7",
"@saber2pr/node": "^0.2.4",
"axios": "^0.19.0"
}
}
}
# @saber2pr/baidu-chart-api
[![npm](https://img.shields.io/npm/v/@saber2pr/baidu-chart-api.svg?color=%23f253d4)](https://www.npmjs.com/package/@saber2pr/baidu-chart-api)
> 百度指数数据解析

@@ -13,2 +15,4 @@

windows 下执行
```bash

@@ -18,5 +22,13 @@ npm install @saber2pr/baidu-chart-api -g

3. 命令行执行
mac 或 linux 下执行(加 sudo)
```bash
sudo npm install @saber2pr/baidu-chart-api -g
```
### 命令安装好 app 后
命令行执行
```bash
baiduChart

@@ -29,3 +41,9 @@ ```

```bash
npm i @saber2pr/baidu-chart-api
```
```ts
import { BaiduChart } from '@saber2pr/baidu-chart-api'
const cookie = '你的百度帐号cookie'

@@ -38,2 +56,8 @@

- 如果使用命令行工具,cookie 会缓存在命令执行的目录下,不需要再次输入!
- 自动生成 excel csv。(0.0.3 版本及以上支持, 版本号查询请执行 `baiduChart -v`)
> 以下解释为什么我需要手动复制 cookie
> document.cookie 拿到的值不行!虽然大部分 cookie 都是 httponly=false 的,但是也有个别有安全策略。所以不如直接抓包来的快。

@@ -40,0 +64,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc