Module: Booru::Utils

Included in:
Downloader
Defined in:
lib/booru/utils.rb

Instance Method Summary collapse

Instance Method Details

#clean_filename(filename) ⇒ Object



5
6
7
# File 'lib/booru/utils.rb', line 5

def clean_filename(filename)
  filename.gsub(' ', '_').gsub(/[^a-zA-Z0-9_.]+/, '-')
end

#curl(url, output, user_agent, referer) ⇒ Object



69
70
71
72
73
# File 'lib/booru/utils.rb', line 69

def curl(url, output, user_agent, referer)
  command = "curl -A #{user_agent} -e #{referer} --progress-bar "\
            "-o #{output} #{url}"
  system(command)
end

#get_md5_hash(file) ⇒ Object



34
35
36
37
# File 'lib/booru/utils.rb', line 34

def get_md5_hash(file)
  string = File.read(file)
  hash = Digest::MD5.hexdigest(string)
end

#reduce_filename(filename, max = 255) ⇒ Object

TODO Rather than just reduce filename size by removing characters, try to not to clip tags in the filename



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

def reduce_filename(filename, max = 255)
  if filename.length > max
    suffix   = File.extname(filename)
    basename = filename.chomp(suffix)

    size = max.to_i - suffix.length
    shortname = basename[0..size]

    [shortname, suffix].join('')
  else
    filename
  end
end

#sanitize(values) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/booru/utils.rb', line 25

def sanitize(values)
    values = values.gsub(" ", "+") if values.is_a?(String)
    if values.is_a?(Array)
      values.each { |value| value.gsub(' ', '_') }
      values = values.join("+")
    end
    values
end

#transfer(url, output, user_agent, referer) ⇒ Object

File download wrapper



53
54
55
56
57
58
59
60
61
# File 'lib/booru/utils.rb', line 53

def transfer(url, output, user_agent, referer)
  if which('wget')
    wget(url, output, user_agent, referer)
  elsif which('curl')
    curl(url, output, user_agent, referer)
  else
    write(url, output, user_agent, referer)
  end
end

#wget(url, output, user_agent, referer) ⇒ Object



63
64
65
66
67
# File 'lib/booru/utils.rb', line 63

def wget(url, output, user_agent, referer)
  command = "wget -nv #{url} -O #{output} "\
            "--user-agent=#{user_agent} --referer=#{referer}"
  system(command)
end

#which(cmd) ⇒ Object

Cross-platform ‘which’ stackoverflow.com/a/5471032



41
42
43
44
45
46
47
48
49
50
# File 'lib/booru/utils.rb', line 41

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = "#{path}/#{cmd}#{ext}"
      return exe if File.executable? exe
    }
  end
  return nil
end

#write(url, output, user_agent, referer) ⇒ Object



75
76
77
78
# File 'lib/booru/utils.rb', line 75

def write(url, output, user_agent, referer)
  data = open(url, "User-Agent" => user_agent, "Referer" => referer).read
  File.open(output, "wb") { |file| file.write(data) }
end