Class: TorS::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/tors/search.rb

Instance Method Summary collapse

Constructor Details

#initialize(query = '', from = 'katcr', auto = false, directory = Dir.pwd) ⇒ Search

Returns a new instance of Search.



10
11
12
13
14
15
16
17
18
19
# File 'lib/tors/search.rb', line 10

def initialize(query = '', from = 'katcr', auto = false, directory = Dir.pwd)
  @provider = YAML.load_file(File.expand_path("../../../providers/#{from}.yml", __FILE__))
  @query = query
  @from = from
  @auto = auto
  @directory = directory

  check_download_directory
  scrape
end

Instance Method Details

#download(choice) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tors/search.rb', line 96

def download(choice)
  if choice[:url] =~ /^magnet:\?/
    puts '😏  Sorry, I can\'t start automatically magnet links. Please use this in your torrent client.'
    puts choice[:url]
  else
    begin
      source            = Net::HTTP.get(URI.parse(choice[:url]))
      target_file_name  = choice[:name].tr("\n", ' ').squeeze(' ').strip + '.torrent'
      target_file       = File.join(@directory, target_file_name)
      puts 'Downloading ' + target_file_name
      File.write(target_file, source)
    rescue IOError => e
      puts '😵  There is an error! ' + e.message + ' Here: L108'
    ensure
      puts '🥂  Downloaded!'
    end
  end
end

#promptObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tors/search.rb', line 84

def prompt
  prompt = TTY::Prompt.new(interrupt: :exit)
  choice = prompt.ask("Which torrent you want to download? (1..#{@downloads.size} or ctrl+c/cmd+c for interrupt)",
                      convert: :int,
                      default: 1) do |c|
    c.in "1-#{@downloads.size}"
  end

  c = @downloads.find { |v| v[:key] == choice.to_i }
  download c
end

#resultsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/tors/search.rb', line 68

def results
  puts 'Results for : ' + @query
  puts 'From : ' + @from

  table = TTY::Table.new %i[# Category Title Size Seed Leech], @rows
  puts table.render(:unicode, padding: [0, 1, 0, 1]) do |renderer|
    renderer.border.style = :green
  end

  if @auto
    download @downloads.find { |v| v[:key] == 1 }
  else
    prompt
  end
end

#scrapeObject



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
# File 'lib/tors/search.rb', line 21

def scrape
  @url = @provider['url'].gsub(/%{(\w+)}/, @query ? @query.tr(' ', '+') : '')
  @page = Nokogiri::HTML(open(@url))

  if @page.css(@provider['scrape']['selector']).empty?
    if threat_defence @page
      puts '😰  Sorry, I think you banned from ' + @from + '. There is a threat defense redirection.'
    end

    puts 'Please check this url is works : ' + @url
    return
  end

  @rows = []
  @downloads = []

  puts 'Scrabing...'

  @page.css(@provider['scrape']['selector']).each_with_index do |row, key|
    hash = {
      key: key + 1,
      name: row.search(@provider['scrape']['data']['name']).text,
      url: ''
    }
    if @provider['scrape']['data']['download'].is_a?(String)
      hash[:url] = @provider['download_prefix'] + row.search(@provider['scrape']['data']['download']).first['href']
    else
      @subpage = Nokogiri::HTML(open(@provider['download_prefix'] + row.css(@provider['scrape']['data']['download']['url']).first['href']))

      hash[:url] = @subpage.css(@provider['scrape']['data']['download']['selector']).first['href']
    end

    @downloads << hash

    @rows << [
      (key + 1).to_s,
      !@provider['scrape']['data']['category'].empty? ? row.css(@provider['scrape']['data']['category']).text.tr("\n", ' ').squeeze(' ').strip : '',
      !@provider['scrape']['data']['name'].empty? ? row.css(@provider['scrape']['data']['name']).text.strip : '',
      !@provider['scrape']['data']['size'].empty? ? row.css(@provider['scrape']['data']['size']).text.strip : '',
      !@provider['scrape']['data']['seed'].empty? ? row.css(@provider['scrape']['data']['seed']).text.strip.green : '',
      !@provider['scrape']['data']['leech'].empty? ? row.css(@provider['scrape']['data']['leech']).text.strip.red : ''
    ]
  end

  results
end

#threat_defence(page) ⇒ Object



115
116
117
118
# File 'lib/tors/search.rb', line 115

def threat_defence(page)
  return false unless page.text =~ /threat_defence.php/
  true
end