Class: Roebe::DownloadFromThisUrl

Inherits:
Base show all
Defined in:
lib/roebe/classes/download_from_this_url.rb

Overview

Roebe::DownloadFromThisUrl

Constant Summary collapse

/<a href="(.+?)">?/

Constants inherited from Base

Base::COLOURS, Base::HOME_DIRECTORY_OF_USER_X, Base::N, Base::NAMESPACE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #bold_blue, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkslateblue, #deeppink, #delete_symlink, #disable_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esystem, #esystem_colourized, #ewarn, #firebrick, #get_current_day, #get_current_month, #get_files_from, #get_german_name_for_this_weekday, #gold, #grey, #hh_mm_ss, #home_dir?, #home_directory_of_user_x?, #is_a_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lime, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumspringgreen, #mediumturquoise, #mkdir, #mv, #n_days_in_this_month, #no_file_exists_at, #olivedrab, #open_in_browser, #opne, #opnn, #orange, #orchid, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #return_all_directories_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #show_help, #silent_redirection?, #simp, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #swarn, #symlink, #teal, #to_camelcase, #tomato, #touch, #use_colours?, #verbose_truth, #weekday?, #write_what_into, #yellow

Methods included from Base::CommandlineArguments

#clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #return_commandline_arguments_with_leading_hyphens, #set_commandline_arguments

Constructor Details

#initialize(use_this_url = nil, run_already = true) ⇒ DownloadFromThisUrl

#

initialize

#

43
44
45
46
47
48
49
50
51
52
53
# File 'lib/roebe/classes/download_from_this_url.rb', line 43

def initialize(
    use_this_url = nil,
    run_already  = true
  )
  reset
  set_commandline_arguments(
    use_this_url
  )
  set_url(use_this_url)
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

Roebe::DownloadFromThisUrl[]

#

257
258
259
# File 'lib/roebe/classes/download_from_this_url.rb', line 257

def self.[](i = '')
  new(i)
end

Instance Method Details

#do_filter(i) ⇒ Object

#

do_filter

#

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/roebe/classes/download_from_this_url.rb', line 112

def do_filter(i)
  use_this_filter = nil
  if @filter_for
    use_this_filter = @filter_for.map {|line|
      '.'+line.to_s
    }
  end
  # ========================================================================= #
  # The filter is now e. g. this Array:
  #   [".pdf", ".csv", ".gz", ".txt"]
  # ========================================================================= #
  if i.is_a?(Array) and use_this_filter
    i.select! {|line| # line may be: "<a href="vo04.pdf">vo04.pdf</a>"
      use_this_filter.any? {|this_filter|
        line.include? this_filter
      }
    }
  end
  set_remote_dataset(i)
end

#download_these_files(i) ⇒ Object

#

download_these_files (download tag)

#

178
179
180
181
182
183
184
185
186
187
188
# File 'lib/roebe/classes/download_from_this_url.rb', line 178

def download_these_files(i)
  if i.is_a? Array
    i.each {|this_file|
      e "Now downloading #{sfile(this_file)}:"
      from_this_resource = remote_base_url?+this_file
      _ = "wget #{from_this_resource}"
      e royalblue("  #{_}")
      system _
    }
  end
end
#

menu (menu tag)

#

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/roebe/classes/download_from_this_url.rb', line 233

