bin/addic7ed
#!/usr/bin/env ruby
def require_dependencies
require 'optparse'
require 'oga'
require 'addic7ed'
end
begin
require_dependencies # People don't all use rubygems, you know...
rescue LoadError
require 'rubygems' # But most do :-)
require_dependencies
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: addic7ed [options] <file1> [<file2>, <file3>, ...]"
opts.on("-l [LANGUAGE]", "--language [LANGUAGE]", "Language code to look subtitles for (default: French)") do |l|
options[:language] = l
end
opts.on("--no-hi", "Only download subtitles without Hearing Impaired lines") do |hi|
options[:no_hi] = !hi
end
opts.on("-a", "--all-subtitles", "Display all available subtitles") do |a|
options[:all] = a
end
opts.on("-n", "--do-not-download", "Do not download the subtitle") do |n|
options[:nodownload] = n
end
opts.on("-f", "--force", "Overwrite existing subtitle") do |f|
options[:force] = f
end
opts.on("-u", "--untagged", "Do not include language code in subtitle filename") do |u|
options[:untagged] = u
end
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options[:verbose] = v
end
opts.on("-q", "--quiet", "Run without output (cron-mode)") do |q|
options[:quiet] = q
end
opts.on("-d", "--debug", "Debug mode [do not use]") do |d|
options[:debug] = d
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("-L", "--list-languages", "List all available languages") do
puts "All available languages (with their corresponding ISO code):"
Addic7ed::LANGUAGES.each do |lang, infos|
puts "#{lang}:\t#{infos[:name]}"
end
exit
end
opts.on_tail("-V", "--version", "Show version number") do
puts "This is addic7ed-ruby version #{Addic7ed::VERSION} by Michael Baudino (https://github.com/michaelbaudino)"
puts "Licensed under the terms of the MIT License"
exit
end
end.parse!
options[:filenames] = ARGV
options[:language] ||= 'fr'
# Main loop over mandatory arguments (e.g. filenames)
options[:filenames].each do |filename|
unless File.file? filename or options[:debug]
puts "Warning: #{filename} does not exist or is not a regular file. Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
end
begin
ep = Addic7ed::Episode.new(filename, options[:untagged])
puts "Searching subtitles for #{ep.video_file.basename}" if options[:verbose]
if File.file?(filename.gsub(/\.\w{3}$/, '.srt')) and not options[:force]
puts "A subtitle already exists (#{filename.gsub(/\.\w{3}$/, '.srt')}). Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
end
puts ep.video_file.inspect.gsub(/^/, ' ') if options[:verbose]
ep.subtitles(options[:language])
if options[:all] or options[:verbose]
puts 'Available subtitles:'.gsub(/^/, options[:verbose] ? ' ' : '')
ep.subtitles(options[:language]).each do |sub|
puts "#{sub}".gsub(/^/, options[:verbose] ? ' ' : ' ')
end
next if options[:all]
end
ep.best_subtitle(options[:language], options[:no_hi])
if options[:verbose]
puts ' Best subtitle:'
puts " #{ep.best_subtitle(options[:language])}"
end
unless options[:nodownload]
ep.download_best_subtitle!(options[:language], options[:no_hi])
puts "New subtitle downloaded for #{filename}.\nEnjoy your show :-)".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
end
rescue Addic7ed::InvalidFilename
puts "#{filename} does not seem to be a valid TV show filename. Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
rescue Addic7ed::ShowNotFound
puts "Show not found on Addic7ed : #{ep.video_file.filename}. Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
rescue Addic7ed::EpisodeNotFound
puts "Episode not found on Addic7ed : #{ep.video_file.filename}. Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
rescue Addic7ed::LanguageNotSupported
puts "Addic7ed does not support language '#{options[:language]}'. Exiting.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
break
rescue Addic7ed::ParsingError
puts "HTML parsing failed. Either you've found a bug (please submit an issue) or Addic7ed website has been updated and cannot be crawled anymore (in this case, please wait for an update or submit a pull request). Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
rescue Addic7ed::NoSubtitleFound
puts "No (acceptable) subtitle has been found on Addic7ed for #{filename}. Maybe try again later.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
rescue Addic7ed::DownloadError
puts "The subtitle could not be downloaded. Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
rescue Addic7ed::DownloadLimitReached
puts "You exceeded your daily download count. Exiting.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
break
rescue Addic7ed::SubtitleCannotBeSaved
puts "The downloaded subtitle could not be saved as #{filename.gsub(/\.\w{3}$/, '.srt')}. Skipping.".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
rescue Addic7ed::HTTPError => e
puts "Network error: #{e.message}".gsub(/^/, options[:verbose] ? ' ' : '') unless options[:quiet]
next
end
end