def delete(bot, update): text_chat = update['message']['text'] coin_from_chat_list = text_chat.split(" ") coin_name = coin_from_chat_list[1].lower() user_chat_id = update['message']['chat']['id'] coins = db_session.query(UserQuery).join(CoinBase).join(User).\ filter(User.id_user_chat==user_chat_id).\ filter(CoinBase.coin_name==coin_name).all() if len(coins) > 0: if not coin_name: print(111) coins = db_session.query(UserQuery).join(User).join( CoinBase).filter(User.id_user_chat == user_chat_id).all() for coins_del in coins: db_session.delete(coins_del) db_session.commit() else: print(222) for coins_del in coins: db_session.delete(coins_del) db_session.commit() else: print("@#!$!@") my_text = '''Такого запроса нет. ¯\_(ツ)_/¯''' update.message.reply_text(my_text)
def coin_check(bot, update): user_chat_id = update['message']['chat']['id'] base_chat_id = db_session.query(User) for user_chat_id1 in base_chat_id: user_chat_id_dict = user_chat_id1.__dict__['id_user_chat'] coins = db_session.query(UserQuery).join(CoinBase).join(User).\ filter(User.id_user_chat==user_chat_id_dict).filter(UserQuery.user_coin_name==CoinBase.id).\ filter(or_(and_(UserQuery.query_minmax == "меньше", UserQuery.query_price > CoinBase.price_usd), and_(UserQuery.query_minmax == "больше", UserQuery.query_price < CoinBase.price_usd))).all() if len(coins) > 0: for coin in coins: coin_id = coin.__dict__['user_coin_name'] coin_names = db_session.query(CoinBase).filter( CoinBase.id == coin_id).all() for coin_result in coin_names: print(coin_result.__dict__['coin_name']) bot.send_message( chat_id=user_chat_id_dict, text= "Тревога!!! Твоя монета %s сейчас %s чем %s, она стоит - %s" % (coin_result.__dict__['coin_name'], coin.__dict__['query_minmax'], coin.__dict__['query_price'], coin_result.__dict__['price_usd'])) else: print("Проверка не пройдена")
def _up(): data = get_bit("https://api.coinmarketcap.com/v1/ticker/?limit=10") for data_coin in data: name_coin = data_coin['name'] price_coin = data_coin['price_usd'] print(price_coin) print(name_coin) coin_in_db = db_session.query(CoinBase).filter( CoinBase.coin_name == name_coin).first() if not coin_in_db: coin = CoinBase(data_coin['name'].lower(), data_coin['price_usd'], datetime.utcnow()) db_session.add(coin) db_session.commit() print("Добавлено значение - %s" % name_coin) else: print('обновляю значение') coin_in_db.price_usd = float(price_coin) db_session.add(coin_in_db) db_session.commit()
def query_bot(bot, update): user_chat_id = update['message']['chat']['id'] text_chat = update['message']['text'] coin_from_chat_list = text_chat.split(" ") try: coin_name = coin_from_chat_list[1].lower() except IndexError as err: print(err) update.message.reply_text("Что-то не так с названием валюты") try: coin_minmax = coin_from_chat_list[2].lower() print(coin_minmax) if (coin_minmax == "больше"): coin_minmax = coin_minmax elif coin_minmax == "меньше": coin_minmax = coin_minmax else: update.message.reply_text("Не правильно задаете параметры") return except IndexError as err: print(err) update.message.reply_text("Не правильно задаете параметры") try: coin_price = coin_from_chat_list[3] if is_float(coin_price): coin_price = coin_price else: update.message.reply_text("Что-то не так с ценой") return except IndexError as err: print(err) update.message.reply_text("Что-то не так с ценой") try: coin = db_session.query(CoinBase).filter( CoinBase.coin_name == coin_name).first() except IndexError as err: print(err) update.message.reply_text("Что-то пошло не так!") if not coin: text = "Такой валюты нет" update.message.reply_text(text) return user = db_session.query(User).filter( User.id_user_chat == user_chat_id).first() if not user: text = "Такого пользователя нет" update.message.reply_text(text) return check_query = db_session.query(UserQuery).join(User).join(CoinBase).\ filter(User.id==user.id, CoinBase.id==coin.id, UserQuery.query_minmax == coin_minmax).first() if not check_query: user_query = UserQuery(user.id, coin.id, coin_minmax, coin_price, datetime.utcnow()) print(user_query) db_session.add(user_query) db_session.commit() else: user_query = db_session.query(UserQuery).join(User).join(CoinBase).\ filter(User.id==user.id, CoinBase.id==coin.id, UserQuery.query_minmax == coin_minmax).first() user_query.query_price = coin_price db_session.add(user_query) db_session.commit() text = "Такой запрос уже был, интересующее Вас значение обновлено" update.message.reply_text(text) return my_text = """{} {}, ваш запрос добавлен! """.format(update.message.chat.first_name, update.message.chat.last_name) update.message.reply_text(my_text)