class Tsearch::Commands::Search

Constants

CATEGORY_MAP

Public Class Methods

new(options) click to toggle source
# File lib/tsearch/commands/search.rb, line 21
def initialize(options)
  @options = options
end

Public Instance Methods

execute(_input: $stdin, output: $stdout) click to toggle source
# File lib/tsearch/commands/search.rb, line 25
def execute(_input: $stdin, output: $stdout)
  results = search_torrent.map(&:deep_symbolize_keys)
  if results.empty?
    output.puts 'No results were found, please try again'
    return
  end

  display_results(results.reverse, output)
end

Private Instance Methods

display_results(results, output) click to toggle source
# File lib/tsearch/commands/search.rb, line 73
def display_results(results, output)
  results.each do |result|
    output.puts "Name: #{result[:title]}"
    output.puts "Category: #{result[:category]}"
    output.puts "Magnet: #{format_link(result[:download])}"
    unless verbose
      output.puts
      next
    end

    output.puts "Seeders: #{result[:seeders]}"
    output.puts "Leechers: #{result[:leechers]}"
    output.puts "Filesize: #{filesize(result[:size])}"
    output.puts "Ranked: #{result[:ranked] == 1 ? 'Yes' : 'No'}"
    output.puts "Info page: #{format_link(result[:info_page])}"
    output.puts
  end
end
filesize(size) click to toggle source
# File lib/tsearch/commands/search.rb, line 61
def filesize(size)
  units = ['B', 'KB', 'MB', 'GB', 'TB', 'Pb', 'EB']

  return '0.0 B' if size == 0

  exp = (Math.log(size) / Math.log(1024)).to_i
  exp += 1 if size.to_f / 1024**exp >= 1024 - 0.05
  exp = 6 if exp > 6

  '%.1f %s' % [size.to_f / 1024**exp, units[exp]]
end
method_missing(key, *args, &block) click to toggle source
Calls superclass method
# File lib/tsearch/commands/search.rb, line 92
def method_missing(key, *args, &block)
  super unless %i[string
                  imdb
                  tvdb
                  themoviedb
                  category
                  limit
                  min_seeders
                  min_leechers
                  ranked
                  sort
                  verbose].include?(key)

  @options[key]
end
rarbg() click to toggle source
# File lib/tsearch/commands/search.rb, line 37
def rarbg
  @rarbg ||= RARBG::API.new
end
search_torrent() click to toggle source
# File lib/tsearch/commands/search.rb, line 41
def search_torrent
  query = {
    string: string,
    imdb: imdb,
    tvdb: tvdb,
    category: CATEGORY_MAP[category],
    limit: limit,
    format: :json_extended,
    min_seeders: min_seeders,
    min_leechers: min_leechers,
    ranked: ranked,
    sort: sort
  }.compact
  rarbg.search(query)
end