Class: WebsiteCloner::Downloader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, output_dir, session_cookie = nil) ⇒ Downloader

Returns a new instance of Downloader.



10
11
12
13
14
15
16
17
18
# File 'lib/website_cloner/downloader.rb', line 10

def initialize(base_url, output_dir, session_cookie = nil)
  @base_url = URI.parse(base_url)
  @output_dir = output_dir
  @session_cookie = session_cookie
  FileUtils.mkdir_p(@output_dir)
  FileUtils.mkdir_p(File.join(@output_dir, 'assets'))
  FileUtils.mkdir_p(File.join(@output_dir, 'css'))
  FileUtils.mkdir_p(File.join(@output_dir, 'js'))
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



8
9
10
# File 'lib/website_cloner/downloader.rb', line 8

def base_url
  @base_url
end

#output_dirObject (readonly)

Returns the value of attribute output_dir.



8
9
10
# File 'lib/website_cloner/downloader.rb', line 8

def output_dir
  @output_dir
end

Returns the value of attribute session_cookie.



8
9
10
# File 'lib/website_cloner/downloader.rb', line 8

def session_cookie
  @session_cookie
end

Instance Method Details

#download_asset(url, type) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/website_cloner/downloader.rb', line 45

def download_asset(url, type)
  Utils.logger.info "Downloading asset: #{url}"
  uri = URI.parse(URI.join(@base_url, url))
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request_path = uri.path.empty? ? '/' : uri.path
  request_path += "?#{uri.query}" if uri.query

  request = Net::HTTP::Get.new(request_path)
  request['Cookie'] = @session_cookie if @session_cookie

  response = http.request(request)

  case response
  when Net::HTTPSuccess
    content = response.body
    filename = File.basename(uri.path).gsub(/^[0-9a-f]+_/, '')
    filename = URI.decode_www_form_component(filename).gsub('%20', '-')
    dir = case type
          when 'css' then 'css'
          when 'js' then 'js'
          else 'assets'
          end
    path = File.join(@output_dir, dir, filename)
    FileUtils.mkdir_p(File.dirname(path))

    File.open(path, 'wb') do |file|
      file.write(content)
    end

    "#{dir}/#{filename}"
  else
    Utils.logger.warn "Failed to download asset: #{url}"
    url # Return the original URL if download fails
  end
end

#download_page(url) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/website_cloner/downloader.rb', line 20

def download_page(url)
  Utils.logger.info "Downloading page: #{url}"
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = (uri.scheme == 'https')
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request_path = uri.path.empty? ? '/' : uri.path
  request_path += "?#{uri.query}" if uri.query

  request = Net::HTTP::Get.new(request_path)
  request['Cookie'] = @session_cookie if @session_cookie

  response = http.request(request)

  case response
  when Net::HTTPSuccess
    response.body
  when Net::HTTPRedirection
    download_page(response['location'])
  else
    response.error!
  end
end