def process_queue(): while True: try: for task in QUEUE.find().batch_size(5): event_id = task['_id'] users = task['users'] if task['time_start'] - datetime.now(timezone.utc) < timedelta( minutes=20): logger.debug(f'Tick-tock for {event_id}') config = configparser.ConfigParser() config.read('settings.ini') data = Clubhouse( user_id=config['Clubhouse']['user_id'], user_token=config['Clubhouse']['user_token'], user_device=config['Clubhouse'] ['user_device']).get_event(event_hashid=event_id) if data.get('success'): ev = data['event'] room_id = ev['channel'] topic = ev['name'] if ev['is_expired']: logger.warning('Event expired!') QUEUE.delete_one({'_id': event_id}) for user in users: logger.info(f'Informing {user}') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram']['token'] ).bot.send_message( chat_id=user, text= f'Event <b>{topic}</b> has either expired or we were banned by clubhouse', parse_mode='html') except telegram.error.Unauthorized: logger.warning(f'{user} banned the bot!') elif ev['is_member_only']: logger.warning('Private!') QUEUE.delete_one({'_id': event_id}) for user in users: logger.info(f'Informing {user}') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram']['token'] ).bot.send_message( chat_id=user, text= f'Event <b>{topic}</b> is private, we cannot record it.', parse_mode='html') except telegram.error.Unauthorized: logger.warning(f'{user} banned the bot!') elif room_id: logger.info(f'Got room_id {room_id}') cur_task = TASKS.find_one({'_id': room_id}) if cur_task: logger.info('We already know about that room') TASKS.update_one( {'_id': room_id}, {'$addToSet': { 'users': users }}) QUEUE.delete_one({'_id': event_id}) for user in users: logger.info(f'Informing {user}') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram']['token'] ).bot.send_message( chat_id=user, text= f'Event <b>{topic}</b> has started. Preparing to record that room. Because of some new limits from Clubhouse that can take some time.', parse_mode='html') except telegram.error.Unauthorized: logger.warning( f'{user} banned the bot!') else: logger.error(f'{room_id}: New') TASKS.insert_one({ '_id': room_id, 'status': 'WAITING_FOR_TOKEN', 'topic': topic, 'users': users, 'dt': datetime.utcnow() }) QUEUE.delete_one({'_id': event_id}) for user in users: logger.info(f'Informing {user}') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram']['token'] ).bot.send_message( chat_id=user, text= f'Event <b>{topic}</b> has started. Preparing to record that room. Because of some new limits from Clubhouse that can take some time.', parse_mode='html') except telegram.error.Unauthorized: logger.warning( f'{user} banned the bot!') else: QUEUE.delete_one({'_id': event_id}) logger.critical('NO TOKEN! BAN???') for user in users: logger.info(f'Informing {user}') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram'] ['token']).bot.send_message( chat_id=user, text=f'Failed to get event {event_id}', parse_mode='html') except telegram.error.Unauthorized: logger.warning(f'{user} banned the bot!') sleep(15) elif datetime.now( timezone.utc) - task['time_start'] > timedelta( minutes=20): logger.warning('Event expired by timeout!') QUEUE.delete_one({'_id': event_id}) except: logger.critical('process_queue has broken') print(traceback.format_exc()) sleep(30)
def process_token(): while True: sleep(5) try: for task in TASKS.find({'status': 'WAITING_FOR_TOKEN'}): room_id = task['_id'] logger.info(f'Need token for {room_id}') config = configparser.ConfigParser() config.read('settings.ini') data = Clubhouse(user_id=config['Clubhouse']['user_id'], user_token=config['Clubhouse']['user_token'], user_device=config['Clubhouse'] ['user_device']).join_channel(room_id) users = task['users'] if data.get('success'): token = data['token'] topic = data['topic'] logger.debug(f'Got token for {room_id}: {token}') TASKS.update_one({'_id': room_id}, { '$set': { 'token': token, 'topic': topic, 'status': 'GOT_TOKEN' } }) for user in users: logger.info(f'Informing {user} about token') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram']['token'] ).bot.send_message( chat_id=user, text= f"Recording <b>{topic}</b>. We'll notify you as soon as it's over.", parse_mode='html') except telegram.error.Unauthorized: logger.warning(f'{user} banned the bot!') sleep(5) config = configparser.ConfigParser() config.read('settings.ini') Clubhouse(user_id=config['Clubhouse']['user_id'], user_token=config['Clubhouse']['user_token'], user_device=config['Clubhouse'] ['user_device']).leave_channel(room_id) elif 'This room is no longer available' in data.get( 'error_message', ''): for user in users: logger.info(f'Informing {user}') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram']['token'] ).bot.send_message( chat_id=user, text= f'Planned event has either expired or we were banned by clubhouse. Sorry :(', parse_mode='html') except telegram.error.Unauthorized: logger.warning(f'{user} banned the bot!') TASKS.delete_one({'_id': room_id}) else: TASKS.delete_one({'_id': room_id}) logger.critical('NO TOKEN! BAN???') for user in users: logger.info(f'Informing {user}') try: config = configparser.ConfigParser() config.read('settings.ini') Updater( config['Telegram']['token']).bot.send_message( chat_id=user, text=f'This is probably ban', parse_mode='html') except telegram.error.Unauthorized: logger.warning(f'{user} banned the bot!') sleep(25) except: logger.critical('process_token has broken') print(traceback.format_exc())