Exemple #1
0
 def disconnect(self):
     res = None
     try:
         res = self.client.disconnect()
     except Exception as e:
         logging.critical(e)
     return res
Exemple #2
0
    def record_or_serve(self,
                        mode,
                        data_id,
                        data,
                        f1,
                        f2,
                        f1args=[],
                        f2args=[]):
        data = f1(data, *f1args)
        to_serve = f2(data, *f2args)

        if data_id not in self.recorded_data.keys():
            self.recorded_data[data_id] = {}
        if type(data) != str and type(data) != bytes:
            logging.critical(
                "L'argument 'data' doit être de type 'str' or 'bytes'")
        timestamp_key = (dt.datetime.now() - self.started_at).microseconds
        self.recorded_data[data_id][timestamp_key] = data

        now = dt.datetime.now()
        if now - self.last_file_save >= dt.timedelta(0, SAVE_EVERY):
            self.last_file_save = now
            dir_ = "data/test_data"
            create_dirs_if_not_exists([dir_])
            with open(os.path.join(dir_, "data_" + self.id + ".csv"),
                      "w+") as f:
                f.write("%s\t%s\t%s\n" % ("data_id", "datetime", "data"))
                for data_id, value in self.recorded_data.items():
                    for datetime, data in value.items():
                        f.write("%s\t%s\t%s\n" % (data_id, datetime, data))

        return to_serve
Exemple #3
0
 def __init__(self, tg_id):
     if not isinstance(tg_id, int):
         logging.test(20004)
         logging.critical("Un id Télégram est toujours un nombre entier")
     else:  ###
         logging.test(20005)
     self.tg_specific_data = {'tg_id': tg_id}
Exemple #4
0
 def _check_id_type(self, id_) -> None:
     if not isinstance(id_, TelegramParticipantId):
         logging.test(20029)
         logging.critical(
             "L'argument 'id' doit être de type TelegramParticipantId")
     else:  ###
         logging.test(20030)
Exemple #5
0
 def _check_id_type(self, id_) -> None:
     if not isinstance(id_, TelegramMessageId):
         logging.test(20022)
         logging.critical(
             "L'argument 'sender_id' doit être de type TelegramMessageId")
     else:  ###
         logging.test(20023)
Exemple #6
0
 def _check_message_id_type(self, last_checked_message_id) -> None:
     if not isinstance(last_checked_message_id, TelegramMessageId):
         logging.test(20031)
         logging.critical(
             "L'argument 'last_checked_message_id' doit être de type TelegramMessageId"
         )
     else:  ###
         logging.test(20032)
Exemple #7
0
 def _check_sender_id_type(self, sender_id) -> None:
     if not isinstance(sender_id, TelegramParticipantId):
         logging.test(20024)
         logging.critical(
             "L'argument 'sender_id' doit être de type TelegramParticipantId"
         )
     else:  ###
         logging.test(20025)
Exemple #8
0
 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
    def add_intro_about_sender(self, sender_name, scribe_type) -> str:
        st = self.message_str
        if scribe_type == "SCRIBE_ROBOT":
            intro = s.ROBOT_MSG_SUFFIX
        elif scribe_type == "SCRIBE_ANIMATOR":

            intro = s.ANIMATOR_MSG_SUFFIX + s.ANIMATOR_MSG_INTRO
            #intro = s.ANIMATOR_MSG_SUFFIX + s.ANIMATOR_MGS_INTRO % sender_name
        else:
            logging.critical('Option non disponible')
        st = intro + st

        return st
 async def _check_me(self, me):
     if self.me is None:
         logging.test(30017)
         if me is not None:
             logging.test(30018)
             self.me = me
             self.my_channel = await self._get_1to1_channel(me)
         else:
             logging.test(30019)
             if await s.get_conf_value(s.CAMPAIN_ID, 'SEND_ONLY_TO_ME'):
                 # todo_chk : vérifier l'utilité d'avoir un utilisateur ME si send_only_to_me = False.
                 # todo_chk : Si pas utile, décaler le if s.SEND_ONLY_TO_ME avant l'appel de la méthode _check_me
                 logging.critical("Attention, le compte de test ME doit être présent dans le channel dédié à la " +
                                 "twitterstorm si SEND_ONLY_TO_ME = True")
     else:  ###
         logging.test(30020)
Exemple #11
0
 def disconnect(self, exc_type, exc_value, traceback):
     """
     Overrides method in AbstractConnection
     """
     if exc_type is not None:
         logging.test(20046)
         if isinstance(exc_type, ConnectionError):
             logging.test(20047)
             logging.critical((
                 "La connection à Télégram a expirée. Reconnectez-vous.\n" +
                 "Le cas échéant, supprimez le fichier {}").format(
                     s.SESSION_LISTENING_LOOP))
         else:  ###
             logging.test(20044)
     else:  ###
         logging.test(20045)
     self.tg_client.disconnect()
Exemple #12
0
 def from_normalised_id_to_tg_id(norm_id):
     logging.test(20013)
     regexp = r'%s(\d+)\Z' % re.escape(TelegramMessageId.type_str)
     tg_id = None
     try:
         logging.test(20014)
         tg_id = re.findall(regexp, norm_id)[0]
     except IndexError:
         logging.test(20015)
         logging.critical(
             "L'argument norm_id est mal formé, il ne correspond pas à la regexp '%s'"
             % regexp)
     try:
         logging.test(20016)
         tg_id = int(tg_id)
     except ValueError:
         logging.test(20017)
         logging.critical(
             "L'argument norm_id est mal formé, l'id doit être un entier")
     return tg_id
Exemple #13
0
    def get_participant_by_id(self, id, participant_class):
        # todo_es : quand un user revient dans la boucle, il faudrait lui actualiser certains champs comme son nom par exemple
        result = []
        q = "select {} from participant where ".format(",".join(
            self.PARTICIPANT_FIELDS))
        q += "version = (select max(version) from participant "
        q += "           where campain = '{}' and normalised_id = '{}') ".format(
            s.CAMPAIN_ID, id)
        q += "and campain = '{}' ".format(s.CAMPAIN_ID)
        q += "and normalised_id = '{}' ".format(id)
        for row in self._select(q, []):
            result += [participant_class.from_db(*row)]

        #print(928789, "\n\nRETURNING USER!!!!!!!!! ", result[0].display_name)

        if len(result) > 1:
            logging.critical(
                "Plusieurs participants trouvés pour une même version")
        elif len(result) == 1:
            return result[0]
        else:
            return None
 def right_time_to_suggest_if_sandbox(self, now):
     if s.USE_SANDBOX :
         time_before_suggesting = self.time_before_suggesting_if_sandbox
     else :
         logging.critical("On ne doit pas appeler cette méthode si USE_SANBOX = False")
     return (self.last_arrival_in_channel + time_before_suggesting) < now