Module: DropSync::Auto

Defined in:
lib/dropsync/auto.rb

Class Method Summary collapse

Class Method Details

.download(path, url) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dropsync/auto.rb', line 8

def download(path, url)
  agent = Mechanize.new do |agent|
    agent.user_agent_alias = 'Mac Safari'
  end

  # Gets download link
  page = agent.get(url)
  link = page.uri.to_s.gsub('dl=0', 'dl=1')

  # Creates directory
  download_path = "#{File.expand_path('~')}/Downloads"
  FileUtils.cd(download_path)
  path.split('/').each do |d|
    begin
      FileUtils.mkdir(d)
    rescue StandardError
      puts "> Directory '#{d}' already exists"
      FileUtils.rm_rf(d)
      FileUtils.mkdir(d)
    end
    FileUtils.cd(d)
  end
  filename = path.split('/').pop

  # Download
  system("curl -L -s -o #{download_path}/#{path}/#{filename}.zip #{link}")
  system("unzip #{download_path}/#{path}/#{filename}.zip > /dev/null 2>&1")

  # Cleans directory
  FileUtils.rm_rf('__MACOSX')
  FileUtils.rm_rf("#{filename}.zip")
end