def fit_my_data(): data = cache.get("my_data") # Success vs. Failure if data: # Drop the user id for x in ["user_id", "userid"]: if x in data.keys(): print("DELETING", x) del (data[x]) # Right now, no test on the data to make sure it complies with API... # * * * * * * * * * * * * # This is where the magic happens prediction = fit_to_model(data) # * * * * * * * * * * * * # return jsonify(prediction) return render_template('submit-data-success.html', title="Success", data=prediction) else: errors = cache.get("errors") return render_template('submit-data-failure.html', title="Failure", errors=errors)
def get(cls, key): user = cache.get("User::uid::%s" % key) or \ cache.get("User::username::%s" % key) or \ cache.get("User::nickname::%s" % key) if not user: user = User.query.get(key) or \ User.query.get_by_username(key) or \ User.query.get_by_nickname(key) if user: cls.set(user) return user
def get(cls, key): if key is None: return ct = cache.get("CIType::ID::%s" % key) or \ cache.get("CIType::Name::%s" % key) if ct is None: ct = db.session.query(CIType).filter( CIType.type_name == key).first() or \ db.session.query(CIType).filter(CIType.type_id == key).first() if ct is not None: CITypeCache.set(ct) return ct
def wrapped(*args, **kwargs): current_user_id = get_jwt_identity() if not current_user_id: return abort(401) if not cache.get(current_user_id): return abort(401) if cache.get(current_user_id) not in roles: return abort(403) return f(*args, **kwargs)
def get(cls, rid): role = None if isinstance(rid, (int, long)): role = cache.get("Role::rid::%s" % rid) if not role: role = db.session.query(Role).filter(Role.rid == rid).first() cls.set(role) elif isinstance(rid, basestring): role = cache.get("Role::role_name::%s" % rid) if not role: role = db.session.query(Role).filter( Role.role_name == rid).first() cls.set(role) return role
def download_page(self, item): """ 获取下一页的网页地址等 根据当前状态标记新的状态 0:初始页面 1:全局列表页面 2: 抽取下载连接 """ item = MyDict(item) try: print(f" download_page info: {item.url}") logger.info("开始下载网页!{}。类型:{}".format(item.url, item.type)) # current_app.logger.info("开始下载网页!{}。类型:{}".format(item.url, item.type)) old_type = item.type if item.url != "None" and not cache.get(item.url): # if item.url != "None": html_obj = requests.get(item.url, headers=self.headers) html_str = html_obj.content.decode("gbk") item.content = html_str print(len(html_str)) # 请求结果存入redis数据库 cache.set(item.url, html_str) cache.expire(item.url, self.redis_key_timeout) # item.xpath_obj = lxml.html.fromstring(html_str) logger.info("下载前类型:{}, 下载后类型:{}".format(old_type, item.type)) self.push_item_in_redis_list(self.message_b, item) except Exception as e: logger.error("download_page: {}".format(e))
def search_iphone(): # 购买者的信息,每次循环实时获取最新的购买者信息 buyers_info = get_buyers_info() availability = r.get(current_config['IPHONE_MODELS_URL']).json() if availability['stores']: # 遍历意向购买的商店和意向购买的商品 for store, models in buyers_info.items(): # 遍历意向购买的型号和对应的购买人 for model_number, buyers in models.items(): # 获取商品型号在店内的库存 stock = availability['stores'][store][model_number] if stock['availability']['unlocked'] is True: for buyer in buyers: key = '{}{}{}'.format(buyer['idcard'], store, model_number) if cache.get(key) is None: buyer['store'] = store # with rabbit as mq: # mq.send_message(exchange_name='iphone', queue_name='buyers', messages=buyer) # 日志记录及微信消息发送 msg = '发现有效库存,商店【{0}】, 型号【{1}】,时间【{2}】。'.format( get_store_name(store), get_model_name(model_number), datetime.now().strftime('%Y-%m-%d %H:%M:%S')) r.get( ' http://sc.ftqq.com/{}.send?text=您关注的iPhone已有库存&desp={}' .format(current_config.SEC_KEY, msg)) logging.info(msg) logging.info('买家信息:{}'.format(buyer)) logging.info('[猎鹰] 已将目标设备和买家信息发送给猎手') # 已经发送过的购买者信息,5分钟内不再发送 cache.set(key=key, val='已发送', time=180) else: logging.info('[猎鹰] 没有发现有效库存')
def get(cls, key): if key is None: return attr = cache.get('Field::Name::%s' % key) or \ cache.get('Field::ID::%s' % key) or \ cache.get('Field::Alias::%s' % key) if attr is None: attr = db.session.query(CIAttribute).filter_by( attr_name=key).first() or \ db.session.query(CIAttribute).filter( CIAttribute.attr_id == key).first() or \ db.session.query(CIAttribute).filter( CIAttribute.attr_alias == key).first() db.session.close() if attr is not None: CIAttributeCache.set(attr) return attr
def get(cls, key): if key is None: return ct = cache.get("CITypeSPEC::ID::%d" % key) if ct is None: ct = db.session.query(CIType).filter(CIType.type_id == key).first() if ct is not None: CITypeSpecCache.set(ct) return ct
def get(cls, key): if key is None: return if isinstance(key, basestring) and isinstance(key, unicode): key = unicode(key, 'utf8') citypes = cache.get("CITypeAttribute::Name::%s" % key) or \ cache.get("CITypeAttribute::ID::%s" % key) if not citypes: citypes = db.session.query(CITypeAttribute).filter( CITypeAttribute.type_id == key).all() if citypes is None: ci_type = db.session.query(CIType).filter( CIType.type_name == key).first() if ci_type is not None: citypes = db.session.query(CITypeAttribute).filter_by( type_id=ci_type.type_id).all() if citypes is not None: CITypeAttributeCache.set(key, citypes) return citypes
def cart(modify=None): carts = cache.get('cart%d' % current_user.id) if carts: commodities = Commodity.query.filter(Commodity.id.in_(carts)).all() else: commodities = None if modify == 'add': pass return render_template('auth/cart/index.html', title='购物车', commodities=commodities)
def check_if_token_in_blacklist(decrypted_token): jti = decrypted_token.get('jti') if not jti: return True token = cache.get(jti) if token: return False return True
def cart_add(): cart = cache.get('cart%d' % current_user.id) c_id = int(request.args.get('id')) if cart is None: data = [] else: data = cart if c_id in data: return jsonify({'status': False, 'msg': '您已添加过该产品,不可重复添加'}) data.append(c_id) data = list(set(data)) cache.set('cart%d' % current_user.id, data) return jsonify({'status': True, 'msg': '添加成功'})
def code_sign_in_service(code: str): user_id = cache.get(code) if not user_id: return None user = mongo.db.users.find_one({'_id': ObjectId(user_id)}) if not user: return None tokens = create_tokens(user_id=user_id) cache.set(user_id, user.get('roles')) return tokens
def index(): if request.method == 'POST': value = request.form.get('indexValue') bar = cache.get(value) if bar: return jsonify({'cached_result': bar}) else: r = requests.post('http://api:4000', json={'value': value}) result = r.json() #得到的其实使dict cache.set(value, result.get('result'), timeout=50) history.insert({ "requested time": datetime.now(), "requested index": value, "calculated value": result.get('result', '') }) return jsonify(result) return render_template('index.html')
def clean(cls, attr): if cache.get('Field::ID::%s' % attr.attr_id): cache.delete('Field::ID::%s' % attr.attr_id) cache.delete('Field::Name::%s' % attr.attr_name) cache.delete('Field::Alias::%s' % attr.attr_alias)
def testcache(): name = 'mink' return name + " " + str(cache.get('testcache'))
def send_reg_code(self): """ 申请注册码 :return: """ logging.info('[猎手] 当前链接:{}'.format(self.driver.current_url)) # 注册码,暂时没什么用,最终是通过缓存获取到用于验证的注册码 reg_code = '123456' # 手机号,暂时没什么用,最终是通过缓存获取到用于验证的手机号码 phone_number = '18858888888' # 获取页面元素 validate_reg_code = self.wait_find_element_by_xpath( current_config.VALIDATE_REG_CODE) btn_continue = current_config.BTN_NEED_REG_CODE_XPATH # 需要申请注册码的情况 if validate_reg_code.text == '申请并验证你的注册码。': # 说明验证码已经过期,清理掉 cache.delete(self.apple_id) # 重新申请一个 logging.info('[猎手] 需要申请注册码') btn_continue = current_config.BTN_NEED_SEND_SMS_XPATH # 验证码 sms_code = self.wait_find_element_by_xpath( current_config.SMS_CODE_XPATH) logging.info('[猎手] 页面验证码为{}'.format(sms_code.text)) # 发送短信 rabbit.connect() self.send_message( messages={ 'content': sms_code.text, 'target': current_config.SEND_TO, 'apple_id': self.apple_id }) logging.info('[猎手] 将验证码发送给消息队列') rabbit.disconnect() # 遍历获取缓存注册码 logging.info('[猎手] 等待获取注册码短信') while True: sms_list = cache.get(self.apple_id) if sms_list: logging.info('[猎手] 已获取到短信:{}'.format(sms_list)) break # 排序 sms_list.sort(key=itemgetter('datetime')) for sms in sms_list: if '注册码' in sms['text']: phone_number = sms['send_from'] reg_code = sms['text'][6:15] logging.info('[猎手] 解析短信完成,apple id:{0},注册码:{1}'.format( self.apple_id, reg_code)) break # 填写手机号码 input_phone_number = self.wait_find_element_by_xpath( current_config.PHONE_NUMBER_XPATH) input_phone_number.send_keys(phone_number) logging.info('[猎手] 已填写手机号码:{}'.format(phone_number)) # 填写注册码 input_reg_code = self.wait_find_element_by_xpath( current_config.REG_CODE_XPATH) input_reg_code.send_keys(reg_code) logging.info('[猎手] 已填写注册码:{}'.format(reg_code)) # 继续 btn_continue = self.wait_find_element_by_xpath(btn_continue) btn_continue.click() logging.info('[猎手] 点击继续按钮') # 如果出现注册码错误,清理缓存并重试 # if self.is_elements_by_xpath(current_config.ERR_REG_CODE): # err_reg_code = self.wait_find_element_by_xpath(current_config.ERR_REG_CODE) # if err_reg_code.is_displayed() is True: # cache.delete(current_config.APPLE_ID) # raise ErrorBuy logging.info('[猎手] 准备进行最后一步预约') self.last_step()
def testcache(): name = 'mink' return name + " " + str(cache.get('testcache'))
def save_sms(apple_id, sms_list): # 将验证码写入缓存,30分钟超时 cache.set(apple_id, sms_list, time=1750) if cache.get(apple_id) is None: raise RuntimeError
def get(cls, uid): sp = cache.get("SpecialPermission::uid::%s" % uid) return sp