def trade_coin(user, input): message = "The trading format is 'trade 10 eth for 1000 gnt'" if len(input) < 6: return message else: from_num, from_str = get_num_coins(input[:3]) to_num, to_str = get_num_coins(input[3:]) from_coin = get_coin_from_input(from_str) to_coin = get_coin_from_input(to_str) w_from_coin = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == from_coin)).first() w_to_coin = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == to_coin)).first() if w_from_coin: if w_from_coin.amount > from_num: w_from_coin.amount -= from_num else: session.delete(w_from_coin) if w_to_coin: w_to_coin.amount += to_num else: print "adding {} {}".format(to_num, to_coin) new_wallet = Wallet(user, to_coin, to_num) session.add(new_wallet) session.commit() message = "You traded {} {} for {} {}".format( from_num, from_coin.symbol, to_num, to_coin.symbol) else: pass return message
def agg_start_list(start=None, end=None): #Data for tmin,tavg,tmax vals sel = [ func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs) ] def get_date(date1): year, month, day = map(int, date1.split('-')) converted_to_date = dt.datetime(year, month, day) return converted_to_date # check if start date and end end are provided or not if start is not None and end is not None: start_date = get_date(start) end_date = get_date(end) agg_list = session.query(*sel).filter( Measurement.date >= start_date).filter( Measurement.date <= end_date).all() elif start is not None and end is None: start_date = get_date(start) agg_list = session.query(*sel).filter( Measurement.date >= start_date).all() new_agg = np.ravel(agg_list) return jsonify({"min": new_agg[0], "avg": new_agg[1], "max": new_agg[2]})
def bio(bot, update): message = update.message.text.split() if len(message) == 1: update.message.reply_text(bio_helptext) return if message[1].lower() == 'add': user = get_or_create_user(update.message.from_user) bio = " ".join(message[2:]) user.set_bio(bio) bot.send_message(chat_id=update.message.chat_id, text="Added Bio") else: # the user has queried someone elses bio for entity in update.message.parse_entities().iterkeys(): if entity.user is not None: # get the requested user from their id t_id = entity.user.id user = session.query(User).filter( User.telegram_id == t_id).first() update.message.reply_text = user.get_bio() if entity.type == 'mention': # query db with username instead message[1] user = session.query(User).filter( User.username == message[1][1:]).first() update.message.reply_text = user.get_bio()
def wallet(bot, update): user = get_or_create_user(update.message.from_user) wallets = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.amount > 0)).all() if len(wallets) == 0: message = "You have no coins in your portfolio" else: message = u"<b>{}'s Portfolio</b>".format(user.first_name) total_value = 0 total_change = 0 for w in wallets: try: amount_val = w.coin.price_usd * w.amount dollar_change = amount_val * (w.coin.change_24h / 100) total_value += amount_val total_change += dollar_change message += u"\n{} {}: ${:,} ({:+}% \u2192 ${:+,})".format( w.amount, w.coin.symbol, round(amount_val, 2), w.coin.change_24h, round(dollar_change, 2)) except: pass message += u"\n\nUSD Change over 24h: ${:,}".format( round(total_change, 2)) message += u"\nTotal Value: <b>${:,}</b>".format(round(total_value, 2)) bot.send_message(chat_id=update.message.chat_id, text=message, parse_mode=ParseMode.HTML)
def stations(): station_list = session.query(Station.station)\ .order_by(Station.station).all() print() print("Station List:") list_stations = list(station_list) return jsonify(list_stations)
def tobs(): prev_yr_date = dt.datetime(2017, 8, 23) - dt.timedelta(days=365) temp_obs = session.query(Measurement.date, Measurement.tobs)\ .filter(Measurement.date >= prev_yr_date)\ .order_by(Measurement.date).all() print() print("Temperature Results for All Stations") return jsonify(temp_obs)
def get_or_create_user(user_data): t_id = user_data.id first_name = user_data.first_name last_name = user_data.last_name try: username = user_data.username except KeyError: username = None if session.query(exists().where(User.telegram_id == t_id)).scalar(): # get user user = session.query(User).filter(User.telegram_id == t_id).first() else: # create a new user user = User(t_id, first_name, last_name, username) session.add(user) session.commit() session.flush() return user
def precipitation(): # 365 days from most_recent prev_yr_date = dt.datetime(2017, 8, 23) - dt.timedelta(days=365) # Date and prcp values prcp_results = session.query(Measurement.date, Measurement.tobs)\ .filter(Measurement.date >= prev_yr_date).all() p_dict = dict(prcp_results) print() print("Results for Precipitation") return jsonify(p_dict)
def add_coin(user, input): num, coin_string = get_num_coins(input) coin = get_coin_from_input(coin_string) wallet = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == coin)).first() # if the user has this coin simply add to it, otherwise set it up if wallet: wallet.amount += num session.commit() else: wallet = Wallet(user, coin, num) session.add(wallet) session.commit() return "{} {} added".format(round(num, 3), coin.symbol)
def remove_coin(user, input): num, coin_string = get_num_coins(input) coin = get_coin_from_input(coin_string) wallet = session.query(Wallet).filter((Wallet.user == user) &\ (Wallet.coin == coin)).first() if wallet: if num == "all": return_string = "You have sold all of your {}".format(coin.symbol) session.delete(wallet) session.commit() return return_string else: wallet.amount -= num session.commit() return "You have sold {} of your {}. {} {} remaining".format( round(num, 3), coin.symbol, round(wallet.amount, 3), coin.symbol) else: # dont remove someting thats not there return "You have no {} to remove you big dumb baby".format(coin.symbol)
def refresh(bot, update): logging.log(logging.INFO, "Deleting Coins From DB") session.query(Coin).delete() session.commit() update.message.reply_text('Bot is restarting...') Thread(target=stop_and_restart).start()