Skip to content

Commit

Permalink
Merge pull request #73 from jmendeth/robustness
Browse files Browse the repository at this point in the history
Improvements when downloading media / handling responses
  • Loading branch information
tvdstaaij authored Feb 25, 2017
2 parents f3a272d + a48a2eb commit cd260ea
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
5 changes: 2 additions & 3 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@
chunk_retry: 3

# Time in seconds to wait before considering a download stuck (0 = infinite)
# This setting is not very useful at the moment, it just skips to the next
# dialog if downloading a media file takes too long
media_timeout: 0
# After this time the script skips to the next message
media_timeout: 100


## Advanced behavior settings ##
Expand Down
45 changes: 27 additions & 18 deletions telegram-history-dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,30 @@ def dump_dialog(dialog)
])
msg_chunk = nil
retry_count = 0
while retry_count <= $config['chunk_retry'] do
loop do
if retry_count >= $config['chunk_retry']
$log.error('Failed to fetch chunk of %d messages from offset %d '\
'after retrying %d times. Dump of "%s" is incomplete.' % [
$config['chunk_size'], cur_offset,
retry_count, dialog['print_name']
])
msg_chunk = []
offset += $config['chunk_size']
break
end
begin
Timeout::timeout($config['chunk_timeout']) do
msg_chunk = exec_tg_command('history', dialog['print_name'],
$config['chunk_size'], cur_offset)
end
break
rescue Timeout::Error
if retry_count == $config['chunk_retry']
$log.error('Failed to fetch chunk of %d messages from offset %d '\
'after retrying %d times. Dump of "%s" is incomplete.' % [
$config['chunk_size'], cur_offset,
retry_count, dialog['print_name']
])
msg_chunk = []
offset += $config['chunk_size']
if msg_chunk.is_a?(Array)
break
end
$log.error('Timeout, retrying... (%d/%d)' % [
$log.warn('telegram-cli returned a non array chunk, retrying... (%d/%d)' % [
retry_count += 1, $config['chunk_retry']
])
rescue Timeout::Error
$log.warn('Timeout, retrying... (%d/%d)' % [
retry_count += 1, $config['chunk_retry']
])
end
Expand Down Expand Up @@ -155,15 +160,18 @@ def process_media(dialog, msg)
next unless $config['download_media'][media_type]
next unless msg['media']['type'] == media_type
response = nil
Timeout::timeout($config['media_timeout']) do
begin
begin
Timeout::timeout($config['media_timeout']) do
response = exec_tg_command('load_' + media_type, msg['id'])
rescue StandardError => e
$log.error('Failed to download media file: %s' % e)
return
end
rescue StandardError => e
# This is a warning because we're going to log an error afterwards
$log.warn('Failed to download media file: %s' % e)
end
filename = case
when response.nil? || !response.is_a?(Hash)
$log.error('Wrong response on media download for message id %s' % msg['id'])
nil
when $config['copy_media']
filename = File.basename(response['result'])
destination = File.join(get_media_dir(dialog), fix_media_ext(filename))
Expand All @@ -177,7 +185,7 @@ def process_media(dialog, msg)
rescue StandardError => e
$log.error('Failed to delete media file: %s' % e)
end
msg['media']['file'] = filename if filename
msg['media']['file'] = filename
end
end

Expand Down Expand Up @@ -246,6 +254,7 @@ def save_progress
cli_opts.cfgfile ||
File.expand_path('../config.yaml', __FILE__)
)
STDOUT.sync = true
$log = Logger.new(STDOUT)

if $config['track_progress'] && system_big_endian?
Expand Down

0 comments on commit cd260ea

Please sign in to comment.