Class: Wget
- Inherits:
-
Object
- Object
- Wget
- Defined in:
- lib/wget/wget.rb,
lib/wget/version/version.rb,
lib/wget/toplevel_methods/toplevel_methods.rb
Overview
#
require ‘wget/toplevel_methods.rb’
#
Constant Summary collapse
- BACKUP_DIR =
#
BACKUP_DIR
#
'/home/x/Temp/'
- LAST_DOWNLOADED_FILE =
#
LAST_DOWNLOADED_FILE
The next line tells us where we keep our last downloaded file.
#
ENV['HOME'].to_s+'/LAST_DOWNLOADED_FILE.md'
- VERSION =
#
Wget::VERSION
#
'1.0.38'
- LAST_UPDATE =
#
Wget::LAST_UPDATE
#
'19.04.2024'
Class Method Summary collapse
-
.check_if_remote_file_exists(this_file) ⇒ Object
# === Wget.check_if_remote_file_exists.
-
.download(this_file) ⇒ Object
# === Wget.download ========================================================================= #.
-
.download_to? ⇒ Boolean
# === Wget.download_to? ========================================================================= #.
-
.remote_file_exists?(i) ⇒ Boolean
# === Wget.remote_file_exists? ========================================================================= #.
Instance Method Summary collapse
-
#are_the_colours_available? ⇒ Boolean
# === are_the_colours_available? ========================================================================= #.
-
#backup_existing_file(i = local_target?) ) ⇒ Object
# === backup_existing_file ========================================================================= #.
-
#check_if_remote_file_exists ⇒ Object
# === check_if_remote_file_exists.
-
#consider_downloading_original_file_via_wget(shall_we_check_whether_the_remove_file_exists_or_not = remote_file_exists? ) ⇒ Object
# === consider_downloading_original_file_via_wget.
-
#consider_making_a_backup ⇒ Object
# === consider_making_a_backup ========================================================================= #.
-
#download_last_url ⇒ Object
(also: #downloaded_where?)
# === download_last_url.
-
#initialize(download_this = nil, run_already = true, &block) ⇒ Wget
constructor
# === initialize ========================================================================= #.
-
#input? ⇒ Boolean
# === url? ========================================================================= # === input?.
-
#local_target? ⇒ Boolean
(also: #local_file?)
# === local_target? ========================================================================= #.
-
#log_this_file_was_downloaded(i) ⇒ Object
# === log_this_file_was_downloaded ========================================================================= #.
-
#opne(i = '') ⇒ Object
# === opne ========================================================================= #.
-
#opnn(optional_pass_this = @pass_this_to_opn) ⇒ Object
# === opnn.
-
#rds(i) ⇒ Object
# === rds ========================================================================= #.
-
#register_sigint ⇒ Object
# === register_sigint ========================================================================= #.
-
#remote_file_exists ⇒ Boolean
# === remote_file_exists? ========================================================================= # === remote_file_exists.
-
#remote_file_exists? ⇒ Boolean
# === remote_file_exists? ========================================================================= #.
-
#reset ⇒ Object
# === reset ========================================================================= #.
-
#run ⇒ Object
# === run (run tag) ========================================================================= #.
-
#saving_last_downloaded_file ⇒ Object
# === saving_last_downloaded_file ========================================================================= #.
-
#set_url(i = nil) ⇒ Object
# === set_url ========================================================================= #.
-
#try_to_report_where_we_downloaded_the_file ⇒ Object
# === try_to_report_where_we_downloaded_the_file ========================================================================= #.
-
#url? ⇒ Boolean
# === url? ========================================================================= #.
Constructor Details
#initialize(download_this = nil, run_already = true, &block) ⇒ Wget
#
initialize
#
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/wget/wget.rb', line 63 def initialize( download_this = nil, run_already = true, &block ) register_sigint reset set_url(download_this) case run_already.to_s when 'dont', 'do_not', 'dont_connect', 'do_not_connect' run_already = false end # ======================================================================= # # === Handle blocks next # ======================================================================= # if block_given? yielded = yield # ====================================================================== # # === Handle Hashes next # ====================================================================== # if yielded.is_a?(Hash) and yielded.has_key?(:namespace) @pass_this_to_opn = yielded.delete(:namespace) # ====================================================================== # # === Handle Symbols next # ====================================================================== # elsif yielded.is_a? Symbol case yielded # ==================================================================== # # === :no_check_certificate # ==================================================================== # when :no_check_certificate @additional_options_to_wget = '--no-check-certificate' end end end run if run_already end |
Class Method Details
.check_if_remote_file_exists(this_file) ⇒ Object
#
Wget.check_if_remote_file_exists
This method will return a boolean value - true if the remote website (remote URL) exists, and false otherwise. The functionality depends on the availability of wget.
The first argument is the remote target file.
Note that, for a reason I am not sure, “wget –spider” does not always work. So perhaps “curl” is better for these cases.
Invocation example:
Wget.does_this_remote_website_exist? 'http://shevegen.square7.ch/rbt_changelog.html'
#
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/wget/toplevel_methods/toplevel_methods.rb', line 33 def self.check_if_remote_file_exists(this_file) # ======================================================================= # # Use wget --spider to check if the remote file exists. # ======================================================================= # _ = "wget --spider -v #{this_file} 2>&1" result = `#{_}` if result.include?('Remote file exists') or # Yes, the remote file exists. result =~ /File '.+' exists./ remote_file_exists = true else remote_file_exists = false end if result.include? '/404' remote_file_exists = false end return remote_file_exists end |
.download(this_file) ⇒ Object
#
Wget.download
#
54 55 56 57 |
# File 'lib/wget/toplevel_methods/toplevel_methods.rb', line 54 def self.download(this_file) _ = Wget.new(this_file) _.try_to_report_where_we_downloaded_the_file end |
.download_to? ⇒ Boolean
#
Wget.download_to?
#
12 13 14 |
# File 'lib/wget/toplevel_methods/toplevel_methods.rb', line 12 def self.download_to? LAST_DOWNLOADED_FILE end |
Instance Method Details
#are_the_colours_available? ⇒ Boolean
#
are_the_colours_available?
#
348 349 350 |
# File 'lib/wget/wget.rb', line 348 def are_the_colours_available? Object.const_defined?(:Colours) end |
#backup_existing_file(i = local_target?) ) ⇒ Object
#
backup_existing_file
#
185 186 187 188 189 190 191 |
# File 'lib/wget/wget.rb', line 185 def backup_existing_file(i = local_target?) new_target = BACKUP_DIR+File.basename(i) opne 'Now moving existing file from '+sfile(i)+ ' to '+sfile(new_target)+'.' FileUtils.mv(i, new_target) File.delete(i) if File.exist? i end |
#check_if_remote_file_exists ⇒ Object
#
check_if_remote_file_exists
This method will set the variable @remote_file_exists appropriately.
#
259 260 261 |
# File 'lib/wget/wget.rb', line 259 def check_if_remote_file_exists @remote_file_exists = Wget.check_if_remote_file_exists(url?) end |
#consider_downloading_original_file_via_wget(shall_we_check_whether_the_remove_file_exists_or_not = remote_file_exists? ) ⇒ Object
#
consider_downloading_original_file_via_wget
Simply use wget to download a remote file, but only if it actually exists.
#
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/wget/wget.rb', line 321 def consider_downloading_original_file_via_wget( shall_we_check_whether_the_remove_file_exists_or_not = remote_file_exists? ) if @additional_options_to_wget shall_we_check_whether_the_remove_file_exists_or_not = true # Bit hackish for now. end if shall_we_check_whether_the_remove_file_exists_or_not _ = "wget #{@url}" if @additional_options_to_wget _ << " #{@additional_options_to_wget}" end opnn # ====================================================================== # # We will display the actual command that is to be used next. # ====================================================================== # if are_the_colours_available? e "#{::Colours.steelblue(_)}#{rev}" else e _ end system(_) end end |
#consider_making_a_backup ⇒ Object
#
consider_making_a_backup
#
174 175 176 177 178 179 180 |
# File 'lib/wget/wget.rb', line 174 def consider_making_a_backup if remote_file_exists? and File.exist?(local_file?) opne "We will first backup the existing file at `"\ "#{sfile(local_file?)}`." backup_existing_file(local_file?) end end |
#download_last_url ⇒ Object Also known as: downloaded_where?
#
download_last_url
This will try to download the last saved URL.
#
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/wget/wget.rb', line 205 def download_last_url _ = LAST_DOWNLOADED_FILE if File.exist? _ where = File.readlines(_).first # ======================================================================= # # where may include a '#' character such as in # "/Depot/jj/pygtksourceview-2.3.0.tar.bz2 # 2015-01-05 02:37:12 +0100" # We will eliminate this part next. # ======================================================================= # where = where[0, where.index('#')] where = where.strip return where else e 'The file at '+sfile(_)+' does not exist.' end end |
#input? ⇒ Boolean
#
url?
#
input?
227 228 229 |
# File 'lib/wget/wget.rb', line 227 def url? @url end |
#local_target? ⇒ Boolean Also known as: local_file?
#
local_target?
#
196 197 198 |
# File 'lib/wget/wget.rb', line 196 def local_target? rds(Dir.pwd+'/'+File.basename(@url)) end |
#log_this_file_was_downloaded(i) ⇒ Object
#
log_this_file_was_downloaded
#
161 162 163 164 165 166 167 168 169 |
# File 'lib/wget/wget.rb', line 161 def log_this_file_was_downloaded(i) opne "Next storing #{simp(i)}" opne 'in '+sfile(LAST_DOWNLOADED_FILE) # ======================================================================= # # As of 17.09.2014 we also add the time: # ======================================================================= # i << " # #{Time.now}" save_what_into(i, LAST_DOWNLOADED_FILE) end |
#opne(i = '') ⇒ Object
#
opne
#
154 155 156 |
# File 'lib/wget/wget.rb', line 154 def opne(i = '') opnn; e i end |
#opnn(optional_pass_this = @pass_this_to_opn) ⇒ Object
#
opnn
This method must be able to operate with a specific namespace at hand - see the first entry clause for that.
#
305 306 307 308 309 310 311 312 313 |
# File 'lib/wget/wget.rb', line 305 def opnn( optional_pass_this = @pass_this_to_opn ) if optional_pass_this # First entry clause. Opn.opn(namespace: optional_pass_this) else # Second entry clause. Opn.opn end end |
#rds(i) ⇒ Object
#
rds
#
135 136 137 |
# File 'lib/wget/wget.rb', line 135 def rds(i) i.squeeze('/') end |
#register_sigint ⇒ Object
#
register_sigint
#
108 109 110 |
# File 'lib/wget/wget.rb', line 108 def register_sigint Signal.trap('SIGINT') { e; exit } end |
#remote_file_exists ⇒ Boolean
#
remote_file_exists?
#
remote_file_exists
234 235 236 |
# File 'lib/wget/wget.rb', line 234 def remote_file_exists? @remote_file_exists end |
#remote_file_exists? ⇒ Boolean
#
remote_file_exists?
#
232 233 234 |
# File 'lib/wget/wget.rb', line 232 def remote_file_exists? @remote_file_exists end |
#reset ⇒ Object
#
reset
#
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/wget/wget.rb', line 115 def reset # ======================================================================= # # === @remote_file_exists # ======================================================================= # @remote_file_exists = true # ======================================================================= # # === @pass_this_to_opn # ======================================================================= # @pass_this_to_opn = nil # Whether to pass something to Opn.opn() # ======================================================================= # # === @additional_options_to_wget # # The next variable can be used to pass additional options to "wget". # ======================================================================= # @additional_options_to_wget = nil end |
#run ⇒ Object
#
run (run tag)
#
355 356 357 358 359 360 |
# File 'lib/wget/wget.rb', line 355 def run check_if_remote_file_exists consider_making_a_backup consider_downloading_original_file_via_wget saving_last_downloaded_file # We save the last downloaded file, to make further use of that information. end |
#saving_last_downloaded_file ⇒ Object
#
saving_last_downloaded_file
#
142 143 144 145 146 147 148 149 |
# File 'lib/wget/wget.rb', line 142 def saving_last_downloaded_file # ======================================================================= # # If the file was successfully downloaded, we will log this. # ======================================================================= # if File.exist?(local_target?) log_this_file_was_downloaded(local_target?) end end |
#set_url(i = nil) ⇒ Object
#
set_url
#
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/wget/wget.rb', line 273 def set_url(i = nil) i = i.first if i.is_a? Array if i.nil? opne 'Wget: We are missing an URL.' opne 'Usage: wget [URL]' exit end case i # case tag # ======================================================================= # # === wget --query # ======================================================================= # when 'QUERY?','?', /^-?-?query$/i opne "The last downloaded URL is at #{sfancy(download_last_url)}." exit when 'LAST' i = download_last_url end i = i.to_s.dup i.strip! if i.end_with? '/' or i.end_with? ':' i.chop! end @url = i end |
#try_to_report_where_we_downloaded_the_file ⇒ Object
#
try_to_report_where_we_downloaded_the_file
#
248 249 250 251 252 |
# File 'lib/wget/wget.rb', line 248 def try_to_report_where_we_downloaded_the_file if downloaded_where?.include? input? opne 'We downloaded into: '+sfancy(downloaded_where?) end end |
#url? ⇒ Boolean
#
url?
#
225 226 227 |
# File 'lib/wget/wget.rb', line 225 def url? @url end |