Clash/script/dingdong/dingdong_checkin.js

151 lines
34 KiB
JavaScript
Raw Normal View History

2021-04-09 19:34:05 +08:00
const scriptName = '叮咚买菜';
const getCookieRegex = /^https?:\/\/maicai\.api\.ddxq\.mobi\/point\/home\?api_version/;
const dingDongCookieKey = 'dingdongmaicai_checkin_cookie';
const dingDongBodyKey = 'dingdongmaicai_checkin_body';
const dingDongSyncQinglongKey = 'dingdongmaicai_sync_qinglong';
const $ = MagicJS(scriptName, "INFO");
2021-04-09 19:34:05 +08:00
let currentCookie = "";
let currentBody = "";
function getUserId(cookie) {
return new Promise((resolve, reject) => {
$.http.get({
url: "https://maicai.api.ddxq.mobi/user/info",
headers: {
"Referer": "https://activity.m.ddxq.mobi/",
"Host": "maicai.api.ddxq.mobi",
"Origin": "https://activity.m.ddxq.mobi",
"Cookie": cookie
}
}).then(resp => {
const obj = resp.body;
if (obj.code === 0) {
$.logger.info(`当前登录用户的UserId:${obj.data.id}`);
resolve(obj.data.id);
}
else {
const msg = `获取UserId失败\n${JSON.stringify(resp)}`;
$.logger.warning(msg);
reject(msg);
}
}).catch(err => {
const msg = `获取UserId异常\n${err}`;
$.logger.error(msg);
reject(msg);
})
})
}
function checkIn(cookie, body) {
return new Promise((resolve, reject) => {
$.http.post({
2021-04-09 19:34:05 +08:00
url: 'https://sunquan.api.ddxq.mobi/api/v2/user/signin/',
headers: {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-cn",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
"Cookie": cookie,
"Host": "sunquan.api.ddxq.mobi",
"Origin": "https://activity.m.ddxq.mobi",
"Referer": "https://activity.m.ddxq.mobi/",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 xzone/9.15.1 station_id/5500fe01916edfe0738b4e43"
},
body: body
}).then(resp => {
const obj = resp.body;
if (obj.code === 0) {
let msg = `签到成功,连续签到${obj.data.sign_series}天,获取积分${obj.data.point}`;
if (!!obj.data.ticket_money) {
msg += `,优惠券${obj.data.ticket_money}!`;
} else {
msg += "!";
2021-04-09 19:34:05 +08:00
}
$.logger.info(msg);
resolve(msg);
2021-04-09 19:34:05 +08:00
}
else if (obj.code === 9007) {
const msg = `签到失败Cookie已过期`;
$.logger.warning(`${msg}\n${JSON.stringify(obj)}`);
reject(msg);
}
}).catch(err => {
const msg = `签到出现异常\n${err}`;
$.logger.error(msg);
reject(msg);
2021-04-09 19:34:05 +08:00
})
})
}
; (async () => {
if ($.isRequest && getCookieRegex.test($.request.url)) {
const cookie = $.request.headers.Cookie;
const body = $.request.url.split('?')[1];
// 获取UserId
const userId = await $.utils.retry(getUserId, 3, 500)(cookie).catch(err => {
$.notification.post(err);
$.done();
})
let hisCookie = $.data.read(dingDongCookieKey, "", userId);
if (cookie !== hisCookie) {
$.data.write(dingDongCookieKey, cookie, userId);
$.data.write(dingDongBodyKey, body, userId);
$.logger.info(`旧的Cookie${hisCookie}\n新的Cookie${cookie}\nCookie不同写入新的Cookie成功`);
$.notification.post("🎈Cookie写入成功");
2021-04-09 19:34:05 +08:00
}
else {
$.logger.info("Cookie没有变化无需更新");
2021-04-09 19:34:05 +08:00
}
// 同步Cookies至青龙面板
if ($.data.read(dingDongSyncQinglongKey, false) === true) {
hisCookie = await $.qinglong.read(dingDongCookieKey, "", userId);
if (cookie !== hisCookie) {
await $.qinglong.write(dingDongCookieKey, cookie, userId);
await $.qinglong.write(dingDongBodyKey, body, userId);
$.logger.info(`旧的Cookie${hisCookie}\n新的Cookie${cookie}\nCookie不同写入新的Cookie成功`);
$.notification.post("🎈Cookie同步到青龙面板成功");
2021-04-09 19:34:05 +08:00
}
}
}
else {
const allSessions = $.data.allSessions(dingDongCookieKey);
if (!allSessions || allSessions.length <= 0) {
const msg = "没有需要执行的Cookies请先打开APP获取";
$.logger.warning(msg);
$.notification.post(msg);
} else {
$.logger.info(`当前共 ${allSessions.length} 个Cookies需要执行`);
for (let [index, session] of allSessions.entries()) {
$.logger.info(`开始执行第 ${index + 1} 个Cookies的作业`);
currentCookie = $.data.read(dingDongCookieKey, "", session);
currentBody = $.data.read(dingDongBodyKey, "", session);
await $.utils.retry(checkIn, 3, 1000)(currentCookie, currentBody).then(msg => {
$.notification.post(msg);
}).catch(err => {
$.notification.post(err);
})
$.logger.info(`${index + 1} 个Cookies的作业执行完毕`);
2021-04-09 19:34:05 +08:00
}
}
}
$.done();
2021-04-09 19:34:05 +08:00
})();
/**
*
* $$\ $$\ $$\ $$$$$\ $$$$$$\ $$$$$$\
* $$$\ $$$ | \__| \__$$ |$$ __$$\ $$ ___$$\
* $$$$\ $$$$ | $$$$$$\ $$$$$$\ $$\ $$$$$$$\ $$ |$$ / \__| \_/ $$ |
* $$\$$\$$ $$ | \____$$\ $$ __$$\ $$ |$$ _____| $$ |\$$$$$$\ $$$$$ /
* $$ \$$$ $$ | $$$$$$$ |$$ / $$ |$$ |$$ / $$\ $$ | \____$$\ \___$$\
* $$ |\$ /$$ |$$ __$$ |$$ | $$ |$$ |$$ | $$ | $$ |$$\ $$ | $$\ $$ |
* $$ | \_/ $$ |\$$$$$$$ |\$$$$$$$ |$$ |\$$$$$$$\\$$$$$$ |\$$$$$$ | \$$$$$$ |
* \__| \__| \_______| \____$$ |\__| \_______|\______/ \______/ \______/
* $$\ $$ |
* \$$$$$$ |
* \______/
*
*/
function MagicJS(e = "MagicJS", t = "INFO") { const r = () => { const e = typeof $loon !== "undefined"; const t = typeof $task !== "undefined"; const n = typeof module !== "undefined"; const r = typeof $httpClient !== "undefined" && !e; const i = typeof $storm !== "undefined"; const o = typeof $environment !== "undefined" && typeof $environment["stash-build"] !== "undefined"; const s = r || e || i || o; const a = typeof importModule !== "undefined"; return { isLoon: e, isQuanX: t, isNode: n, isSurge: r, isStorm: i, isStash: o, isSurgeLike: s, isScriptable: a, get name() { if (e) { return "Loon" } else if (t) { return "QuantumultX" } else if (n) { return "NodeJS" } else if (r) { return "Surge" } else if (a) { return "Scriptable" } else { return "unknown" } }, get build() { if (r) { return $environment["surge-build"] } else if (o) { return $environment["stash-build"] } else if (i) { return $storm.buildVersion } }, get language() { if (r || o) { return $environment["language"] } }, get version() { if (r) { return $environment["surge-version"] } else if (o) { return $environment["stash-version"] } else if (i) { return $storm.appVersion } else if (n) { return process.version } }, get system() { if (r) { return $environment["system"] } else if (n) { return process.platform } }, get systemVersion() { if (i) { return $storm.systemVersion } }, get deviceName() { if (i) { return $storm.deviceName } } } }; const i = (n, e = "INFO") => { let r = e; const i = { SNIFFER: 6, DEBUG: 5, INFO: 4, NOTIFY: 3, WARNING: 2, ERROR: 1, CRITICAL: 0, NONE: -1 }; const o = { SNIFFER: "", DEBUG: "", INFO: "", NOTIFY: "", WARNING: "❗ ", ERROR: "❌ ", CRITICAL: "❌ ", NONE: "" }; const t = (e, t = "INFO") => { if (!(i[r] < i[t.toUpperCase()])) console.log(`[${t}] [${n}]\n${o[t.toUpperCase()]}${e}\n`) }; const s = e => { r = e }; return { setLevel: s, sniffer: e => { t(e, "SNIFFER") }, debug: e => { t(e, "DEBUG") }, info: e => { t(e, "INFO") }, notify: e => { t(e, "NOTIFY") }, warning: e => { t(e, "WARNING") }, error: e => { t(e, "ERROR") }, retry: e => { t(e, "RETRY") } } }; return new class { constructor(e, t) { this._startTime = Date.now(); this.version = "3.0.0"; this.scriptName = e; this.env = r(); this.logger = i(e, t); this.http = typeof MagicHttp === "function" ? MagicHttp(this.env, this.logger) : undefined; this.data = typeof MagicData === "function" ? MagicData(this.env, this.logger) : undefined; this.notification = typeof MagicNotification === "function" ? MagicNotification(this.scriptName, this.env, this.logger, this.http) : undefined; this.utils = typeof MagicUtils === "function" ? MagicUtils(this.env, this.logger) : undefined; this.qinglong = typeof MagicQingLong === "function" ? MagicQingLong(this.env, this.data, this.logger) : undefined; if (typeof this.data !== "undefined") { let e = this.data.read("magic_loglevel"); const n = this.data.read("magic_bark_url"); if (e) { this.logger.setLevel(e.toUpperCase()) } if (n) { this.notification.setBark(n) } } } get isRequest() { return typeof $request !== "undefined" && typeof $response === "undefined" } get isResponse() { return typeof $response !== "undefined" } get isDebug() { return this.logger.level === "DEBUG" } get request() { return typeof $request !== "undefined" ? $request : undefined } get response() { if (typeof $response !== "undefined") { if ($response.hasOwnProperty("status")) $response["statusCode"] = $response["status"]; if ($response.hasOwnProperty("statusCode")) $response["status"] = $response["statusCode"]; return $response } else { return undefined } } done = (e = {}) => { this._endTime = Date.now(); let t = (this._endTime - this._startTime) / 1e3; this.logger.info(`SCRIPT COMPLETED: ${t} S.`); if (typeof $done !== "undefined") { $done(e) } } }(e, t) } function MagicHttp(u, c) { const t = "Mozilla/5.0 (iPhone; CPU iPhone OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.5 Mobile/15E148 Safari/604.1"; const n = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36 Edg/84.0.522.59"