Class: Chaptan::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/chaptan/command.rb,
lib/chaptan/command/options.rb

Overview

A command class for Chaptan

Defined Under Namespace

Modules: Options

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



13
14
15
# File 'lib/chaptan/command.rb', line 13

def initialize(argv)
  @argv = argv
end

Class Method Details

.run(argv) ⇒ Object



9
10
11
# File 'lib/chaptan/command.rb', line 9

def self.run(argv)
  new(argv).execute
end

Instance Method Details

#add_chapter(filename, chapters) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chaptan/command.rb', line 61

def add_chapter(filename, chapters)
  Mp3Info.open(filename) do |mp3info|
    unless mp3info.tag2.CHAP.nil?
      st = mp3info.tag2.CHAP[0]
      st[34] = "."
      mp3info.tag2.CHAP[0] = st
    end

    if chapters.size.positive?
      chaps = []
      ctoc = "toc1\x00".dup
      ctoc << [3, chapters.size].pack("CC")
      chapters.each_with_index do |ch, i|
        num = i + 1
        title = ch["title"]
        description = ch["description"]
        link = ch["link"]

        ctoc << "chp#{num}\x00"

        chap = "chp#{num}\x00".force_encoding("ASCII-8BIT").dup
        chap << [ch["start"] * 1000, ch["to"] * 1000].pack("NN").force_encoding("ASCII-8BIT")
        chap << "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF".dup.force_encoding("ASCII-8BIT")

        title_tag = [title.encode("utf-16")].pack("a*")
        chap << "TIT2"
        chap << [title_tag.size + 1].pack("N")
        chap << "\x00\x00\x01"
        chap = chap.force_encoding("ASCII-8BIT")
        chap << title_tag

        unless description.nil?
          description_tag = [description.encode("utf-16")].pack("a*")
          chap << "TIT3"
          chap << [description_tag.size + 1].pack("N")
          chap << "\x00\x00\x01"
          chap << description_tag
        end

        unless link.nil?
          chap << "WXXX"
          chap << [link.length + 2].pack("N")
          chap << "\x00\x00\x00#{link}\00"
        end

        chaps << chap
      end
      mp3info.tag2.CTOC = ctoc
      mp3info.tag2.CHAP = chaps
    end
  end
end

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chaptan/command.rb', line 17

def execute
  options = Options.parse!(@argv)

  mp3filename = options[:filename]
  ymlfilename = options[:yaml]
  if ymlfilename.nil?
    read_chapter(mp3filename)
  else
    chapters = load_yaml(ymlfilename)
    chapters.last["to"] = file_length(mp3filename)
    add_chapter(mp3filename, chapters)
  end
end

#file_length(filename) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/chaptan/command.rb', line 31

def file_length(filename)
  unless FileTest.exist?(filename)
    puts "Error: #{filename} does not exist."
    exit
  end
  Mp3Info.open(filename).length
end

#load_yaml(filename) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/chaptan/command.rb', line 114

def load_yaml(filename)
  yml = YAML.load_file(filename)
  (yml.length - 1).times.each do |i|
    yml[i]["to"] = yml[i + 1]["start"]
  end
  yml
end

#read_chapter(filename) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/chaptan/command.rb', line 39

def read_chapter(filename)
  unless FileTest.exist?(filename)
    puts "Error: #{filename} does not exist."
    exit
  end

  Mp3Info.open(filename) do |mp3info|
    puts mp3info
    puts "CTOC: #{mp3info.tag2.CTOC}"
    puts "TIT2: #{mp3info.tag2.TIT2}"
    puts "TPE1: #{mp3info.tag2.TPE1}"
    if mp3info.tag2.CHAP.nil?
      puts "There is no chapter information."
    else
      mp3info.tag2.CHAP.each do |chapter|
        puts "--------------------"
        puts chapter # .encode('utf-8', 'UTF-16')
      end
    end
  end
end