Skip to content

Commit

Permalink
✨ 支持定时执行
Browse files Browse the repository at this point in the history
  • Loading branch information
zas023 committed Aug 1, 2022
1 parent fb6d996 commit adc379d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
20 changes: 18 additions & 2 deletions JdBuyerApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
import json

from PySide6.QtCore import Qt, QThread, Signal
from PySide6.QtCore import Qt, QThread, Signal, QDateTime
from PySide6.QtGui import QImage, QPixmap
from PySide6.QtWidgets import (
QWidget,
Expand All @@ -12,9 +12,11 @@
QLineEdit,
QSlider,
QPushButton,
QGridLayout
QGridLayout,
QDateTimeEdit
)

from timer import Timer
from JdSession import Session

NUM_LABEL_FORMAT = '商品购买数量[{0}]个'
Expand Down Expand Up @@ -100,6 +102,13 @@ def initUI(self):
grid.addWidget(passwordLabel, 3, 0)
grid.addWidget(self.passwordEdit, 3, 1)

# 开始时间
buyTimeLabel = QLabel('定时开始执行时间')
self.buyTimeEdit = QDateTimeEdit(QDateTime.currentDateTime(), self)
self.buyTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
grid.addWidget(buyTimeLabel, 3, 3)
grid.addWidget(self.buyTimeEdit, 3, 4)

# 二维码
self.qrLabel = QLabel()
grid.addWidget(self.qrLabel, 4, 0, 1, 2)
Expand Down Expand Up @@ -133,6 +142,7 @@ def startTask(self):
self.qrLogin()
self.infoLabel.setText('请使用京东扫码登录')
return
self.config['buyTime'] = self.buyTimeEdit.text()
self.config['skuId'] = self.skuEdit.text()
self.config['areaId'] = self.areaEdit.text()
self.saveData()
Expand Down Expand Up @@ -266,11 +276,17 @@ def run(self):
area_id = self.taskParam.get('areaId')
count = self.taskParam.get('count')
stock_interval = self.taskParam.get('stockInterval')
buyTime = self.taskParam.get('buyTime')

self.session.item_details[sku_id] = self.session._get_item_detail(
sku_id)
submit_retry = 3
submit_interval = 5

timer = Timer(buyTime)
self.info_signal.emit('定时中,将于 {0} 开始执行'.format(buyTime))
timer.start()

while True:
if self._isPause:
self.info_signal.emit('{0} 已取消下单'.format(
Expand Down
20 changes: 20 additions & 0 deletions timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding:utf-8 -*-
import time
from datetime import datetime


class Timer(object):

def __init__(self, buyTime, sleepInterval=0.5):

# '2018-09-28 22:45:50'
self.buy_time = datetime.strptime(buyTime, "%Y-%m-%d %H:%M:%S")
self.sleepInterval = sleepInterval

def start(self):
now_time = datetime.now
while True:
if now_time() >= self.buy_time:
break
else:
time.sleep(self.sleepInterval)

0 comments on commit adc379d

Please sign in to comment.