secret
anonymous / HugoFigueiredo
Last active

Hugo Figueiredo Solution

  • Download Gist
HugoFigueiredo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
#
# [Author] HugoLnx/HugoLinux
# [Version] 0.0.1.0
#
require 'optparse'
require 'jcode'
class String
def is_number?
s = self
s = self[0...s.size - 1] if s[(s.size - 1)..s.size] == "\n"
return false if s == ''
s.each_char{|char|
return false if !('0'..'9').any?{|num| num == char}
}
return true
end
end
class SubTitle
def initialize(file)
@composition = []
lines = file.readlines
i_line = 0
i_block = 0
while lines[i_line].to_i == 0
i_line += 1
end
while i_line < lines.size
block = {}
block[:id] = lines[i_line]
i_line += 1
block[:time_range] = TimeRange.new(lines[i_line])
i_line += 1
texts = []
while i_line < lines.size and !lines[i_line].is_number?
texts << lines[i_line]
i_line += 1
end
block[:texts] = texts
@composition[i_block] = block
i_block += 1
end
end
def to_file(path)
file = File.new(path,'w')
@composition.each{|block|
file << (block[:id])
file << block[:time_range].to_s + "\n"
block[:texts].each{|line| file << line}
}
end
def add(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
@composition.each{|block|
block[:time_range].add(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
}
end
def sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
warning = false
@composition.each{|block|
block[:time_range].sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
warning = true if block[:time_range].init_time.hour < 0 or
block[:time_range].final_time.hour < 0
}
puts 'WARNING: Some times are negative' if warning
end
end
class Time
def initialize(time_s)
time_s[8] = '.'
@hour = time_s.slice(0..1).to_i
@min = time_s.slice(3..4).to_i
@sec = time_s.slice(6..7).to_i
@millisec = time_s.slice(9..11).to_i
end
def to_s
return "%.2d:%.2d:%.2d,%.3d" % [@hour,@min,@sec,@millisec]
end
def add(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
@hour += qnt_hour
@min += qnt_min
@sec += qnt_sec
@millisec += qnt_millisec
while @millisec >= 1000
@millisec -= 1000
@sec += 1
end
while @sec >= 60
@sec -= 60
@min += 1
end
while @min >= 60
@min -= 60
@hour += 1
end
end
def sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
@hour -= qnt_hour
@min -= qnt_min
@sec -= qnt_sec
@millisec -= qnt_millisec
while @millisec < 0
@millisec += 1000
@sec -= 1
end
while @sec < 0
@sec += 60
@min -= 1
end
while @min < 0
@min += 60
@hour -= 1
end
end
end
class TimeRange
attr_accessor :init_time
attr_accessor :final_time
def initialize(time_range_s)
@init_time = Time.new(time_range_s.slice(0..11))
@final_time = Time.new(time_range_s.slice(17..28))
end
def to_s
return @init_time.to_s + ' --> ' + @final_time.to_s
end
def add(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
@init_time.add(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
@final_time.add(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
end
def sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
@init_time.sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
@final_time.sub(qnt_hour,qnt_min,qnt_sec,qnt_millisec)
end
end
class OptParser < OptionParser
attr :opts
alias ori_initialize initialize
def initialize
ori_initialize
@opts = {}
declare_options
end
def declare_options
def_option('--operation'){
@opts[:oper] = true
}
def_option('--time'){
@opts[:time] = true
}
parse!
end
end
class TimeArgument
attr :hour
attr :min
attr :sec
attr :millisec
def initialize(arg)
hour_s = ''
min_s = ''
sec_s = ''
millisec_s = ''
arg.reverse!
while arg[0].chr != ','
millisec_s += arg[0].chr
arg[0] = ''
return if arg[0] == nil
end
arg[0] = ''
return if arg[0] == nil
while arg[0].chr != ','
sec_s += arg[0].chr
arg[0] = ''
return if arg[0] == nil
end
arg[0] = ''
return if arg[0] == nil
while arg[0].chr != ','
min_s += arg[0].chr
arg[0] = ''
return if arg[0] == nil
end
arg[0] = ''
return if arg[0] == nil
while arg[0].chr != ','
hour_s += arg[0].chr
arg[0] = ''
return if arg[0] == nil
end
arg[0] = ''
ensure
hour_s.reverse!
min_s.reverse!
sec_s.reverse!
millisec_s.reverse!
@hour = hour_s.to_i
@min = min_s.to_i
@sec = sec_s.to_i
@millisec = millisec_s.to_i
end
end
begin
optparser = OptParser.new rescue(
puts 'Incorrect Option(s)'
sleep 1
exit
)
if optparser.opts[:oper] and optparser.opts[:time]
puts 'Loading subtitle'
input_file = File.open(ARGV[2],'r')
subtitle = SubTitle.new(input_file)
puts 'Changing the times of subtitle'
time_arg = TimeArgument.new(ARGV[1].dup)
eval("subtitle.#{ARGV[0]}(#{time_arg.hour},#{time_arg.min},#{time_arg.sec},#{time_arg.millisec})")
puts "Creating #{ARGV[3]}"
subtitle.to_file(ARGV[3])
puts "'#{ARGV[3]}' has been created"
end rescue(
puts 'Incorrect Argument(s)'
sleep 1
)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.