Class: Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/get_tapas/downloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options)
  @html = html
  @options = options
end

Instance Attribute Details

#htmlObject

Returns the value of attribute html.



3
4
5
# File 'lib/get_tapas/downloader.rb', line 3

def html
  @html
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/get_tapas/downloader.rb', line 3

def options
  @options
end

Instance Method Details

#callObject



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 = options.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 options.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 options.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 options.sleep_interval && (! this_was_the_last_one)
        puts "Sleeping #{options.sleep_interval} seconds..."
        sleep(options.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 = options.min_episode_num
  max = options.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


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