def menu(
    i = return_commandline_arguments_with_leading_hyphens
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ========================================================================= #
    # === --no-filters
    #
    # Usage example:
    #
    #   download_from_this_url https://archive.xfce.org/xfce/4.16pre1/src/ --no-filter
    #
    # ========================================================================= #
    when /^-?-?no(-|_| )?filters?$/i
      @filter_for = nil
    end
  end
end

#obtain_all_entries_from_the_remote_website(i = url? ) ⇒ Object

#

obtain_all_entries_from_the_remote_website

#

217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/roebe/classes/download_from_this_url.rb', line 217

def obtain_all_entries_from_the_remote_website(
    i = url?
  )
  e 'Reading all entries from `'+sfancy(i)+'` next.'
  begin
    @remote_dataset = URI.open(i).read
  rescue Net::FTPPermError => error # Rescue permission errors.
    e 'Net::FTPPermError occurred:'
    pp error
    @remote_dataset = nil
  end
end

#regex?Boolean

#

regex?

#

Returns:

  • (Boolean)

171
172
173
# File 'lib/roebe/classes/download_from_this_url.rb', line 171

def regex?
  NON_GREEDY_REGEX_FOR_A_HREF_LINKS
end

#remote_base_url?Boolean

#

remote_base_url?

#

Returns:

  • (Boolean)

136
137
138
139
140
141
142
# File 'lib/roebe/classes/download_from_this_url.rb', line 136

def remote_base_url?
  _ = @url
  unless _.end_with? '/'
    _ = _[0 .. _.rindex('/')]
  end
  _
end

#remote_dataset?Boolean Also known as: main_dataset?

#

remote_dataset?

#

Returns:

  • (Boolean)

82
83
84
# File 'lib/roebe/classes/download_from_this_url.rb', line 82

def remote_dataset?
  @remote_dataset
end

#resetObject

#

reset (reset tag)

#

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/roebe/classes/download_from_this_url.rb', line 58

def reset
  super()
  @remote_dataset = nil # This holds the content of the remote website.
  # ======================================================================= #
  # === @filter_for
  #
  # If the following instance variable is set to nil, then we will
  # NOT filter for anything.
  # ======================================================================= #
  @filter_for = [
    :pdf,
    :csv,
    :gz,
    :txt,
    :bz2,
    :"tar.bz2",
    :"tar.gz",
    :"tar.xz"
  ] # Filter for these file types.
end

#runObject

#

run (run tag)

#

193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/roebe/classes/download_from_this_url.rb', line 193

def run
  menu
  obtain_all_entries_from_the_remote_website
  # ======================================================================= #
  # Next, we must determine what to do with this dataset. For now
  # we have it hardcoded that we will simply download all entries
  # on the website that have a href tag.
  # ======================================================================= #
  if @remote_dataset
    scanned = @remote_dataset.scan(/<a href=".+"/)
    if @filter_for
      e 'We will filter for these file types: '+
        royalblue(
          @filter_for.join(', ')
        )
      do_filter(scanned)
    end
    scan_for_proper_entries
  end
end

#scan_for_proper_entriesObject

#

scan_for_proper_entries

#

147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/roebe/classes/download_from_this_url.rb', line 147

def scan_for_proper_entries
  _ = main_dataset?
  # ======================================================================= #
  # We will try to obtain all results between href entries.
  # ======================================================================= #
  scanned = _.map {|line|
    line.scan(NON_GREEDY_REGEX_FOR_A_HREF_LINKS).flatten # Use a non-greedy regex.
  }.flatten
  if scanned.empty?
    e tomato('No files were found that fit.')
    e 'The regex we used for this scan was: '+sfancy(regex?)
    e
    e 'The main dataset was:'
    cliner
    pp _
    cliner
  else
    download_these_files(scanned)
  end
end

#set_remote_dataset(i) ⇒ Object

#

set_remote_dataset

#

105
106
107
# File 'lib/roebe/classes/download_from_this_url.rb', line 105

def set_remote_dataset(i)
  @remote_dataset = i
end

#set_url(i = '') ⇒ Object

#

set_url

#

89
90
91
92
93
# File 'lib/roebe/classes/download_from_this_url.rb', line 89

def set_url(i = '')
  i = i.first if i.is_a? Array
  i = i.to_s.dup
  @url = i
end

#url?Boolean

#

url?

#

Returns:

  • (Boolean)

98
99
100
# File 'lib/roebe/classes/download_from_this_url.rb', line 98

def url?
  @url
end