Class: DownloadHistory

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging, Singleton
Defined in:
lib/download_history.rb

Overview

A singleton of this type manages the previously unfinished or queued downloads.

Constant Summary collapse

@@log =
init_logger()
@@history_base =
'rget_queue'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

init_logger, log_level=, log_target=

Methods included from File_Checking

#file_check, file_check

Constructor Details

#initializeDownloadHistory

load an existing list of unfinished downloads


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/download_history.rb', line 60

def initialize
	@log = @@log
        @log.level = $log_level	
	home = ENV['HOME']
	@history = nil
	@hthread = nil
	if home
		hist_dir = home.dup << File::SEPARATOR << ".#{File::basename($0) }"
		if !File.exist?(hist_dir)
			begin
			Dir.mkdir(hist_dir, 0700)
			rescue Exception => ex
				@log.error('Cannot create history in ' << hist_dir.dup << ': ' << ex.message)
				@log.error('Aborting')
				exit false
			end
		end
		@history_file = hist_dir.dup << File::SEPARATOR << @@history_base
		load_history
		@log.debug("history-file is in #{@history_file}")
	else
		@log.error('Cannot determine the current users home-directory')
		exit false
	end
end

Instance Attribute Details

#history_fileObject (readonly)

Returns the value of attribute history_file


160
161
162
# File 'lib/download_history.rb', line 160

def history_file
  @history_file
end

Instance Method Details

#[](ele) ⇒ Object


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/download_history.rb', line 105

def [](ele)
	@log.debug('checking for element ' << ele.to_s)
	prev_dl = nil
	if(@history && !@history.empty?)
		if(ele.respond_to?(:to_str))
			@history[ele]
		elsif(ele.respond_to?(:to_int))
			@log.debug('looking for queued item #' << (ele).to_s)
			a = @history.to_a
			if(ele <= a.size)
				a[ele - 1][1]
			else(a.size > 0)
				raise StandardError.new('Queued items range from 1 to ' << a.size.to_s << '!')
			end
		end
	end
end

#delete(url) ⇒ Object


149
150
151
152
153
154
# File 'lib/download_history.rb', line 149

def delete(url)
	@log.debug('deleting from queue : ' << @history[url].to_s)
	@history.delete(url)
	dump
	load_history
end

#empty?Boolean

is there a history?

Returns:

  • (Boolean)

56
57
58
# File 'lib/download_history.rb', line 56

def empty?
	!@history || @history.empty?
end

#lengthObject


156
157
158
# File 'lib/download_history.rb', line 156

def length()
	return @history.length
end

#load_historyObject


86
87
88
89
90
91
92
93
# File 'lib/download_history.rb', line 86

def load_history
	if(File.exist?(@history_file) && !File.zero?(@history_file)  )
		File.open(@history_file, 'r') do |hf|
			@history = Marshal.load(hf)	
			@log.debug ('history is ' << @history.to_s)
		end
	end
end

#previous_download(url) ⇒ Object

find and keep for later reference a previous attempt to load the file from url


101
102
103
# File 'lib/download_history.rb', line 101

def previous_download(url)
	@history ? @history[url] : nil
end

#queued_URLsObject


95
96
97
# File 'lib/download_history.rb', line 95

def queued_URLs
	@history ? @history.keys : []
end

#update(url, opts = {}) ⇒ Object

update te download-history for the file at url.


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/download_history.rb', line 124

def update(url, opts = {})
	begin
		@log.debug('update')
		@history ||= Hash.new
		# produces a lot of output 
		if ! @history.has_key?(url) 
			@history[url] = Download.new(url, opts[:local])
		elsif(opts[:local])
			@history[url].local_file = opts[:local]
		end

		@history[url].position = opts[:position] if opts[:position]
		@history[url].size = opts[:size] if opts[:size]
		@history[url].updated=DateTime.now
		@history[url].file_hash
		@log.debug('dumping history ' << @history.inspect)
		dump
	rescue Exception => ex
		@log.error('CANNOT update history : ' << ex.message)
		@log.error('history is ' << @history.to_s)
		raise ex
	end
	@log.debug('download is ' << @history[url].to_s)
end