Class: Subfinder::Parser::Download

Inherits:
Object
  • Object
show all
Defined in:
lib/subfinder/parser/download.rb

Overview

Download subtitle files from Internet

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Download

Returns a new instance of Download.



7
8
9
# File 'lib/subfinder/parser/download.rb', line 7

def initialize(url)
  @url = url
end

Instance Method Details

#response_is_healthy?(res) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/subfinder/parser/download.rb', line 25

def response_is_healthy?(res)
  if res.code != 200
    Logger.info "Error when downloading '#{@url}'\n resposnse code: #{res.code}\n".red
    return false
  elsif res.body.include? 'An error occurred while processing your request.'
    Logger.info "Error downloading '#{@url}'. Try Again in few seconds".red
    return false
  else
    true
  end
end

#saveObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/subfinder/parser/download.rb', line 11

def save
  res = RestClient.get @url
  return false unless  @url =~ URI::DEFAULT_PARSER.make_regexp
  return false unless response_is_healthy? res

  file_name = res.headers[:content_disposition].split('=')[1]
  File.write("#{Config.working_dir}/#{file_name}", res.body)
  Logger.info "Downloaded to #{Config.working_dir}/#{file_name}"
  true
rescue StandardError => e
  Logger.info "Error when downloading '#{@url}'\n Error message: #{e}\n".red
  false
end