-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
69 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
STED_SIZE_RANGE=8000-15000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,6 @@ | |
}, | ||
"dependencies": { | ||
"axios": "^0.21.1", | ||
"moment": "^2.29.1" | ||
"dayjs": "^1.10.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |