class SubtitleIt::Subdownloader

Public Instance Methods

down_a_sub(sub, dst_format) click to toggle source
# File lib/subtitle_it/bin.rb, line 43
def down_a_sub(sub, dst_format)
  dst_format ||= sub.format
  dump = @down.download_subtitle(sub)
  if sub.format != dst_format
    subt = Subtitle.new(dump: dump, format: sub.format)
    dump = subt.send :"to_#{dst_format}"
  end
  fname = @movie.filename[0..-4] + sub.info['SubLanguageID']
  Bin.write_out("#{fname}.#{dst_format}", dump)
end
parse_input(input) click to toggle source
# File lib/subtitle_it/bin.rb, line 63
def parse_input(input)
  choose = input.split(' ').map do |c|
    if c =~ /-/
      numbers = c.split('-').map(&:to_i)
      (numbers[0]..numbers[1]).to_a
    else
      c.to_i
    end
  end
  choose.flatten.uniq
end
print_option(r, index) click to toggle source
run!(movie, lang = nil, dst_format = nil, first = false) click to toggle source
# File lib/subtitle_it/bin.rb, line 16
def run!(movie, lang = nil, dst_format = nil, first = false)
  @movie = Movie.new(movie)
  @down = Subdown.new
  @down.log_in!
  res = @down.search_subtitles(@movie, lang).sort
  if res.length == 0
    puts 'No results found.'
    return
  end
  unless first
    puts "Found #{res.size.to_s.yellow} result#{'s' if res.size > 1}:\n"
    res.each_with_index { |r, i| puts print_option(r.info, i) }
    puts 'You can choose multiple ones separated with spaces: 1 3 5 '
    puts 'Or a range separated with a hifen: 3-5'
    STDOUT.print "Choose (1..#{res.size}): "
    choose = parse_input(STDIN.gets.chomp)
    choose = choose.map { |c| res[c.to_i - 1] }
  else
    choose = [res.first]
  end
  puts "Downloading #{choose.size} subtitle#{'s' if choose.size > 1}..."
  choose.each do |sub|
    down_a_sub(sub, dst_format)
  end
  @down.log_out!
end