Beispiel #1
0
def send_update_to_user(user: str, auto, codes=None):
    user_data = None
    try:
        user_data = persistence.user_data[user]
        running = user_data.setdefault(Section.Running.value, False)
        update_updater_data()

        if Section.API_Key.value not in user_data:
            updater.bot.send_message(
                user, text='API Key not set ⛔️\nSet in /settings')
            return

        if running and not auto:
            updater.bot.send_message(user, text='Already running 🏃')
            return

        user_data[Section.Running.value] = True
        market = Market(user_data[Section.API_Key.value])
        now = current_timestamp()

        first = True
        for code in codes or user_data.get(Section.Watchlist.value, {}):
            try:
                code_data = persistence.user_data[user][
                    Section.Watchlist.value][code]['value']
                last_run = code_data[Section.LastRun.value]
                frequency = parse(code_data[Section.Frequency.value])
                interval = parse(code_data[Section.Interval.value])
                enabled = code_data[Section.Enabled.value]
                # print(code, last_run + frequency, now, last_run + frequency - now)

                if auto and (not enabled or last_run + frequency > now):
                    continue

                code_data[Section.LastRun.value] = current_timestamp()
                update_updater_data()

                if first:
                    if auto:
                        updater.bot.send_message(user,
                                                 text='Getting updates 🌎')
                    first = False
                else:
                    # Wait to not overload the api
                    msg = updater.bot.send_message(
                        user, text='Waiting 60 seconds for API... ⏳')
                    run(sleep(60))
                    msg.delete()

                msg = updater.bot.send_message(user,
                                               text=f'Calculating {code}... ⏳')
                delta = datetime.fromtimestamp(now - interval)
                chart = market.get_wma(code, delta)
                msg.delete()
                updater.bot.send_photo(
                    user,
                    photo=chart,
                    disable_notification=True,
                    caption=f'{code} - {code_data[Section.Interval.value]}')
            except Exception as e:
                print(f'❌ {user} - {e}')
                updater.bot.send_message(user,
                                         text=f'There was an error ⚠️\n {e}')

        if not first and auto:  # Has run at least once
            updater.bot.send_message(user, text=f'Done ✅')
    finally:
        if user_data:
            user_data[Section.Running.value] = False
        update_updater_data()