async def main_listening_loop(conn, analyser): await init(conn) now = dt.datetime.now(s.TIMEZONE) last_loop_execution = dt.datetime(1970, 1, 1, tzinfo=s.TIMEZONE) last_new_participants_check = now participants_info = await conn.check_for_new_participants_in_main_channel( first_time=True) # todo_f: first_time et known_participants sont redondants (y'a pas 1 sans l'autre, enfin presque...) while now < s.END_LISTENING_LOOP: last_loop_execution = await wait_for_next_iteration( last_loop_execution, s.NEW_LISTENING_LOOP_ITERATION_EVERY) try: now, last_new_participants_check, participants_info = await _loop_action( conn, analyser, now, last_new_participants_check, participants_info) except KeyboardInterrupt as e: raise e except: logging.exception( "[ERREUR AU SEIN DE LA BOUCLE] Erreur inconnue dans listening_loop" ) if now >= s.END_LISTENING_LOOP: logging.info( "Fin de la boucle LISTENING_LOOP car END_LISTENING_LOOP = %s" % s.END_LISTENING_LOOP)
async def sandbox_loop(conn): await init(conn) now = dt.datetime.now(sb.TIMEZONE) last_loop_execution = dt.datetime(1970, 1, 1, tzinfo=sb.TIMEZONE) await sb.populate() # todo_es : populate ne doit s'exécuter que si la campagne est nouvelle (ou si effacement BDD) while now < END_SANDBOX_LOOP: last_loop_execution = await wait_for_next_iteration( last_loop_execution, NEW_SANDBOX_LOOP_ITERATION_EVERY) try: now = await _loop_action(conn) except KeyboardInterrupt as e: raise e except Exception: logging.exception( "[ERREUR AU SEIN DE LA BOUCLE] Erreur inconnue dans sandbox_loop" ) if now >= END_SANDBOX_LOOP: logging.info( "Fin de la boucle SANDBOX_LOOP car END_SANDBOX_LOOP = %s" % END_SANDBOX_LOOP)
async def main_suggestion_loop(conn): await init(conn) now = dt.datetime.now(s.TIMEZONE) last_loop_execution = dt.datetime(1970, 1, 1, tzinfo=s.TIMEZONE) last_new_participants_check = now # participants_info = await conn.check_for_new_participants_in_main_channel(first_time=True) while now < s.END_SUGGESTION_LOOP: last_loop_execution = await wait_for_next_iteration( last_loop_execution, s.NEW_SUGGESTION_LOOP_ITERATION_EVERY) try: # todo_es dans les 3 boucles, now n'est pas forcement modifié par _loop_action (en cas de levée d'erreur). # sortir donc le now de _llop_action now, last_new_participants_check = await _loop_action( conn, now, last_new_participants_check) except KeyboardInterrupt as e: raise e except Exception as e: logging.exception( "[ERREUR AU SEIN DE LA BOUCLE] Erreur inconnue dans suggestion_loop" ) if now >= s.END_SUGGESTION_LOOP: logging.info( "Fin de la boucle SUGGESTION_LOOP car END_SUGGESTION_LOOP = %s" % s.END_SUGGESTION_LOOP)
async def iter_participants(self, tg_channel, first_time=False, **kwargs): if tg_channel is None: logging.error( "On essaie d'obtenir les participant·e·s d'un channel qui n'a pas été trouvé" ) return iter([]) try: tg_participants = self.client.iter_participants( tg_channel, **kwargs) except Exception as e: logging.exception(e) return iter([]) now = dt.datetime.now(s.TIMEZONE) # todo_es : !!!! lors d'une suppression de bdd postgres (ou nouveau campain_id), cette ligne plante au tout premier lancement, pas au 2nd default_sug_freq = await s.get_conf_value( s.CAMPAIN_ID, 'DEFAULT_SUGGESTIONS_FREQUENCY') res = [ await TelegramParticipant.from_telethon_object(p, now, default_sug_freq, first_time) async for p in tg_participants ] # todo_chk peut on utiliser yield ici? return iter(res)
def run(): try: db = DataBase() with TelegramConnection(db) as conn: analyser = MessageAnalyser(conn, actions, admin_actions) conn.run_with_async_loop(loops(conn, analyser)) except KeyboardInterrupt as e: raise e except Exception as e: logging.exception("[ARRET DU PROGRAMME] Erreur inconnue au niveau de run_all_loops")
def run(): try: db = DataBase() with TelegramConnection(db) as conn: conn.run_with_async_loop(sandbox_loop(conn)) except KeyboardInterrupt as e: raise e except Exception: logging.exception( "[ARRET DU PROGRAMME] Erreur inconnue au niveau de sandbox_loop")
def connect(self): res = None try: res = self.client.connect() except sqlite3.OperationalError as e: try: self._manage_sqlite3_errors(e) except TwitterstormError as ee: logging.critical(ee) except Exception as e: logging.exception(e) return res
async def get_dialogs(self, *args, **kwargs) -> TelegramPersonnalChannel: res = [] try: res = self.client.get_dialogs(*args, **kwargs) except sqlite3.OperationalError as e: try: self._manage_sqlite3_errors(e) except TwitterstormError as ee: logging.exception(ee) except Exception: logging.exception("Erreur inconnue") return [TelegramPersonnalChannel.from_telethon_object(c) for c in res]
async def get_input_entity(self, peer) -> TelegramPersonnalChannel: tg_channel = None try: tg_channel = await self.client.get_input_entity(peer) except Exception: logging.exception("Channel non trouvé pour l'argument 'peer' = " + str(peer)) raise KeyboardInterrupt( "Si tu tombes là une fois, c'est qu'il est pertinent de laisser la suite du code dans " "self._get_1to1_channel. Sinon, non ;)" ) # todo_cr à retirer à terme return TelegramPersonnalChannel.from_telethon_object(tg_channel)
def send_message(self, tg_channel, message, **kwargs): if tg_channel is None: logging.error( "On essaie d'envoyer un message vers un channel qui n'a pas été trouvé\nMessage=\n" + str(message)) return None try: res = self.client.send_message(tg_channel, message, **kwargs) except Exception as e: logging.exception(e) return None return res
async def get_messages(self, tg_channel, nb_msgs, **kwargs): if tg_channel is None: logging.error( "On essaie d'obtenir les messages d'un channel qui n'a pas été trouvé" ) return [] try: res = await self.client.get_messages(tg_channel, nb_msgs, **kwargs) except Exception as e: logging.exception(e) return [] res = [TelegramMessage.from_telethon_object(m) for m in res] return res
async def get_entity(self, entity) -> TelegramChannel: unknown_channel_msg = 'L\'identifiant du channel demandé ({}) est inconnu'.format( entity) try: tg_channel = await self.client.get_entity(entity) except struct.error: logging.exception(unknown_channel_msg) tg_channel = None except PeerIdInvalidError: logging.exception( unknown_channel_msg + '\nIl est possible que le compte Télégram qu\'utilise le bot' + ' ne soit pas parmi les membres du channel principal. Il n\'est donc pas ' + 'autorisé à accéder à ses informations. Merci de l\'y ajouter.' ) tg_channel = None except Exception: logging.exception("Erreur inconnue") tg_channel = None return TelegramChannel.from_telethon_object(tg_channel)