def refresh(self): # appleid = self._getCurrentAppleId() taskstatus = self._getCurrentTaskStatus() if not taskstatus: return appleid = taskstatus['appleId'] taskstatus['verifyCodeData'] = None if taskstatus: taskstatus['taskCmd'] = 'refresh' time.sleep(2) data = taskstatus['verifyCodeData'] counter = 3 while not data and counter > 0: time.sleep(1) data = taskstatus['verifyCodeData'] counter -= 1 if data: self.fillVerifyCodePic(data) taskname = self.appContext.getCurrentTaskName() self.reserverResult.update(taskname, appleid, 'verifyCodeData', data) taskstatus['verifyCodeData'] = None else: debug.info('refresh failed') taskstatus['taskCmd'] = None
def submit(self): taskStatus = self._getCurrentTaskStatus() if taskStatus: captcha = self.lECaptchaAnswer.text() taskStatus['captchaAnswer'] = str(captcha) phoneNumber = self.lEPhoneNumber.text() taskStatus['phoneNumber'] = str(phoneNumber) taskStatus['smsCode'] = str(self.lESmsCode.text()) taskStatus['clientTimezone'] = 'Asia/Shanghai' taskStatus['countryISDCode'] = '86' taskStatus['cmdStatus'] = None taskStatus['taskCmd'] = 'submit' timer = 30 while not taskStatus['cmdStatus'] and timer > 0: time.sleep(1) #debug.debug('check cmdStatus') timer -= 1 print('end check cmdStatus') if taskStatus['cmdStatus'] == 'NOK': debug.info('submit error') debug.info(taskStatus['prompInfo']) self.refresh() elif taskStatus['cmdStatus'] == 'OK': result = taskStatus['timeSlots'] self.fillTableWidget(result[0], result[1]) self.showTimeSlots(1) else: debug.error('submit task error') else: debug.error('submit error')
def fillResultView(self, appleId): currentTaskName = self.appContext.getCurrentTaskName() results = self.reserverResult.getData(currentTaskName) if not results: debug.info('Can not find %s' % currentTaskName) return findResult = None for result in results: if appleId in result.values(): findResult = result if not findResult: debug.error('can not found verifyData %s,%s' % (appleId, currentTaskName)) return verifyData = findResult['verifyCodeData'] if verifyData: self.fillVerifyCodePic(verifyData) else: debug.debug('can not get verfiy pic') smsMsg = findResult['smsMsg'] if smsMsg: self.pTSmsChallengeTip.setPlainText(smsMsg) else: debug.error('Can not found smsMsg %s %s' % (appleId, currentTaskName))
def get_tag_value(self, tagLabel, attrs): soup = self.get_soup() try: tag = soup.find(tagLabel, attrs=attrs) return tag.get('value') except AttributeError as e: debug.info('info cannot find %s %s' % (tagLabel, str(e))) return ''
def onTaskCompleted(self, taskname): debug.info('task %s finished' % taskname) count = self.ui.lw_processing_tasks.rowCount() for i in range(count): item = self.ui.lw_processing_tasks.item(i, 0) if item.text() == taskname: self.ui.lw_processing_tasks.removeRow(i) self.appendFinished(taskname) break
def taskMonitor(self): debug.info('start task monitor') task = self.new_tasks.get() while task is not None: debug.info('received task %s' % task.task_name) self.startTask(task, self.running_tasks_status) task = self.new_tasks.get() self.running_tasks.put((None, 0))
def onTaskCompleted(self, taskname): debug.info('task %s finished' % taskname) model = self.ui.lw_processing_tasks.model() for i in range(self.ui.lw_processing_tasks.count()): item = self.ui.lw_processing_tasks.item(i) if item.text() == taskname: model.removeRow(i) self.ui.lw_finished_tasks.addItem(taskname) break
def finished(self): verifyCodeData = self.taskStatus['verifyCodeData'] if verifyCodeData: image = QtGui.QImage.fromData(verifyCodeData) pixmap = QtGui.QPixmap(image) size = QtCore.QSize(pixmap.width(), pixmap.height()) self.ui.lbVerifyCodePic.resize(size) self.ui.lbVerifyCodePic.setPixmap(pixmap) else: debug.info('verifyData is none')
def taskMonitor(self): debug.info('start task monitor') while True: # TaskManager.newtask_cond.acquire() task = self.new_tasks.get() if task is None: self.running_tasks.put((None, 0)) break debug.info('received task %s' % task.task_name) self.startTask(task, self.running_tasks_status)
def get_value(soup, idname, tagname='input', attrs=None): tmpattrs = None if attrs: tmpattrs = attrs else: tmpattrs = {'id': idname} try: result = soup.find(tagname, attrs=tmpattrs) return result.get('value') except AttributeError as e: debug.info('tagname %s %s' % (tagname, str(e)))
def get_value(soup,idname, tagname='input', attrs=None): tmpattrs = None if attrs: tmpattrs = attrs else: tmpattrs = {'id': idname} try: result = soup.find(tagname, attrs=tmpattrs) return result.get('value') except AttributeError as e: debug.info('tagname %s %s' % (tagname, str(e)))
def guiMonitor(self): debug.info("start gui monitor") task = self.taskManager.popFinisedTask() while task is not None: self.taskManager.resetDb('tasks.db') self.signalTaskCompleted.emit(task.task_name) task.task_status = '1' task.task_progress = '100' task.save() del self.taskManager.running_tasks_status[task.task_name] task = self.taskManager.popFinisedTask()
def guiMonitor(self): debug.info('start gui monitor') while True: try: task = self.taskManager.popFinisedTask() if task is None: break self.taskManager.resetDb('tasks.db') self.signalTaskCompleted.emit(task.task_name) task.save() except: pass time.sleep(3)
def updateTaskProgress(self): debug.info('start update task progress') while True and not self.stop: tasks_status = self.taskManager.running_tasks_status for taskName, progress in tasks_status.items(): tasks = self.ui.lw_processing_tasks count = tasks.count() for index in range(count): item = tasks.item(index) name = item.text() if name == taskName: debug.info('%s %s' % (taskName, progress) + '%') time.sleep(1)
def getTaskResult(self, appleId): currentTaskName = self.appContext.getCurrentTaskName() results = self.reserverResult.getData(currentTaskName) if not results: debug.info('Can not find %s' % currentTaskName) return findResult = None for result in results: if appleId in result.values(): findResult = result if not findResult: debug.error('can not found verifyData %s,%s' % (appleId, currentTaskName)) return return findResult
def runningTaskMonitor(self): debug.info('start running monitor') while True: task, pid = self.running_tasks.get() if task is None: self.completed_tasks.put(None) break try: psutil.Process(pid) except psutil.NoSuchProcess: task.task_status = '1' self.completed_tasks.put(task) else: self.running_tasks.put((task, pid)) time.sleep(5)
def updateTaskProgress(self): debug.info('start update task progress') while True and not self.stop: tasks_status = self.taskManager.running_tasks_status try: for taskName, progress in tasks_status.items(): tasks = self.ui.lw_processing_tasks count = tasks.rowCount() for index in range(count): item = tasks.item(index, 0) name = item.text() if name == taskName: self.signalUpdateTaskProgress.emit(index, 1, progress) finally: pass time.sleep(1)
def checking(self, statusTasks, finished): while statusTasks: for index, statusTask in enumerate(statusTasks): progress = str(statusTask['taskProgress']) appleId = statusTask['appleId'] debug.debug('checking %s %s' % (appleId, progress)) if progress == '100': for status in finished: if status['appleId'] == appleId: status = dict(status, **statusTasks[index]) time.sleep(1) self.signalStoreResult.emit(appleId) time.sleep(2) del statusTasks[index] self.signalUpdateProgress.emit(appleId, progress) time.sleep(3) debug.info('Terminal check %s' % self.appContext.getCurrentTaskName()) self.enableStart()
def onNewTaskClicked(self): '''Create a new task''' taskName = self.ui.le_task_name.text() siteName = self.ui.lb_current_site.text() keyWords = self.ui.le_search_keywords.text() if not (taskName and siteName and keyWords): return if not self.hasSameTask(taskName): debug.info('Change task name please') return newTask = Task(task_name=taskName, task_site_name=siteName, task_search_words=keyWords, task_status=0) self.ui.lw_processing_tasks.addItem(taskName) self.taskManager.addTask(newTask) msginfo = 'Task %s is running' % taskName debug.info(msginfo)
def _read_page(self): self.init_cookie() if self.post_data: debug.debug('post %s' % self.url) else: debug.debug('read %s' % self.url) req = urllib2.Request(self.url, data=self.post_data, headers=self.headers) counter = 3 while counter > 0: try: res = urllib2.urlopen(req, data=None, timeout=self.timeout) data = res.read() return data except Exception as e: debug.info(str(e)) counter -= 1 debug.error('Read failed') return None
def submit(self): taskStatus = self._getCurrentTaskStatus() if taskStatus: captcha = self.lECaptchaAnswer.text() taskStatus['captchaAnswer'] = str(captcha) phoneNumber = self.lEPhoneNumber.text() taskStatus['phoneNumber'] = str(phoneNumber) taskStatus['smsCode'] = str(self.lESmsCode.text()) taskStatus['clientTimezone'] = 'Asia/Shanghai' taskStatus['countryISDCode'] = '86' taskStatus['taskCmd'] = 'submit' while not taskStatus['cmdStatus']: time.sleep(1) if taskStatus['cmdStatus'] == 'NOK': debug.info('submit error') self.refresh() if taskStatus['cmdStatus'] == 'Ok': result = taskStatus['cmdResult'] self.fillTableWidget(result[0], result[1]) self.viewDetail(1) else: debug.error('submit error')
def startTask(self): self.storeIndex = self.ui.combStore.currentIndex() self.storeName = self.ui.combStore.currentText() self.reservIndex = self.ui.combServType.currentIndex() self.reservType = self.ui.combServType.currentText() self.loginData['appleId'] = str(self.ui.lEUserName.text()) self.loginData['accountPassword'] = str(self.ui.lEPasswd.text()) self.loginData['phoneNumber'] = str(self.ui.lEPhoneNumber.text()) self.loginData['governmentID'] = str(self.ui.lEGovId.text()) self.loginData['governmentIDType'] = 'CN.PRCID' self.loginData['reservType'] = str(self.reservType) self.loginData['reservCode'] = str(self.ui.lEReservCode.text()) self.loginData['storeName'] = str(self.storeName) isOk = True for key, val in self.loginData.items(): if not val: debug.info('Please input %s' % key) isOk = False break if isOk: self.accept()
def contextMenuEvent(self, event): curItem = None indexTab = self.ui.tbwg_menubar.currentIndex() # finished tasks if 2 == indexTab: curItem = self.ui.lw_finished_tasks.currentItem() if not curItem: debug.error('There is no select items %s' % curItem) return popMenu = QtWidgets.QMenu() popMenu.addAction(self.actionExportToExcel) action = popMenu.exec(self.cursor().pos()) if not action: return if action == self.actionExportToExcel: self.onExportToExcel(curItem.text()) else: debug.info('action error') if 1 == indexTab: pass
def closeEvent(self, event): debug.info('received close evnet') self.taskManager.addTask(None) self.stop = True event.accept()
def waitingCmd(self, page, taskStatus): ''' waiting the input ''' runtime = 300 # waiting time storeUrl = taskStatus['storeUrl'] debug.debug(storeUrl) while runtime > 0: taskCmd = taskStatus['taskCmd'] if taskCmd == 'refresh': debug.debug('refresh cmd %s' % taskStatus['appleId']) verifycodedata, tSt = page.get_verification_code_pic() taskStatus['verifyCodeData'] = verifycodedata taskStatus['taskCmd'] = None time.sleep(1) continue if taskCmd == 'submit': debug.debug('get submit cmd %s' % taskStatus['appleId']) postData = page.build_submit_post_data() postData['captchaAnswer'] = taskStatus['captchaAnswer'] postData['phoneNumber'] = taskStatus['phoneNumber'] postData['smsCode'] = taskStatus['smsCode'] postData['clientTimezone'] = taskStatus['clientTimezone'] # 'Asia/Shanghai' submitUrl = self.challengeUrlFormat % GeniusbarPage.storeNumber headers = page.headers headers['Referer'] = submitUrl headers.pop('Accept-Encoding') submitpage = GeniusbarPage(submitUrl, data=urllib.urlencode(postData), headers=headers) data = submitpage.get_data() resultfile = 'tmp/%s.htm' % taskStatus['appleId'] Writefile(resultfile, data) attrs = {"class": "error-message on", "id": "error_message_generalError"} errorMsg = submitpage.get_tag_text('label', attrs=attrs) if errorMsg: taskStatus['cmdStatus'] = 'NOK' taskStatus['taskCmd'] = None taskStatus['prompInfo'] = errorMsg page = submitpage debug.error(errorMsg) Writefile('tmp/submiterr.html', page.get_data()) continue else: # success for submit # get the time slots ret, maxrow = self.buildTimeSlotsTable(submitpage) taskStatus['cmdStatus'] = 'OK' taskStatus['timeSlots'] = (ret, maxrow) taskStatus['taskCmd'] = None page = submitpage continue if taskCmd == 'timeslot': # post timeslots debug.debug('get timeslot cmd %s' % taskStatus['appleId']) postData = self.buildPostTimeSlotsData(page) postData['clientTimezone'] = taskStatus['clientTimezone'] postData['id'] = taskStatus['id'] tlsUrl = self.timeslotFormat % GeniusbarPage.storeNumber debug.debug('tls ulr %s' % tlsUrl) headers = {} headers['Accept-Language'] = 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3' headers['User-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0" headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' headers["Connection"] = 'keep-alive' headers["Host"] = 'concierge.apple.com' tlspage = GeniusbarPage(tlsUrl, data=urllib.urlencode(postData), headers=headers) data = tlspage.get_data() Writefile('tmp/posttimeslotsresult.html', data) text = self.getConfirmMsg(tlspage) taskStatus['prompInfo'] = text.replace(' ', '') Writefile('tmp/reserv-%s' % taskStatus['appleId'], taskStatus['prompInfo']) taskStatus['taskCmd'] = None taskStatus['cmdStatus'] = 'OK' break if taskCmd == 'end': taskStatus['taskCmd'] = None break #debug.debug('waiting cmd') time.sleep(1) runtime -= 1 debug.info('End task %s' % taskStatus['appleId'])
def onExportToExcel(self, taskName): debug.info('%s on export to excel' % taskName)
def on_actionExportToExcel_triggered(self): debug.info('actionExportToExcel')
def waitingCmd(self, page, taskStatus): ''' waiting the input ''' runtime = 300 # waiting time storeUrl = taskStatus['storeUrl'] debug.debug(storeUrl) while runtime > 0: taskCmd = taskStatus['taskCmd'] if taskCmd == 'refresh': debug.debug('refresh cmd %s' % taskStatus['appleId']) verifycodedata, tSt = page.get_verification_code_pic() taskStatus['verifyCodeData'] = verifycodedata taskStatus['taskCmd'] = None time.sleep(1) continue if taskCmd == 'submit': debug.debug('get submit cmd %s' % taskStatus['appleId']) postData = page.build_submit_post_data() postData['captchaAnswer'] = taskStatus['captchaAnswer'] postData['phoneNumber'] = taskStatus['phoneNumber'] postData['smsCode'] = taskStatus['smsCode'] postData['clientTimezone'] = taskStatus['clientTimezone'] # 'Asia/Shanghai' submitUrl = self.challengeUrlFormat % GeniusbarPage.storeNumber headers = page.headers headers['Referer'] = submitUrl headers.pop('Accept-Encoding') submitpage = GeniusbarPage(submitUrl, data=urllib.urlencode(postData), headers=headers) data = submitpage.get_data() resultfile = 'tmp/%s.htm' % taskStatus['appleId'] Writefile(resultfile, data) attrs = { "class": "error-message on", "id": "error_message_generalError" } errorMsg = submitpage.get_tag_text('label', attrs=attrs) if errorMsg: taskStatus['cmdStatus'] = 'NOK' taskStatus['taskCmd'] = None taskStatus['prompInfo'] = errorMsg page = submitpage debug.error(errorMsg) Writefile('tmp/submiterr.html', page.get_data()) continue else: # success for submit # get the time slots ret, maxrow = self.buildTimeSlotsTable(submitpage) taskStatus['cmdStatus'] = 'OK' taskStatus['timeSlots'] = (ret, maxrow) taskStatus['taskCmd'] = None page = submitpage continue if taskCmd == 'timeslot': # post timeslots debug.debug('get timeslot cmd %s' % taskStatus['appleId']) postData = self.buildPostTimeSlotsData(page) postData['clientTimezone'] = taskStatus['clientTimezone'] postData['id'] = taskStatus['id'] tlsUrl = self.timeslotFormat % GeniusbarPage.storeNumber debug.debug('tls ulr %s' % tlsUrl) headers = {} headers[ 'Accept-Language'] = 'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3' headers[ 'User-Agent'] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0" headers[ 'Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' headers["Connection"] = 'keep-alive' headers["Host"] = 'concierge.apple.com' tlspage = GeniusbarPage(tlsUrl, data=urllib.urlencode(postData), headers=headers) data = tlspage.get_data() Writefile('tmp/posttimeslotsresult.html', data) text = self.getConfirmMsg(tlspage) taskStatus['prompInfo'] = text.replace(' ', '') Writefile('tmp/reserv-%s' % taskStatus['appleId'], taskStatus['prompInfo']) taskStatus['taskCmd'] = None taskStatus['cmdStatus'] = 'OK' break if taskCmd == 'end': taskStatus['taskCmd'] = None break #debug.debug('waiting cmd') time.sleep(1) runtime -= 1 debug.info('End task %s' % taskStatus['appleId'])