Esempio n. 1
0
def history_cb(msg_list, peer, success, msgs):
    print(len(msgs))
    msg_list.extend(msgs)
    print(len(msg_list))
    if len(msgs) == HISTORY_QUERY_SIZE:
        tgl.get_history(peer, len(msg_list), HISTORY_QUERY_SIZE,
                        partial(history_cb, msg_list, peer))
Esempio n. 2
0
def backup_next():
    global peer_queue, outfile, missing_date_count
    if missing_date_count > 0:
        print('Warning: %d messages were missing a date and are '
              'probably not backed up correctly' % missing_date_count)
    missing_date_count = 0
    if outfile:
        outfile.close()
        outfile = None
    if not peer_queue:
        print('All backups have been completed')
        return False
    peer = peer_queue.pop(0)
    filename = re.sub(r'[^a-zA-Z0-9\-.,;]', '_', peer.name)
    path = BACKUP_DIR + '/' + filename + '.jsonl'
    print('Backing up %s to %s' % (peer.name, path))
    os.makedirs(BACKUP_DIR, exist_ok=True)
    outfile = open(path, 'w')
    if HISTORY_LIMIT > 0:
        chunk_size = min([HISTORY_CHUNK_SIZE, HISTORY_LIMIT])
    else:
        chunk_size = HISTORY_CHUNK_SIZE
    cb = partial(history_cb, chunk_size, 0, peer)
    tgl.get_history(peer, 0, chunk_size, cb)
    return True
Esempio n. 3
0
def history_cb(chunk_count, total_count, peer, success, msgs):
    global outfile, missing_date_count
    assert success
    next_total = total_count + chunk_count
    print('Backing up %s [messages %d-%d]' %
          (peer.name, total_count, next_total - 1))
    for i in range(0, len(msgs)):
        msg = msgs[i]
        if not msg:
            continue
        if not msg.date:
            missing_date_count += 1
        msg_dict = make_msg_dict(msg)
        if not msg_dict:
            continue
        json_obj = json.dumps(msg_dict)
        outfile.write(json_obj)
        outfile.write("\n")
    sleep(REQUEST_DELAY)
    if (len(msgs) == chunk_count
            and (next_total < HISTORY_LIMIT or HISTORY_LIMIT == 0)):
        if HISTORY_LIMIT > 0 and next_total + chunk_count > HISTORY_LIMIT:
            chunk_count = HISTORY_LIMIT - next_total
        cb = partial(history_cb, chunk_count, next_total, peer)
        tgl.get_history(peer, next_total, chunk_count, cb)
    else:
        backup_next()
    return True
Esempio n. 4
0
File: bot.py Progetto: MeGaTG/pybot
def history_cb(msg_list, peer, success, msgs):
    print(len(msgs))
    msg_list.extend(msgs)
    print(len(msg_list))
    if len(msgs) == HISTORY_QUERY_SIZE:
        tgl.get_history(
            peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer))
    else:
        text = '\n'.join([str(i + 1) + ") " + x.text for (i, x) in enumerate(msgs[::-1]) if x.text is not None and not x.out])
        peer.send_msg(text, msg_cb)
Esempio n. 5
0
 def history_cb(self, msg_count, chat, success, msgs):
     if success:
         self.insert_history(msgs)
         msg_count += len(msgs)
         if len(msgs) == self.HISTORY_QUERY_SIZE:
             tgl.get_history(chat, msg_count,
                             self.HISTORY_QUERY_SIZE,
                             partial(self.history_cb, msg_count, chat))
         else:
             tgl.send_msg(chat, "Loaded {0} messaged into the table".format(msg_count))
Esempio n. 6
0
def history_cb(msg_list, peer, success, msgs):
    print(len(msgs))
    msg_list.extend(msgs)
    print(len(msg_list))
    if len(msgs) == HISTORY_QUERY_SIZE:
        tgl.get_history(
            peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer))
    else:
        text = '\n'.join([str(i + 1) + ") " + x.text for (i, x) in enumerate(msgs[::-1]) if x.text is not None and not x.out])
        peer.send_msg(text, msg_cb)
Esempio n. 7
0
 def history_cb(self, msg_count, chat, success, msgs):
     if success:
         self.insert_history(msgs)
         msg_count += len(msgs)
         if len(msgs) == self.HISTORY_QUERY_SIZE:
             tgl.get_history(chat, msg_count, self.HISTORY_QUERY_SIZE,
                             partial(self.history_cb, msg_count, chat))
         else:
             tgl.send_msg(
                 chat,
                 "Loaded {0} messaged into the table".format(msg_count))
Esempio n. 8
0
def history_cb(msg_list, peer, success, msgs):
  print(len(msgs))
  msg_list.extend(msgs)
  print(len(msg_list))
  if len(msgs) == HISTORY_QUERY_SIZE:
    tgl.get_history(peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
Esempio n. 9
0
 def load_history(self, msg, matches):
     chat = msg.dest
     msg_count = 0
     tgl.get_history(chat, msg_count,
                     self.HISTORY_QUERY_SIZE,
                     partial(self.history_cb, msg_count, chat))
Esempio n. 10
0
 def load_history(self, msg, matches):
     chat = msg.dest
     msg_count = 0
     tgl.get_history(chat, msg_count, self.HISTORY_QUERY_SIZE,
                     partial(self.history_cb, msg_count, chat))