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))
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
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
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)
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))
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))
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));
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))