Class: Downloader
- Inherits:
-
Object
- Object
- Downloader
- Defined in:
- lib/get_tapas/downloader.rb
Instance Attribute Summary collapse
-
#html ⇒ Object
Returns the value of attribute html.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #call ⇒ Object
- #download_file(link, data_dir) ⇒ Object
- #download_file_info(links) ⇒ Object
- #ensure_output_dir_exists(dir) ⇒ Object
- #episode_num_ok(episode_num) ⇒ Object
- #filespecs_available(data_dir, links) ⇒ Object
- #filespecs_needing_download(available, present) ⇒ Object
- #filespecs_present(data_dir) ⇒ Object
- #get_link_info(html, dir) ⇒ Object
-
#initialize(html, options) ⇒ Downloader
constructor
A new instance of Downloader.
Constructor Details
#initialize(html, options) ⇒ Downloader
Returns a new instance of Downloader.
5 6 7 8 |
# File 'lib/get_tapas/downloader.rb', line 5 def initialize(html, ) @html = html @options = end |
Instance Attribute Details
#html ⇒ Object
Returns the value of attribute html.
3 4 5 |
# File 'lib/get_tapas/downloader.rb', line 3 def html @html end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/get_tapas/downloader.rb', line 3 def @options end |
Instance Method Details
#call ⇒ Object
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 104 105 106 107 |
# File 'lib/get_tapas/downloader.rb', line 78 def call data_dir = .data_dir ensure_output_dir_exists(data_dir) links = get_link_info(html, data_dir) _available, _present, absent = download_file_info(links) absent.reverse! if .reverse absent_within_desired_range = absent.select { |a| episode_num_ok(a.episode_num)} num_downloads_to_do = absent_within_desired_range.size if num_downloads_to_do == 0 puts "No files within the selection criteria needed to be downloaded." elsif .no_op puts "No-op mode requested. The following #{num_downloads_to_do} file(s) would have been downloaded without it:\n\n" puts absent_within_desired_range.map(&:filename).join("\n"); puts exit(0) else absent_within_desired_range.each_with_index do |link, index| download_file(link, data_dir) this_was_the_last_one = (index == num_downloads_to_do - 1) if .sleep_interval && (! this_was_the_last_one) puts "Sleeping #{.sleep_interval} seconds..." sleep(.sleep_interval) end end end end |
#download_file(link, data_dir) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/get_tapas/downloader.rb', line 65 def download_file(link, data_dir) puts "Downloading #{link.filespec}..." tempfilespec = File.join(data_dir, 'tempfile') `curl -o #{tempfilespec} #{Shellwords.shellescape(link.url)}` if $?.exitstatus == 0 FileUtils.mv(tempfilespec, link.filespec) puts "Finished downloading #{link.filename}\n\n" else raise "Curl Download failed with exit status #{$0.exitstatus}." end end |
#download_file_info(links) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/get_tapas/downloader.rb', line 48 def download_file_info(links) present, absent = links.partition do |link| File.file?(link.filespec) end [links, present, absent] end |
#ensure_output_dir_exists(dir) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/get_tapas/downloader.rb', line 20 def ensure_output_dir_exists(dir) return if Dir.exists?(dir) begin FileUtils.mkdir_p(dir) puts "Created output data directory #{dir}." rescue Errno::EACCES puts "Unable to create directory #{dir}. Exiting..." exit(-1) end end |
#episode_num_ok(episode_num) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/get_tapas/downloader.rb', line 10 def episode_num_ok(episode_num) min = .min_episode_num max = .max_episode_num return false if min && episode_num < min return false if max && episode_num > max true end |
#filespecs_available(data_dir, links) ⇒ Object
32 33 34 35 |
# File 'lib/get_tapas/downloader.rb', line 32 def filespecs_available(data_dir, links) filenames = links.map { |link| link.filename} filenames.map { |fn| File.join(data_dir, fn) } end |
#filespecs_needing_download(available, present) ⇒ Object
43 44 45 |
# File 'lib/get_tapas/downloader.rb', line 43 def filespecs_needing_download(available, present) available - present end |
#filespecs_present(data_dir) ⇒ Object
38 39 40 |
# File 'lib/get_tapas/downloader.rb', line 38 def filespecs_present(data_dir) Dir["#{data_dir}/*"] end |
#get_link_info(html, dir) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/get_tapas/downloader.rb', line 56 def get_link_info(html, dir) links = PageParser.parse(html) links.each do |link| link.filespec = File.join(dir, link.filename) link.episode_num = link.filename.split('-').first.to_i end end |