Skip to content

Commit

Permalink
Feat: a new Api of time server (#20)
Browse files Browse the repository at this point in the history
* Feat: a new Api of time server
* Fix: windows下不实用stdout.write 输出时间
  • Loading branch information
meooxx authored Mar 21, 2021
1 parent f05649e commit f501a9e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
14 changes: 8 additions & 6 deletions jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ async function submitOrderFromShoppingCart(
}
// loopTime 0: 50
// loopTime 1: 100
await sleep(100);
await sleep(loopTime * 50 + 50);
}
if (!isAvailable) {
console.log('访问结算页面彻底失败, 溜了');
Expand Down Expand Up @@ -469,11 +469,13 @@ async function submitOrderProcess(
let printPoints = false;
const id = setInterval(async () => {
const now = Date.now();
printPoints = !printPoints;
process.stdout.write('\r\x1b[K');
process.stdout.write(dayjs(now).format('YYYY-MM-DD HH:mm:ss.SSS'));
if (printPoints) {
process.stdout.write('...');
if (process.platform !== 'win32') {
printPoints = !printPoints;
process.stdout.write('\r\x1b[K');
process.stdout.write(dayjs(now).format('YYYY-MM-DD HH:mm:ss.SSS'));
if (printPoints) {
process.stdout.write('...');
}
}
if (now + m * 60 * 1000 >= date) {
for (let i = 0; i < skuIds.length; i++) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jd-yuyue",
"version": "1.2.1",
"version": "1.2.3",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
30 changes: 14 additions & 16 deletions timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,35 @@ const dayjs = require('./dayjs.min.js');

const getDiffTime = async () => {
const old = Date.now();
const res = await fetch('https://a.jd.com//ajax/queryServerData.html', {
const res = await fetch('https://api.m.jd.com/client.action?functionId=queryMaterialProducts&client=wh5', {
headers: {
// 瞎鸡儿填写一个 UA
'User-Agent': config.userAgent || 'chrome 81.0',
},
});
const now = Date.now();
const json = await res.json();
return Date.now() - (now - old) / 2 - json.serverTime;
return Date.now() - (now - old) / 2 - json.currentTime2;
};

module.exports = async (d, fn, im = false) => {
// Should excute immediately
if (im) return Promise.resolve(fn());
// const diffTime = await Promise.race([
// getDiffTime(),
// getDiffTime(),
// getDiffTime(),
// getDiffTime(),
// ]);
// console.log(
// '多次请求中最快的一次与服务器时间差,',
// 'now - 请求往返/2 - jd服务器时间=',
// diffTime
// );
console.log('jd时间服务器挂壁, 请自行根据以往误差往前调抢购时间');
const diffTime = await Promise.race([
getDiffTime(),
getDiffTime(),
getDiffTime(),
getDiffTime(),
]);
console.log(
'多次请求中最快的一次与服务器时间差,',
'now - 请求往返/2 - jd服务器时间=',
diffTime
);
return new Promise(r => {
console.log('等待时间到达:', dayjs(d).format('YYYY-MM-DD HH:mm:ss.SSS'));
while (true) {
// if (Date.now() - diffTime >= d) {
if (Date.now() >= d) {
if (Date.now() - diffTime >= d) {
r(fn());
break;
}
Expand Down

0 comments on commit f501a9e

Please sign in to comment.