Sha256: ff9d30effdd368e65c2f8d5d28e8ee979ea0314d71c88b3720591b6f36dc3de8

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

#!/usr/bin/env ruby

require 'popcorntime_search'
require 'highline/import'
require 'optparse'

options = {}

optparse = OptionParser.new do |opts|
  opts.banner = 'Usage: popcorn [options] <search>'

  opts.on('-y', '--auto', 'Pick best match automatically') do
    options[:auto] = true
  end

  opts.on('-v', '--version', 'Print version and exit') do
    puts "popcorn v#{PopcorntimeSearch::VERSION}"
    exit
  end

  opts.on('-h', '--help', 'Show this message') do
    puts opts
    exit
  end
end.parse!

# Join arguments to create the search term
options[:search] = optparse.join(' ')

begin
  HighLine.colorize_strings

  if options[:search].empty?
    say 'Please type a search'.red
    exit
  end

  search = PopcorntimeSearch::Search.new(options[:search])

  unless search.results_found?
    say 'No results found'.red
    exit
  end

  # If 'auto' or only one result, pick the first/only one
  if options[:auto] || search.results.one?
    result = search.results.first
  else
    # If there are several matches, choose the appropriate one
    say "Search results for #{options[:search].blue}:".yellow
    result = choose(*search.results) do |menu|
      menu.default = '1'
      menu.select_by = :index_or_name
    end
  end

  # Choose link
  if result.links.empty?
    say 'No results found'.red
    exit
  end

  links = result.links.sort.reverse # Sort by number of seeds desc

  if options[:auto]
    link = links.first
  else
    say "Choose a link for #{result.to_s.blue}:".yellow
    link = choose(*links) do |menu|
      menu.default = '1'
      menu.select_by = :index_or_name
    end
  end

  say link.magnet
rescue Interrupt, EOFError
  say 'Interrupted!'.red
rescue SocketError
  say "Couldn't perform action. Are you online?".red
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
popcorntime_search-1.1.0 exe/popcorn
popcorntime_search-1.0.0 exe/popcorn
popcorntime_search-0.1.0 exe/popcorn