Class: DropSync::Mecha

Inherits:
Object
  • Object
show all
Defined in:
lib/dropsync/mecha.rb

Class Method Summary collapse

Class Method Details

.automatic_download(path, url) ⇒ Object



7
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
# File 'lib/dropsync/mecha.rb', line 7

def self.automatic_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
        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