From 601b776cfb323c46b14eb8efbf5e645ab27654e6 Mon Sep 17 00:00:00 2001 From: Tim van der Staaij Date: Sat, 4 Mar 2017 13:22:52 +0100 Subject: [PATCH] Calculate sleep time based on clock Speeds up dumping somewhat because processing and media download time is subtracted from the sleeping time; only the time actually necessary for complying with chunk_delay is spent sleeping. --- telegram-history-dump.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/telegram-history-dump.rb b/telegram-history-dump.rb index c17a82c..0851289 100755 --- a/telegram-history-dump.rb +++ b/telegram-history-dump.rb @@ -4,6 +4,7 @@ require 'json' require 'logger' require 'socket' +require 'time' require 'timeout' require 'yaml' require_relative 'dumpers/json' @@ -73,6 +74,7 @@ def dump_dialog(dialog) ]) msg_chunk = nil retry_count = 0 + last_chunk_download_time = Time.now loop do if retry_count >= $config['chunk_retry'] $log.error('Failed to fetch chunk of %d messages from offset %d '\ @@ -84,6 +86,7 @@ def dump_dialog(dialog) offset += $config['chunk_size'] break end + last_chunk_download_time = Time.now begin Timeout::timeout($config['chunk_timeout']) do msg_chunk = exec_tg_command('history', dialog['print_name'], @@ -153,7 +156,11 @@ def dump_dialog(dialog) end keep_dumping = false if offset < cur_offset + $config['chunk_size'] - sleep($config['chunk_delay']) if keep_dumping + if keep_dumping + time_to_sleep = last_chunk_download_time - Time.now + + $config['chunk_delay'] + sleep(time_to_sleep) if time_to_sleep > 0 + end end state = $dumper.end_dialog(dialog) || {} cur_progress.dumper_state=(state)