Module: EbookRenamer

Defined in:
lib/ebook_renamer/cli.rb,
lib/ebook_renamer/errors.rb,
lib/ebook_renamer/version.rb,
lib/ebook_renamer/ebook_renamer.rb

Defined Under Namespace

Modules: Errors Classes: CLI

Constant Summary collapse

VERSION =
"0.2.5"

Instance Method Summary collapse

Instance Method Details

#formatted_name(meta_hash = {}, fields = {}) ⇒ Object

Return formatted file name using the metadata values

Parameters:

  • meta_hash (Hash<Symbol,String>) (defaults to: {})

    output from the program ‘ebook-meta’ or ‘exiftoo’

  • fields (Hash<Symbol,String>) (defaults to: {})

    list of fields that will be used to set the name



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

def formatted_name(meta_hash = {}, fields = {})
  if hash.nil? || fields.nil?
    fail ArgumentError.new("Argument must not be nil")
  end
  # Let's not continue if we have no title metadata
  fail "No title found" unless meta_hash.fetch("title", nil)

  # The keys that we get from the 'mdls' or 'exiftool'
  args = {
    keys: [
      "title",
      "author(s)"
    ],
    sep_char: " "
  }.merge(fields)

  keys = args[:keys]
  sep_char = args[:sep_char]

  # Note: only show if we have the value for title
  result = []
  keys.each do |key|
    value = meta_hash.fetch(key, nil)
    # Note: don't add 'Author(s)' => 'Unknown' to keep the result clean
    if value && value.downcase != "unknown"
      result << meta_hash[key]
    end
  end
  result.join(sep_char)
end

#meta(filename, binary = "ebook-meta") ⇒ String

Extract meta data from the input file using the ebook-meta tool

Parameters:

  • filename (String)

    the input file name

  • binary (String) (defaults to: "ebook-meta")

    the executable for use to extract the metadata

Returns:

  • (String)

    result of the output from running the command



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/ebook_renamer/ebook_renamer.rb', line 7

def meta(filename, binary = "ebook-meta")
  fail Errors::EbookMetaNotInstall, "Need to install ebook-meta to use this gem" if AgileUtils::Helper.which(binary).nil?
  command = [
    binary,
    Shellwords.escape(filename)
  ]

  stdout_str, stderr_str, status = Open3.capture3(command.join(" "))
  fail "Problem processing #{filename}" unless status.success?
  stdout_str
end

#meta_to_hash(text) ⇒ Hash<String,String>

Convert the output string to hash

Parameters:

  • text (String)

    output string from the ‘ebook-meta’ command

Returns:

  • (Hash<String,String>)

    hash pair for the input string



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

def meta_to_hash(text)
  hash = {}
  return hash if text.nil?
  result_list = []

  text.split(/\n/).each do |meta|
    # split by the first ':' string
    list = meta.split /^(.*?):/

    # ignore the empty string element
    list.delete_at(0)

    unless list.empty?
      list.map(&:strip!)
      # downcase the first item to make it easy
      result_list << [list[0].downcase, list[1]]
      hash = Hash[*result_list.flatten]
    end
  end
  hash
end