tpb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ v0.0.1 testing gem creation
@@ -0,0 +1,10 @@
1
+ CHANGELOG
2
+ Manifest
3
+ Rakefile
4
+ junk.html
5
+ lib/pirate_bay.rb
6
+ lib/pirate_bay/base.rb
7
+ lib/pirate_bay/categories.rb
8
+ lib/pirate_bay/result.rb
9
+ lib/pirate_bay/result_set.rb
10
+ tpb.gemspec
@@ -0,0 +1,15 @@
1
+ require 'echoe'
2
+ Echoe.new('tpb', '0.0.1') do |p|
3
+ p.description = "A API to query TPB"
4
+ p.url = "http://www.github.com/hjhart/tpb"
5
+ p.author = "James Hart"
6
+ p.email = "hjhart@gmail.com"
7
+ p.ignore_pattern = ["tmp/*", "scripts/*", "searches/*"]
8
+ p.development_dependencies = []
9
+ end
10
+
11
+ task :default => :console
12
+
13
+ task :console do
14
+ sh "irb -rubygems -r ./lib/pirate_bay.rb"
15
+ end
File without changes
@@ -0,0 +1,11 @@
1
+ require 'nokogiri'
2
+ require 'fileutils'
3
+ require 'net/http'
4
+ require 'uri'
5
+ require 'awesome_print'
6
+
7
+ module PirateBay
8
+ %w(result base categories result_set).each do |filename|
9
+ require File.join(File.dirname(__FILE__), 'pirate_bay', filename)
10
+ end
11
+ end
@@ -0,0 +1,78 @@
1
+ module PirateBay
2
+ class Search
3
+ attr_accessor :search_string, :category_id, :page, :caching, :results
4
+
5
+ def initialize(search_string, category=0)
6
+ self.search_string = URI.encode(search_string)
7
+ self.category_id = PirateBay::Categories::IDS[category.upcase.strip.gsub(/S$/, "").to_sym] unless category == 0
8
+ self.page = -1
9
+
10
+ @results = PirateBay::ResultSet.new(self)
11
+ end
12
+
13
+ def get_search_results
14
+ if caching && File.exists?(cached_filename)
15
+ content = File.read(cached_filename)
16
+ else
17
+ content = fetch_search_results
18
+
19
+ FileUtils.mkdir_p("searches")
20
+ File.open(cached_filename, "w") do |f|
21
+ f.write(content)
22
+ end
23
+ end
24
+ content
25
+ end
26
+
27
+ def execute
28
+ return nil if search_string.nil?
29
+ self.page += 1
30
+
31
+ if (@results.size < @results.total_results)
32
+ doc = Nokogiri::HTML(get_search_results)
33
+ end
34
+
35
+ next_page(doc)
36
+
37
+ end
38
+
39
+ def cached_filename
40
+ File.join("searches", "#{search_string}_#{category_id}_#{page}.html")
41
+ end
42
+
43
+ def fetch_search_results
44
+ url = "http://thepiratebay.org/search/#{search_string}/#{page}/7/#{category_id}" # highest seeded first
45
+
46
+ uri = URI.parse(url)
47
+
48
+ http = Net::HTTP.new(uri.host, uri.port)
49
+ response = http.request(Net::HTTP::Get.new(uri.request_uri))
50
+
51
+ response.body
52
+ end
53
+
54
+ private
55
+
56
+ def next_page(doc)
57
+ if @results.total_results.nil?
58
+ matching_results = doc.css("h2").first.content.match(/Displaying hits from (.*) to (.*) \(approx (.*) found\)/i)
59
+
60
+ if (matching_results.nil?)
61
+ @results.total_results = 0
62
+ else
63
+ @results.total_results = matching_results[3].to_i
64
+ end
65
+ end
66
+
67
+ doc.css("#searchResult tr").each_with_index do |row, index|
68
+ next if index == 0
69
+ result = PirateBay::Result.new(row)
70
+ @results << result
71
+ end
72
+ @results
73
+ end
74
+
75
+ end
76
+
77
+
78
+ end
@@ -0,0 +1,48 @@
1
+ module PirateBay
2
+ class Categories
3
+ EXTENDED_IDS = {
4
+ :MUSIC => 101,
5
+ :AUDIO_BOOKS => 102,
6
+ :SOUND_CLIPS => 103,
7
+ :FLAC => 104,
8
+ :MOVIES => 201,
9
+ :MOVIES_DVDR => 202,
10
+ :MUSIC_VIDEOS => 203,
11
+ :MOVIE_CLIPS => 204,
12
+ :TV_SHOWS => 205,
13
+ :HANDHELD => 206,
14
+ :HIGHRES_MOVIES => 207,
15
+ :HIGHRES_TV_SHOWS => 208,
16
+ :WINDOWS => 301,
17
+ :MAC => 302,
18
+ :UNIX => 303,
19
+ :HANDHELD => 304,
20
+ :OTHER_OS => 399,
21
+ :PC => 401,
22
+ :MAC => 402,
23
+ :PS2 => 403,
24
+ :XBOX360 => 404,
25
+ :WII => 405,
26
+ :HANDHELD => 406,
27
+ :OTHER => 499,
28
+ :MOVIES => 501,
29
+ :MOVIES_DVDR => 502,
30
+ :PICTURES => 503,
31
+ :GAMES => 504,
32
+ :HIGHRES_MOVIES => 505,
33
+ :MOVIE_CLIPS => 506,
34
+ :E_BOOKS => 601,
35
+ :COMICS => 602,
36
+ :PICTURES => 603,
37
+ :COVERS => 604
38
+ }
39
+
40
+ IDS = {
41
+ :APPLICATION => 100,
42
+ :MOVIE => 200,
43
+ :AUDIO => 300,
44
+ :GAMES => 400,
45
+ :OTHER => 600
46
+ }
47
+ end
48
+ end
@@ -0,0 +1,36 @@
1
+ module PirateBay
2
+ class Result
3
+ attr_accessor :name, :seeds, :leeches, :category, :link, :magnet_link, :status, :size
4
+
5
+ def initialize(row = nil)
6
+ if row.css("td")[1].css("img[alt='Trusted']").size > 0
7
+ status = "Trusted"
8
+ elsif row.css("td")[1].css("img[alt='VIP']").size > 0
9
+ status = "VIP"
10
+ else
11
+ status = nil
12
+ end
13
+
14
+ magnet_links = row.css("td")[1].css("a[title='Download this torrent using magnet']")
15
+ if magnet_links.size > 0
16
+ magnet_link = magnet_links.first[:href]
17
+ else
18
+ magnet_link = nil
19
+ end
20
+
21
+ self.name = row.css(".detName").first.content
22
+ self.seeds = row.css("td")[2].content.to_i
23
+ self.leeches = row.css("td")[3].content.to_i
24
+ self.category = row.css("td")[0].css("a").map(&:content).join(" > ")
25
+ self.link = row.css("td")[1].css("a[title='Download this torrent']").first[:href]
26
+ self.magnet_link = magnet_link
27
+ self.status = status
28
+ self.size = row.css(".detDesc").first.content.match(/Size (.*[G|M|K]iB)/i)[1]
29
+
30
+ end
31
+
32
+ def to_s
33
+ "<PirateBay::Result @name => #{name}, @seeds => #{seeds}, @category => #{category}>"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ module PirateBay
2
+ class ResultSet < Array
3
+ attr_accessor :total_results, :search
4
+
5
+ def initialize(initializer)
6
+ self.search = initializer
7
+ self.total_results = 1.0/0 #Infinity
8
+ end
9
+
10
+ def retrieved_results
11
+ self.size
12
+ end
13
+
14
+ def more
15
+ search.next_page
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{tpb}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["James Hart"]
9
+ s.date = %q{2011-05-29}
10
+ s.description = %q{A API to query TPB}
11
+ s.email = %q{hjhart@gmail.com}
12
+ s.extra_rdoc_files = ["CHANGELOG", "lib/pirate_bay.rb", "lib/pirate_bay/base.rb", "lib/pirate_bay/categories.rb", "lib/pirate_bay/result.rb", "lib/pirate_bay/result_set.rb"]
13
+ s.files = ["CHANGELOG", "Manifest", "Rakefile", "junk.html", "lib/pirate_bay.rb", "lib/pirate_bay/base.rb", "lib/pirate_bay/categories.rb", "lib/pirate_bay/result.rb", "lib/pirate_bay/result_set.rb", "tpb.gemspec"]
14
+ s.homepage = %q{http://www.github.com/hjhart/tpb}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Tpb"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{tpb}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{A API to query TPB}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tpb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - James Hart
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-05-29 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A API to query TPB
22
+ email: hjhart@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - CHANGELOG
29
+ - lib/pirate_bay.rb
30
+ - lib/pirate_bay/base.rb
31
+ - lib/pirate_bay/categories.rb
32
+ - lib/pirate_bay/result.rb
33
+ - lib/pirate_bay/result_set.rb
34
+ files:
35
+ - CHANGELOG
36
+ - Manifest
37
+ - Rakefile
38
+ - junk.html
39
+ - lib/pirate_bay.rb
40
+ - lib/pirate_bay/base.rb
41
+ - lib/pirate_bay/categories.rb
42
+ - lib/pirate_bay/result.rb
43
+ - lib/pirate_bay/result_set.rb
44
+ - tpb.gemspec
45
+ has_rdoc: true
46
+ homepage: http://www.github.com/hjhart/tpb
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --line-numbers
52
+ - --inline-source
53
+ - --title
54
+ - Tpb
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 1
72
+ - 2
73
+ version: "1.2"
74
+ requirements: []
75
+
76
+ rubyforge_project: tpb
77
+ rubygems_version: 1.3.7
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: A API to query TPB
81
+ test_files: []
82
+