kat_search 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e758fc996e3383003d55a5966fab1dd7f8c5403
4
+ data.tar.gz: a17aa2b7a131faad2ae54bceb0a2c8efbc80b4ce
5
+ SHA512:
6
+ metadata.gz: 37286866788b1025534adc0c65b09ed366e23d11da1560bf7e41d080a683f759574f9452dc59a15b904ebb8524481df7647856d29cf0eca04c3b88d5f5377549
7
+ data.tar.gz: 6545b4d183d80140339b1a63bd03e9f34552a6f2119c752e2e38ba1e197e542b7a48b7601dfc322cdb2eaf89e8c4b46b6eb1e6278a29b7d1350bb425c8f6c0ff
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kat_search.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'rspec', cmd: 'bundle exec rspec', all_after_pass: false, failed_mode: :none do
2
+ watch(%r{\Aspec/.+_spec\.rb\z})
3
+ watch(%r{\Alib/(.+)\.rb\z}) { |m| "spec/lib/#{m[1]}_spec.rb" }
4
+ watch('spec/spec_helper.rb') { 'spec' }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 David Marchante
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # KatSearch
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/kat_search`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kat_search'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kat_search
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/kat_search.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'kat_search'
5
+ require 'factory_girl'
6
+
7
+ include FactoryGirl::Syntax::Methods
8
+ FactoryGirl.find_definitions
9
+
10
+ # You can add fixtures and/or initialization code here to make experimenting
11
+ # with your gem easier. You can also use a different console, if you like.
12
+ link = build :link
13
+ search = build :search
14
+
15
+ # (If you use this, don't forget to add pry to your Gemfile!)
16
+ require 'pry'
17
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/kat_search ADDED
@@ -0,0 +1,84 @@
1
+ #!/usr/bin/env ruby -w
2
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w( .. lib ))
3
+
4
+ require 'kat_search'
5
+ require 'fileutils'
6
+ require 'highline'
7
+ require 'optparse'
8
+
9
+ options = {
10
+ path: './'
11
+ }
12
+
13
+ optparse = OptionParser.new do |opts|
14
+ opts.banner = 'Usage: kat_search [options] <search>'
15
+
16
+ opts.on('-y', '--auto-download', 'Download first result without confirmation') do
17
+ options[:auto] = true
18
+ end
19
+
20
+ opts.on('-m', '--magnet', 'Print magnet link and exit') do
21
+ options[:magnet] = true
22
+ end
23
+
24
+ opts.on('-p PATH', '--path PATH', 'Path to save the file') do |p|
25
+ options[:path] = p
26
+ end
27
+
28
+ opts.on('-v', '--version', 'Print version and exit') do
29
+ puts "kat_search v#{KatSearch::VERSION}"
30
+ exit
31
+ end
32
+
33
+ opts.on_tail('-h', '--help', 'Show this message') do
34
+ puts opts
35
+ exit
36
+ end
37
+ end.parse!
38
+
39
+ # Join arguments to create the search term
40
+ options[:search] = optparse.join(' ')
41
+
42
+ # main program
43
+ begin
44
+ cli = HighLine.new
45
+ HighLine.colorize_strings # Monkey patch String for cli colors
46
+
47
+ # Valid search?
48
+ if options[:search].empty?
49
+ cli.say 'Please type a search'.red
50
+ exit
51
+ end
52
+
53
+ # Search subtitle
54
+ search = KatSearch::Search.new(options[:search])
55
+
56
+ # Results found?
57
+ unless search.results_found?
58
+ cli.say 'No results found'.red
59
+ exit
60
+ end
61
+
62
+ # Choose
63
+ if options[:auto]
64
+ torrent = search.links.first
65
+ else
66
+ cli.say "Search results for #{options[:search]}:"
67
+ torrent = cli.choose(*search.links) do |menu|
68
+ menu.default = '1'
69
+ menu.select_by = :index
70
+ end
71
+ end
72
+
73
+ # Download torrent or print magnet link
74
+ if options[:magnet]
75
+ cli.say torrent.magnet.green
76
+ else
77
+ torrent_file = torrent.download(options[:path])
78
+ cli.say "#{torrent_file} downloaded successfully!".green
79
+ end
80
+ rescue Interrupt, EOFError
81
+ cli.say 'Interrupted!'.red
82
+ rescue SocketError
83
+ cli.say "Couldn't perform action. Are you online?".red
84
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kat_search/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'kat_search'
8
+ spec.version = KatSearch::VERSION
9
+ spec.authors = ['David Marchante']
10
+ spec.email = ['davidmarchan@gmail.com']
11
+
12
+ spec.summary = 'Search KickAss Torrents.'
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = 'https://github.com/iovis9/kat_search.git'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_runtime_dependency 'nokogiri', '~> 1.6'
23
+ spec.add_runtime_dependency 'highline', '~> 1.7'
24
+ spec.add_runtime_dependency 'httparty', '~> 0.13'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.12'
27
+ spec.add_development_dependency 'rake', '~> 11.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ spec.add_development_dependency 'guard-rspec', '~> 4.6'
30
+ spec.add_development_dependency 'factory_girl', '~> 4.5'
31
+ spec.add_development_dependency 'webmock', '~> 2.0'
32
+ spec.add_development_dependency 'pry', '~> 0.10'
33
+ spec.add_development_dependency 'pry-byebug', '~> 3.3'
34
+ end
@@ -0,0 +1,65 @@
1
+ require 'cgi'
2
+ require 'open-uri'
3
+ require 'httparty'
4
+
5
+ module KatSearch
6
+ ##
7
+ # Object that contains the info for a torrent file
8
+ class Link
9
+ @@max_length_name ||= 0
10
+
11
+ attr_reader :seeders, :leechers, :download_url
12
+
13
+ def initialize(params)
14
+ @params = params
15
+ @seeders = params['seeders']
16
+ @leechers = params['leechers']
17
+ @download_url = params['download_url']
18
+
19
+ # Get the longest filename for pretty print
20
+ set_max_length_name(filename.length)
21
+ end
22
+
23
+ def to_s
24
+ "#{filename.ljust(@@max_length_name + 2)} (#{@seeders.green}/#{@leechers.red})"
25
+ end
26
+
27
+ def filename
28
+ @filename ||= "#{CGI.unescape(@params['name'])}.#{@params['extension']}"
29
+ end
30
+
31
+ def magnet
32
+ @magnet ||= @params['magnet']
33
+ end
34
+
35
+ def info_hash
36
+ @info_hash ||= extract_hash
37
+ end
38
+
39
+ def download(path = './')
40
+ response = HTTParty.get(@download_url)
41
+
42
+ raise 'Wrong content-type. Aborting.' unless response.headers['content-type'].include? 'application/x-bittorrent'
43
+
44
+ # Get file name from the url
45
+ filename = @download_url[/\?title=(.+)/, 1] + '.torrent'
46
+ open(File.join(path, filename), 'w') { |f| f << response }
47
+
48
+ # return filename
49
+ filename
50
+ end
51
+
52
+ private
53
+
54
+ def extract_hash
55
+ # Extract magnet properties to a Hash and then parse the sha1 info hash
56
+ magnet_params = CGI.parse(URI.parse(magnet).query)
57
+ raw_hash = magnet_params['xt'].first
58
+ raw_hash.split(':').last.downcase
59
+ end
60
+
61
+ def set_max_length_name(length)
62
+ @@max_length_name = length if @@max_length_name < length
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,53 @@
1
+ require 'erb'
2
+ require 'nokogiri'
3
+ require 'yaml'
4
+
5
+ module KatSearch
6
+ ##
7
+ # Extract a list of results from your search
8
+ # KAT.new("Suits s05e16 1080p")
9
+ class Search
10
+ NUMBER_OF_LINKS = 5
11
+
12
+ attr_accessor :url
13
+
14
+ def initialize(search)
15
+ @url = "https://kat.cr/usearch/#{ERB::Util.url_encode(search)}/"
16
+ end
17
+
18
+ def results_found?
19
+ @results_found ||= page.at('p:contains("did not match any documents")').nil?
20
+ rescue OpenURI::HTTPError
21
+ @results_found = false
22
+ end
23
+
24
+ def links
25
+ @links ||= generate_links
26
+ end
27
+
28
+ private
29
+
30
+ def page
31
+ @page ||= Nokogiri::HTML(open(@url))
32
+ end
33
+
34
+ def generate_links
35
+ links = []
36
+ return links unless results_found?
37
+
38
+ crawled_links = page.css('.iaconbox')
39
+ seeders = page.css('td.green')
40
+ leechers = page.css('td.red')
41
+
42
+ crawled_links[0..NUMBER_OF_LINKS - 1].each_with_index do |link, i|
43
+ params = YAML.load(link.at('div')['data-sc-params'])
44
+ params['seeders'] = seeders[i].text
45
+ params['leechers'] = leechers[i].text
46
+ params['download_url'] = link.at('a[data-download]')['href']
47
+ links << Link.new(params)
48
+ end
49
+
50
+ return links
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,3 @@
1
+ module KatSearch
2
+ VERSION = "1.0.0"
3
+ end
data/lib/kat_search.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'kat_search/version'
2
+ require 'kat_search/search'
3
+ require 'kat_search/link'
metadata ADDED
@@ -0,0 +1,216 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kat_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - David Marchante
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '11.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '11.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.6'
111
+ - !ruby/object:Gem::Dependency
112
+ name: factory_girl
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '4.5'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.5'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '0.10'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '0.10'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.3'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.3'
167
+ description:
168
+ email:
169
+ - davidmarchan@gmail.com
170
+ executables:
171
+ - kat_search
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".gitignore"
176
+ - ".rspec"
177
+ - ".travis.yml"
178
+ - Gemfile
179
+ - Guardfile
180
+ - LICENSE.txt
181
+ - README.md
182
+ - Rakefile
183
+ - bin/console
184
+ - bin/setup
185
+ - exe/kat_search
186
+ - kat_search.gemspec
187
+ - lib/kat_search.rb
188
+ - lib/kat_search/link.rb
189
+ - lib/kat_search/search.rb
190
+ - lib/kat_search/version.rb
191
+ homepage: https://github.com/iovis9/kat_search.git
192
+ licenses:
193
+ - MIT
194
+ metadata: {}
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ required_rubygems_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubyforge_project:
211
+ rubygems_version: 2.6.4
212
+ signing_key:
213
+ specification_version: 4
214
+ summary: Search KickAss Torrents.
215
+ test_files: []
216
+ has_rdoc: