def async_user(cls, storage: ServiceData): data = storage.classify() data.interval = data.interval or cls.PInterval.p.default_value if not data.work: return crt_time = datetime.datetime.now().timestamp() if data.last_visit_time + 60 * data.interval > crt_time: return data.last_visit_time = crt_time data.visit_times += 1 try: key = cls.get_key_of(data.url) data.error_times = 0 except E: data.error_times += 1 if data.error_times == 3: Phone.announce(storage.user, cls, '监控任务%s网页连续三次无法访问,已停止任务' % data.name) storage.update(dict(work=False)) return if data.key != key: Phone.announce(storage.user, cls, '监控任务%s的网页发生变化' % data.name) storage.update(dict(work=False)) return storage.update(data)
def async_user(cls, storage: ServiceData): data = storage.classify() if not data.status: return if not data.error_times: data.error_times = 0 crt_time = datetime.datetime.now().timestamp() if data.last_visit_time and data.last_visit_time + 60 * 60 > crt_time: return data.last_visit_time = crt_time try: value = bocfx(data.currency, data.sort) except E: data.error_times += 1 if data.error_times == 3: Phone.announce(storage.user, cls, '中银%s汇率连续三次无法访问,已停止任务' % cls.FX[data.currency]) storage.update(dict(status=cls.STOP)) return value = float(value[0]) if not data.value or ((data.value > value) ^ (data.monitor == 'max')): data.value = value message = str(value) + '!' + \ '中银%s的%s达到历史新' % (cls.FX[data.currency], cls.KEY_TRANS[data.sort.replace(',', '_')]) + \ cls.M_TRANS[data.monitor] + '。' Phone.announce(storage.user, cls, message) storage.update(data)
def run(cls, directory: 'Service', storage: ServiceData, pd: ParamDict, *args): data = storage.classify() if pd.has(cls.PRenew): global_storage = cls.get_global_storage() global_data = global_storage.classify() if not global_data.phones: raise SMSMessage.NONE phone_num = len(global_data.phones) phone_index = random.randint(0, phone_num - 1) data.phone = global_data.phones[phone_index] storage.update(data) return data.phone if not data.phone: raise SMSMessage.NO_PHONE if pd.has(cls.PGet): return data.phone if pd.has(cls.PShow): lines = cls.crawler.get_msg(data, service=cls) return Lines(*lines) return cls.need_help()
def async_service(cls, storage: ServiceData): data = storage.classify() last_time = data.last_update_time or 0 crt_time = datetime.datetime.now().timestamp() if last_time + 60 * 30 > crt_time: return data.last_update_time = crt_time data.error_web_times = data.error_web_times or 0 data.error_re_times = data.error_re_times or 0 data.phones = cls.crawler.get_phone_list(data) or [] storage.update(data)
def run(cls, directory: 'Service', storage: ServiceData, pd: ParamDict, *args): storage.user.require_phone() data = storage.classify() if pd.has(cls.PCancel): data.work = False storage.update(data) return '任务已取消' if pd.has(cls.PStatus): if not data.work: return '暂无监控任务' return Lines( '正在监控:%s' % data.name, '任务开始时间:%s' % cls.readable_time(data.create_time), '已监控次数:%s' % data.visit_times, '监控时间间隔:%s分钟' % (data.interval or cls.PInterval.p.default_value)) if not args: return cls.need_help() url = args[0] if not url_validate(url): raise WatchError.URL key = cls.get_key_of(url) name = urlparse(url).netloc if pd.has(cls.PName): name = pd.get(cls.PName) interval = pd.get(cls.PInterval) crt_time = datetime.datetime.now().timestamp() storage.update( dict( work=True, name=name, url=url, visit_times=0, error_times=0, create_time=crt_time, last_visit_time=crt_time, key=key, interval=interval, )) return '监控已开启'
def run(cls, directory: 'Service', storage: ServiceData, pd: ParamDict, *args): data = storage.classify() # if data.status: # 设置手机号 # raise BindPhoneError.MODIFY_NOT_ALLOWED crt_time = datetime.datetime.now().timestamp() last_time = data.last_time or 0 if pd.has(cls.PPhone): phone = pd.get(cls.PPhone) captcha = get_random_string(length=6, allowed_chars='1234567890') send_wait = last_time + 60 - crt_time if send_wait > 0: raise BindPhoneMessage.SEND_WAIT(send_wait) Phone.validate(phone, captcha) storage.update( dict( phone=phone, captcha=captcha, last_time=crt_time, attempt=3, )) raise BindPhoneMessage.CAPTCHA_SENT elif pd.has(cls.PCaptcha): if not data.attempt: raise BindPhoneMessage.CAPTCHA_RESENT data.attempt -= 1 storage.update(data) if last_time + 5 * 60 < crt_time: raise BindPhoneMessage.TIME_EXPIRED captcha = pd.get(cls.PCaptcha) if captcha != data.captcha: raise BindPhoneMessage.CAPTCHA_WRONG(data.attempt) storage.user.set_phone(data.phone) storage.update(dict(status=cls.DONE)) raise BindPhoneMessage.SUCCESS else: return cls.need_help()
def run(cls, directory: 'Service', storage: ServiceData, pd: ParamDict, *args): data = storage.classify() if pd.has(cls.PShow): return Lines(*[ '%s:%s' % (k, '或'.join(cls.FX_REVERSE[k])) for k in cls.FX_REVERSE ]) if pd.has(cls.PSmsStop): if data.status == cls.START: data.status = cls.STOP storage.update(data) raise BOCError.STOP raise BOCError.NOT_START if not args: return cls.need_help() currency = args[0].upper() if currency not in cls.FX: raise BOCError.CURRENCY if pd.has(cls.PSms): storage.user.require_phone() if not (pd.has(cls.PSpotEx) ^ pd.has(cls.PBankNo)): raise BOCError.SE_BN if not (pd.has(cls.PAsk) ^ pd.has(cls.PBid)): raise BOCError.BID_ASK sort = ('SE' if pd.has(cls.PSpotEx) else 'BN') + ',' sort += 'ASK' if pd.has(cls.PAsk) else 'BID' storage.update( dict( currency=currency, sort=sort, monitor=pd.get(cls.PSms), value=None, status=cls.START, )) raise BOCError.START try: keys, values = bocfx(currency) except Exception as e: raise BOCError.SERVICE_INACCESSIBLE(debug_message=e) lines = [] time = '实时' for i, k in enumerate(keys): if k in cls.KEY_TRANS: if pd.has(cls.PSpotEx) ^ pd.has(cls.PBankNo): if pd.has(cls.PSpotEx) ^ ('SE' in k): continue if pd.has(cls.PAsk) ^ pd.has(cls.PBid): if pd.has(cls.PAsk) ^ ('ASK' in k): continue lines.append('%s:%s' % (cls.KEY_TRANS[k], values[i])) if k == 'Time': time = values[i] lines.insert(0, '中国银行%s牌价(%s)' % (cls.FX[currency], time)) return Lines(*lines)