Skip to content

Commit

Permalink
update: 优化日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
Devifish committed Mar 23, 2021
1 parent 5daf5ba commit ba69efb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 47 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
STED_SIZE_RANGE=8000-15000
45 changes: 10 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,14 @@
import { loginByPassword, getAccessToken, pushBandData } from "./src/api.js";
import { isEmpty } from "./src/common.js";
import { run } from "./src/main.js";

const DEFAULT_STEP_SIZE = "5000-15000";

async function start() {
const config = {
username: process.env.XIAOMI_AMAZFIT_USERNAME,
password: process.env.XIAOMI_AMAZFIT_PASSWORD,
user_id: process.env.XIAOMI_AMAZFIT_USER_ID,
app_token: process.env.XIAOMI_AMAZFIT_APP_TOKEN,
step_size: process.env.STED_SIZE_RANGE ?? DEFAULT_STEP_SIZE,
};
// 获取环境变量
const config = {
username: process.env.XIAOMI_AMAZFIT_USERNAME,
password: process.env.XIAOMI_AMAZFIT_PASSWORD,
user_id: process.env.XIAOMI_AMAZFIT_USER_ID,
app_token: process.env.XIAOMI_AMAZFIT_APP_TOKEN,
step_size: process.env.STED_SIZE_RANGE ?? DEFAULT_STEP_SIZE,
};

if (isEmpty(config.app_token) || isEmpty(config.user_id)) {
console.warn("未获取到APP_TOKEN或USER_ID 将使用账号密码方式运行");
const code = await loginByPassword(config.username, config.password);
const token_info = await getAccessToken(code);

config.app_token = token_info.app_token;
config.user_id = token_info.user_id;
}

const step = getRamdomStep(config.step_size);
console.log(`在 [${config.step_size}] 范围内随机步数 step: ${step}`);

await pushBandData(step, config.user_id, config.app_token);
}

function getRamdomStep(step_size = DEFAULT_STEP_SIZE) {
const temp = step_size.split("-");
if (temp.length !== 2) return getRamdomStep();

const min = new Number(temp[0]);
const max = new Number(temp[1]);
return parseInt(min + Math.random() * (max - min));
}

await start();
await run(config);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
},
"dependencies": {
"axios": "^0.21.1",
"moment": "^2.29.1"
"dayjs": "^1.10.4"
}
}
23 changes: 12 additions & 11 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Axios from "axios";
import Moment from "moment";
import { toUrlEncode } from "./common.js";
import dayjs from "dayjs";
import fs from "fs/promises";
import { toUrlEncode } from "./common.js";
import * as log from "./log.js";

// 公共头
const COMMON_HEADERS = {
Expand Down Expand Up @@ -33,19 +34,19 @@ export async function loginByPassword(username, password) {
`https://api-user.huami.com/registrations/+86${username}/tokens`,
data
);
console.log("登录成功, 开始获取登录授权码");
log.info("登录成功, 开始获取登录授权码");

// 获取Code
const path = new URL(res.request.path, redirect_uri);
const params = path.searchParams;
if (params.has("access")) {
const code = params.get("access");
console.log(`获取登录授权码成功 code: ${code}`);
log.info(`获取登录授权码成功 code: ${code}`);
return code;
}
throw new Error("获取登录授权码失败");
} catch (e) {
console.error("登录失败, 请检查账号密码");
log.error("登录失败, 请检查账号密码");
throw e;
}
}
Expand Down Expand Up @@ -95,10 +96,10 @@ export async function getAccessToken(code) {
const res = await axios.post("https://account.huami.com/v2/client/login", data);

const token_info = res.data.token_info;
console.log(`获取AccessToken成功 token: ${token_info.login_token}`);
log.info(`获取AccessToken成功 token: ${token_info.login_token}`);
return token_info;
} catch (e) {
console.error("获取AccessToken失败");
log.error("获取AccessToken失败");
throw e;
}
}
Expand All @@ -123,20 +124,20 @@ export async function pushBandData(step, user_id, app_token) {
}
);

console.log(`修改步数${step}成功`);
log.info(`上传步数成功 step:${step}`);
} catch (e) {
console.error("修改步数失败");
log.error("上传步数失败");
throw e;
}
}

async function buildDataJson(step) {
const time = Moment().format("YYYY-MM-DD");
const time = dayjs().format("YYYY-MM-DD");
const find_date = /.*?date":"(.*?)","data.*?/;
const find_step = /.*?ttl\\":(.*?),\\"dis.*?/;

let data_json = await fs.readFile("./data.json", "utf-8");
data_json = data_json.replace(find_date.exec(data_json)[1], time);
data_json = data_json.replace(find_step.exec(data_json)[1], step);
return data_json;
}
}
15 changes: 15 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import dayjs from "dayjs";

const LogType = {
INFO: "info",
WARN: "warn",
ERROR: "error"
};

function log(msg, type = LogType.INFO) {
console.log(`[${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}] [${type}] ${msg}`);
}

export const info = (msg) => log(msg, LogType.INFO);
export const warn = (msg) => log(msg, LogType.WARN);
export const error = (msg) => log(msg, LogType.ERROR);
30 changes: 30 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { loginByPassword, getAccessToken, pushBandData } from "./api.js";
import { isEmpty } from "./common.js";
import * as log from "./log.js";

export async function run(config) {
if (isEmpty(config.app_token) || isEmpty(config.user_id)) {
log.warn("未获取到APP_TOKEN或USER_ID 将使用账号密码方式运行");
const code = await loginByPassword(config.username, config.password);
const { app_token, user_id } = await getAccessToken(code);

config.app_token = app_token;
config.user_id = user_id;
}

const step = getRamdomStep(config.step_size);
await pushBandData(step, config.user_id, config.app_token);
}

function getRamdomStep(step_size = DEFAULT_STEP_SIZE) {
if (!step_size.includes("-")) throw new Error("步数范围格式异常");

const temp = step_size.split("-");
if (temp.length !== 2) return getRamdomStep();

const min = new Number(temp[0]);
const max = new Number(temp[1]);
const step = parseInt(min + Math.random() * (max - min));
log.info(`在 [${min}${max}] 范围内随机步数 step: ${step}`);
return step;
}

0 comments on commit ba69efb

Please sign in to comment.