class Wget

#

require 'wget/toplevel_methods.rb'

#
#

require 'wget/version/version.rb'

#

Constants

BACKUP_DIR
#

BACKUP_DIR

#
LAST_DOWNLOADED_FILE
#

LAST_DOWNLOADED_FILE

The next line tells us where we keep our last downloaded file.

#
VERSION
#

Wget::VERSION

#

Public Class Methods

check_if_remote_file_exists(this_file) click to toggle source
#

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 target file.

Invocation example:

Wget.does_this_remote_website_exist? 'http://shevegen.square7.ch/rbt_changelog.html'
#
# File lib/wget/toplevel_methods.rb, line 30
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) click to toggle source
#

Wget.download

#
# File lib/wget/toplevel_methods.rb, line 51
def self.download(this_file)
  _ = Wget.new(this_file)
  _.try_to_report_where_we_downloaded_the_file
end
download_to?() click to toggle source
#

Wget.download_to?

#
# File lib/wget/toplevel_methods.rb, line 12
def self.download_to?
  LAST_DOWNLOADED_FILE
end
new( download_this = nil, run_already = true ) { || ... } click to toggle source
#

initialize

#
# File lib/wget/wget.rb, line 62
def initialize(
    download_this = nil,
    run_already   = true
  )
  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
  if block_given?
    yielded = yield
    if yielded.is_a?(Hash) and
       yielded.has_key?(:namespace)
      @pass_this_to_opn = yielded.delete(:namespace)
    end
  end
  run if run_already
end
remote_file_exists?(i) click to toggle source
#

Wget.remote_file_exists?

#
# File lib/wget/wget.rb, line 218
def self.remote_file_exists?(i)
  _ = Wget.new(i, :dont_run_yet)
  _.check_if_remote_file_exists
  return _.remote_file_exists
end

Public Instance Methods

backup_existing_file(i = local_target?) click to toggle source
#

backup_existing_file

#
# File lib/wget/wget.rb, line 150
def backup_existing_file(i = local_target?)
  new_target = BACKUP_DIR+File.basename(i)
  opnn; e '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() click to toggle source
#

check_if_remote_file_exists

This method will set the variable @remote_file_exists appropriately.

#
# File lib/wget/wget.rb, line 238
def check_if_remote_file_exists
  @remote_file_exists = Wget.check_if_remote_file_exists(url?)
end
consider_downloading_original_file_via_wget() click to toggle source
#

consider_downloading_original_file_via_wget

Simply use wget to download a remote file, but only if it actually exists.

#
# File lib/wget/wget.rb, line 171
def consider_downloading_original_file_via_wget
  if remote_file_exists?
    _ = "wget #{@url}"
    opnn; e _
    system(_)
  end
end
consider_making_a_backup() click to toggle source
#

consider_making_a_backup

#
# File lib/wget/wget.rb, line 139
def consider_making_a_backup
  if remote_file_exists? and File.exist?(local_file?)
    opnn; e "We will first backup the existing file at `"\
            "#{sfile(local_file?)}`."
    backup_existing_file(local_file?)
  end
end
does_this_remote_website_exist?()
download_last_url() click to toggle source
#

download_last_url

This will try to download the last saved URL.

#
# File lib/wget/wget.rb, line 184
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
Also aliased as: downloaded_where?
downloaded_where?()
Alias for: download_last_url
input?()
Alias for: url?
local_file?()
Alias for: local_target?
local_target?() click to toggle source
#

local_target?

#
# File lib/wget/wget.rb, line 161
def local_target?
  rds(Dir.pwd+'/'+File.basename(@url))
end
Also aliased as: local_file?
log_this_file_was_downloaded(i) click to toggle source
#

log_this_file_was_downloaded

#
# File lib/wget/wget.rb, line 126
def log_this_file_was_downloaded(i)
  opnn; e "Next storing #{simp(i)}"
  opnn; e '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
opnn(optional_pass_this = @pass_this_to_opn) click to toggle source
#

opnn

#
# File lib/wget/wget.rb, line 280
def opnn(optional_pass_this = @pass_this_to_opn)
  if optional_pass_this
    Opn.opn(optional_pass_this)
  else
    Opn.opn
  end
end
rds(i) click to toggle source
#

rds

#
# File lib/wget/wget.rb, line 107
def rds(i)
  i.squeeze('/')
end
register_sigint() click to toggle source
#

register_sigint

#
# File lib/wget/wget.rb, line 86
def register_sigint
  Signal.trap('SIGINT') { e; exit }
end
remote_file_exists()
Alias for: remote_file_exists?
remote_file_exists?() click to toggle source
#

remote_file_exists?

#
# File lib/wget/wget.rb, line 211
def remote_file_exists?
  @remote_file_exists
end
Also aliased as: remote_file_exists
reset() click to toggle source
#

reset

#
# File lib/wget/wget.rb, line 93
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()
end
run() click to toggle source
#

run (run tag)

#
# File lib/wget/wget.rb, line 291
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() click to toggle source
#

saving_last_downloaded_file

#
# File lib/wget/wget.rb, line 114
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) click to toggle source
#

set_url

#
# File lib/wget/wget.rb, line 252
def set_url(i = nil)
  i = i.first if i.is_a? Array
  if i.nil?
    opnn; e 'Wget: We are missing an URL.'
    opnn; e 'Usage: wget [URL]'
    exit
  end
  case i # case tag
  # ======================================================================= #
  # === wget --query
  # ======================================================================= #
  when 'QUERY?','?','--query'
    opnn; e '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() click to toggle source
#

try_to_report_where_we_downloaded_the_file

#
# File lib/wget/wget.rb, line 227
def try_to_report_where_we_downloaded_the_file
  if downloaded_where?.include? input?
    opnn; e 'We downloaded into: '+sfancy(downloaded_where?)
  end
end
url?() click to toggle source
#

url?

#
# File lib/wget/wget.rb, line 204
def url?
  @url
end
Also aliased as: input?