tpb_search 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 32fed2012403eb90419bd6b3b0bcdf61e1246383
4
+ data.tar.gz: acd231a9ef1eaef65f387326e3f9e880fc3154c6
5
+ SHA512:
6
+ metadata.gz: f47514aedef508ffa61b8910e8d11304030718311969376591c1dda6892e2431c5b055a029d4cec287998715de1be092e99fbd14d07f0134ed5b82460c9ab0a9
7
+ data.tar.gz: d74b720ad678882e7ba6ff1fddbce7745738c42aee04ca36ef12a837000659b4c7896bf8b6a6c5261c055fbe6979a4e35dac8ded89adf113476abc86a4875cbe
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tpb_search.gemspec
4
+ gemspec
@@ -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
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 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.
@@ -0,0 +1,41 @@
1
+ # TpbSearch
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/tpb_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 'tpb_search'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tpb_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/David Marchante/tpb_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
+
@@ -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
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tpb_search"
5
+ require 'factory_girl'
6
+
7
+ include FactoryGirl::Syntax::Methods
8
+ FactoryGirl.find_definitions
9
+
10
+ # search = build :search, search: 'silicon valley s03e08'
11
+ search = build :search
12
+
13
+ require 'pry'
14
+ Pry.start
15
+
@@ -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
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby -w
2
+ # coding: utf-8
3
+
4
+ require 'tpb_search'
5
+ require 'fileutils'
6
+ require 'highline'
7
+ require 'optparse'
8
+
9
+ options = {
10
+ auto: false
11
+ }
12
+
13
+ optparse = OptionParser.new do |opts|
14
+ opts.banner = 'Usage: tpb_search [options] <search>'
15
+
16
+ opts.on('-y', '--auto-download', 'Auto-select first link') do
17
+ options[:auto] = true
18
+ end
19
+
20
+ opts.on('-v', '--version', 'Print version and exit') do
21
+ puts "tpb_search v#{TpbSearch::VERSION}"
22
+ exit
23
+ end
24
+
25
+ opts.on_tail('-h', '--help', 'Show this message') do
26
+ puts opts
27
+ exit
28
+ end
29
+ end.parse!
30
+
31
+ # Join arguments to create the search term
32
+ options[:search] = optparse.join(' ')
33
+
34
+ # main program
35
+ begin
36
+ cli = HighLine.new
37
+ HighLine.colorize_strings # Monkey patch String for cli colors
38
+
39
+ # Valid search?
40
+ if options[:search].empty?
41
+ cli.say 'Please type a search'.red
42
+ exit
43
+ end
44
+
45
+ # Search subtitle
46
+ search = TpbSearch::Search.new(options[:search])
47
+
48
+ # Results found?
49
+ unless search.results_found?
50
+ cli.say 'No results found'.red
51
+ exit
52
+ end
53
+
54
+ # Choose
55
+ if options[:auto]
56
+ torrent = search.links.first
57
+ else
58
+ cli.say "Search results for #{options[:search]}:"
59
+ torrent = cli.choose(*search.links) do |menu|
60
+ menu.default = '1'
61
+ menu.select_by = :index
62
+ end
63
+ end
64
+
65
+ # Print magnet link for selected file
66
+ cli.say torrent.magnet
67
+ rescue Interrupt, EOFError
68
+ cli.say 'Interrupted!'.red
69
+ rescue SocketError
70
+ cli.say "Couldn't perform action. Are you online?".red
71
+ end
@@ -0,0 +1,3 @@
1
+ require "tpb_search/link"
2
+ require "tpb_search/search"
3
+ require "tpb_search/version"
@@ -0,0 +1,38 @@
1
+ require 'cgi'
2
+ require 'httparty'
3
+
4
+ module TpbSearch
5
+ ##
6
+ # Object that contains the info for a torrent file
7
+ class Link
8
+ attr_reader :filename, :size, :magnet, :seeders, :leechers
9
+
10
+ def initialize(filename: nil, size: nil, magnet: nil, seeders: nil, leechers: nil)
11
+ @filename = filename
12
+ @size = size
13
+ @magnet = magnet
14
+ @seeders = seeders.tr(',', '').to_i
15
+ @leechers = leechers.tr(',', '').to_i
16
+ end
17
+
18
+ def <=>(other)
19
+ @seeders <=> other.seeders
20
+ end
21
+
22
+ def to_s
23
+ "#{@filename} (#{@size}) - [#{@seeders.to_s.green}/#{@leechers.to_s.red}]"
24
+ end
25
+
26
+ def info_hash
27
+ @info_hash ||= extract_hash
28
+ end
29
+
30
+ private
31
+
32
+ def extract_hash
33
+ # Extract magnet properties to a Hash and then parse the sha1 info hash
34
+ raw_hash = magnet[/(xt.*?)&/, 1] # extract the xt property
35
+ raw_hash.split(':').last.downcase
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,58 @@
1
+ require 'erb'
2
+ require 'open-uri'
3
+ require 'nokogiri'
4
+ require 'yaml'
5
+
6
+ module TpbSearch
7
+ ##
8
+ # Extract a list of results from your search
9
+ # TpbSearch::Search.new("Suits s05e16")
10
+ class Search
11
+ NUMBER_OF_LINKS = 5
12
+ BASE_URL = 'https://thepiratebay.org'.freeze
13
+
14
+ attr_accessor :url
15
+
16
+ def initialize(search)
17
+ @url = "#{BASE_URL}/search/#{ERB::Util.url_encode(search)}/0/7/200"
18
+ end
19
+
20
+ def results_found?
21
+ @results_found ||= page.at(':contains("No hits. Try adding an asterisk in you search phrase.")').nil?
22
+ rescue OpenURI::HTTPError
23
+ @results_found = false
24
+ end
25
+
26
+ def links
27
+ @links ||= generate_links
28
+ end
29
+
30
+ private
31
+
32
+ def page
33
+ @page ||= Nokogiri::HTML(open(@url))
34
+ end
35
+
36
+ def generate_links
37
+ links = []
38
+ return links unless results_found?
39
+
40
+ link_nodes = page.css('#searchResult tr:not(.header)')
41
+ link_nodes.each { |link| links << crawl_link(link) }
42
+
43
+ links.first(NUMBER_OF_LINKS)
44
+ end
45
+
46
+ def crawl_link(link)
47
+ cells = link.css('td')
48
+
49
+ Link.new(
50
+ filename: cells[1].at('a').text,
51
+ size: cells[1].at('.detDesc').text[/\d+\.\d+ MiB|GiB/],
52
+ magnet: cells[1].at('a[href^=magnet]')['href'],
53
+ seeders: cells[2].text,
54
+ leechers: cells[3].text
55
+ )
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module TpbSearch
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tpb_search/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tpb_search"
8
+ spec.version = TpbSearch::VERSION
9
+ spec.authors = ["David Marchante"]
10
+ spec.email = ["davidmarchan@gmail.com"]
11
+
12
+ spec.summary = %q{'Search The Pirate Bay for torrents and magnet links'}
13
+ # spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = "https://github.com/iovis9/tpb_search.git"
15
+ spec.license = "MIT"
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency 'nokogiri', '~> 1.7'
24
+ spec.add_runtime_dependency 'highline', '~> 1.7'
25
+ spec.add_runtime_dependency 'httparty', '~> 0.14'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.14"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency 'guard-rspec', '~> 4.7'
31
+ spec.add_development_dependency 'factory_girl', '~> 4.8'
32
+ spec.add_development_dependency 'webmock', '~> 2.3'
33
+ spec.add_development_dependency 'pry', '~> 0.10'
34
+ spec.add_development_dependency 'pry-byebug', '~> 3.4'
35
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpb_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Marchante
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-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.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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.14'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.14'
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.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.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.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.7'
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.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '4.8'
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.3'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.3'
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.4'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.4'
167
+ description:
168
+ email:
169
+ - davidmarchan@gmail.com
170
+ executables:
171
+ - tpb_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/tpb_search
186
+ - lib/tpb_search.rb
187
+ - lib/tpb_search/link.rb
188
+ - lib/tpb_search/search.rb
189
+ - lib/tpb_search/version.rb
190
+ - tpb_search.gemspec
191
+ homepage: https://github.com/iovis9/tpb_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.10
212
+ signing_key:
213
+ specification_version: 4
214
+ summary: "'Search The Pirate Bay for torrents and magnet links'"
215
+ test_files: []