async def daily_report(self): hall = self.hall_channel records = await operations.fetch_daily_workers(self.elon.pool) if not records: await hall.send(f'Nobody worked today, are u guys still alive? :sunglasses:') today = get_msk_time() message = f":sunglasses: ** Another productive day is passing by [{today.strftime('%d-%m-%Y')}] ** :sunglasses: \n\n" place = 0 for record in records: place += 1 total_seconds = int(record.get('duration')) username = record.get('username') hours = total_seconds // 3600 minutes = (total_seconds // 60) % 60 if place == 1: message += f':trophy: {username} → ** |{hours:02d} : {minutes:02d}| ** \n' elif place == 2: message += f':second_place: {username} → ** |{hours:02d} : {minutes:02d}| ** \n' elif place == 3: message += f':third_place: {username} → ** |{hours:02d} : {minutes:02d}| ** \n' else: message += f'{username} → ** |{hours:02d} : {minutes:02d}| ** \n' message += f'``` ```' await hall.send(message)
async def schedule_hello_ping(self): ''' Start hello_ping at 10AM ''' await self.elon.wait_until_ready() dt_msk = get_msk_time() if self.elon.debug: # start ASAP *almost td = datetime.timedelta(seconds=3) dt_run_time = dt_msk + td else: # it may leave 1 day off, but it's okay td = datetime.timedelta(days=1) dt_run_time = dt_msk + td dt_run_time = dt_run_time.replace(hour=10, minute=0, second=0) # getting the amount of seconds between them td = dt_run_time - dt_msk ts = td.total_seconds() print(f'Waiting for hello_ping, {ts} seconds left') if ts < 0: # need to reschedule or whatever print('We can\'t run the task in the past!') return # wait some amount of seconds until it's 10AM await asyncio.sleep(ts) self.hello_ping.start()
async def schedule_daily_report(self): ''' To properly start the loop with daily reports Only at 11:59PM ''' await self.elon.wait_until_ready() # I need to start running daily_report everyday at 11:59PM # But first time it should aslo run at 11:59PM # This method only runs once! But we can reconfigure to check if # something went wrong and if we need to rerun daily_report dt_msk = get_msk_time() if self.elon.debug: # start ASAP *almost td = datetime.timedelta(seconds=3) dt_run_time = dt_msk + td else: # start at 11:59PM dt_run_time = dt_msk.replace(hour=23, minute=59, second=50) # getting the amount of seconds between them td = dt_run_time - dt_msk ts = td.total_seconds() print(f'Waiting for daily report, {ts} seconds left') if ts < 0: # need to reschedule or whatever print('We can\'t run the task in the past!') return # wait some amount of seconds until it's 11:59PM await asyncio.sleep(ts) # once it's 11:59PM start loop task self.daily_report.start()
def wrapped(message): from database import users_db user_id = message.chat.id user_name = message.from_user.username group = users_db.get_users_group(message.chat.id) with open('./logs/' + get_log_filename(), 'a') as file: print(utils.get_msk_time(), user_id, user_name, group, message.text, file=file) return func(message)
async def weekly_report(self): today = get_msk_time() hall = self.hall_channel week_num = today.isocalendar()[1] records = await operations.fetch_week_workers(self.elon.pool, week_num) message = f''' :sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles:\ \r\n:sparkles:** Heroes Of The Week ** :sparkles:\ \r\n:sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles:\r ''' if not records: await hall.send(f'Nobody worked this week. Are u ahuel tam? :sunglasses:') if not self.elon.debug: try: await self.manage_hero_role(records[:3]) except Exception as err: logger.exception(err) # nicely printing leaders place = 0 for record in records: place += 1 username = record.get('username') total_seconds = int(record.get('duration')) hours = total_seconds // 3600 minutes = (total_seconds // 60) % 60 if place < 3: message += f'\r\n:man_superhero: {username} → ** |{hours:02d} : {minutes:02d}| **' elif place == 3: message += f'\r\n:man_superhero: {username} → ** |{hours:02d} : {minutes:02d}| **' message += f''' \r\n:sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles:\ \r\n:sparkles:** Heroes Of The Week ** :sparkles:\ \r\n:sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles::sparkles: ''' else: message += f'\r\n {username} → ** |{hours:02d} : {minutes:02d}| **' await hall.send(message)
def get_log_filename(): now = utils.get_msk_time() return '{0}_{1}_{2}_commands_log.txt'.format(str(now.year), str(now.month), str(now.day))
bot.send_message(chat_id=message.chat.id, text=text_timetable, parse_mode='HTML') if __name__ == '__main__': debug = False if debug: #_ = utils.OwnTimetableStorage(0, 101) #apihelper.proxy = {'https': 'socks5://' + str(tokens.proxy)} bot.polling(none_stop=True) else: while True: try: bot.polling(none_stop=True) except Exception as e: print(e) with open('./logs/' + wrappers.get_log_filename(), 'a') as file: print(utils.get_msk_time(), 0, 'admin', 0, 'exception', file=file) print('Connection error at {}, restart in 1 sec'.format( utils.get_msk_time())) time.sleep(1)