pirata 0.0.1

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.
@@ -0,0 +1,4 @@
1
+ require 'pirata/search'
2
+ require 'pirata/torrent'
3
+ require 'pirata/category'
4
+ require 'pirata/config'
@@ -0,0 +1,10 @@
1
+ module Pirata
2
+ module Category
3
+ Audio = "100"
4
+ Video = "200"
5
+ Applications = "300"
6
+ Games = "400"
7
+ Porn = "500"
8
+ Other = "600"
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ module Pirata
2
+ module Config
3
+
4
+ # This is the base URL to query against. Depending on server/mirror
5
+ # availability, blocked URLs, etc, you may need to change this. A
6
+ # list of available mirrors for ThePirateBay may be found at
7
+ # http://http://proxybay.info/
8
+ #
9
+ # Note that all URLs should yeild the same results. You are advised
10
+ # to pick a mirror that is closest to your application server for best
11
+ # results though.
12
+
13
+ BASE_URL = "http://thepiratebay.si"
14
+
15
+ end
16
+ end
@@ -0,0 +1,89 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'torrent'
4
+ require 'user'
5
+ require 'category'
6
+ require 'sort'
7
+ require 'config'
8
+
9
+ module Pirata
10
+ class Search
11
+
12
+ attr_accessor :results, :pages
13
+ attr_reader :category, :sort_type, :query
14
+
15
+ def initialize(query, sort_type = Pirata::Sort::RELEVANCE, categories = ["0"])
16
+ @sort_type = sort_type
17
+ @category = categories.join(',')
18
+ @pages = 0
19
+ @query = query
20
+ @results = search()
21
+ end
22
+
23
+ # Perform a search and return an array of Torrent objects
24
+ # Requires a query string.
25
+ def search(page = 0)
26
+ #build URL ex: http://thepiratebay.se/search/cats/0/99/0
27
+ url = Pirata::Config::BASE_URL + "/search/#{URI.escape(@query)}" + "/#{page.to_s}" + "/#{@sort_type}" + "/#{@category}"
28
+ html = Nokogiri::HTML(open(url))
29
+ Pirata::Search::parse_search_page(html, self)
30
+ end
31
+
32
+ # Return the n page results of a search, assuming it is multipage
33
+ def search_page(page)
34
+ raise "Search must be multipage to search pages" if !multipage?
35
+ raise "Page must be a valid, positive integer" if page.class != Fixnum || page < 0
36
+ raise "Invalid page range" if page > @pages
37
+
38
+ self.search(page)
39
+ end
40
+
41
+ # Return the n most recent torrents from a category
42
+ # Searches all categories if none supplied
43
+ def self.top(category = "all")
44
+ html = Nokogiri::HTML(open(Pirata::Config::BASE_URL + '/top/' + URI.escape(category)))
45
+ Pirata::Search::parse_search_page(html)
46
+ end
47
+
48
+ # Return an array of the 30 most recent Torrents
49
+ def self.recent
50
+ html = Nokogiri::HTML(open(Pirata::Config::BASE_URL + '/recent'))
51
+ Pirata::Search::parse_search_page(html)
52
+ end
53
+
54
+ def multipage?
55
+ @pages > 0
56
+ end
57
+
58
+ private #---------------------------------------------
59
+
60
+ # From a results table, collect and build all Torrents
61
+ # into an array
62
+ def self.parse_search_page(html, search_object = nil)
63
+ results = []
64
+ search_object.pages = html.css('#content div a')[-2].text.to_i unless search_object.nil?
65
+
66
+ html.css('#searchResult tr').each do |row|
67
+ title = row.search('.detLink').text
68
+ next if title == ''
69
+ h = {}
70
+
71
+ begin
72
+ h[:title] = title
73
+ h[:category] = row.search('td a')[0].text
74
+ h[:url] = Pirata::Config::BASE_URL + row.search('.detLink').attribute('href').to_s
75
+ h[:id] = h[:url].split('/')[4].to_i
76
+ h[:magnet] = row.search('td a')[3]['href']
77
+ h[:seeders] = row.search('td')[2].text.to_i
78
+ h[:leechers] = row.search('td')[3].text.to_i
79
+ h[:uploader] = Pirata::User.new(row.search('td a')[5].text, Pirata::Config::BASE_URL)
80
+ rescue
81
+ #puts "not found"
82
+ end
83
+ results << Pirata::Torrent.new(h)
84
+ end
85
+ results
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,12 @@
1
+ module Pirata
2
+ module Sort
3
+ RELEVANCE = "99"
4
+ TYPE = "13"
5
+ NAME = "1"
6
+ DATE = "3"
7
+ SIZE = "5"
8
+ UPLOADER = "11"
9
+ SEEDERS = "7"
10
+ LEECHERS = "9"
11
+ end
12
+ end
@@ -0,0 +1,124 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+ require 'time'
4
+ require 'config'
5
+
6
+ module Pirata
7
+ class Torrent
8
+
9
+ def initialize(params_hash)
10
+ @params = params_hash
11
+ end
12
+
13
+ def title
14
+ @params[:title]
15
+ end
16
+
17
+ def category
18
+ @params[:category]
19
+ end
20
+
21
+ def title
22
+ @params[:title]
23
+ end
24
+
25
+ def url
26
+ @params[:url]
27
+ end
28
+
29
+ def id
30
+ @params[:id]
31
+ end
32
+
33
+ def magnet
34
+ @params[:magnet]
35
+ end
36
+
37
+ def seeders
38
+ @params[:seeders]
39
+ end
40
+
41
+ def leechers
42
+ @params[:leechers]
43
+ end
44
+
45
+ def uploader
46
+ @params[:uploader]
47
+ end
48
+
49
+ def files
50
+ unless @params[:files]
51
+ update_params
52
+ end
53
+ @params[:files]
54
+ end
55
+
56
+ def size
57
+ unless @params[:size]
58
+ update_params
59
+ end
60
+ @params[:size]
61
+ end
62
+
63
+ def date
64
+ unless @params[:date]
65
+ update_params
66
+ end
67
+ @params[:date]
68
+ end
69
+
70
+ def comments
71
+ unless @params[:comments]
72
+ update_params
73
+ end
74
+ @params[:comments]
75
+ end
76
+
77
+ def hash
78
+ unless @params[:hash]
79
+ update_params
80
+ end
81
+ @params[:hash]
82
+ end
83
+
84
+ # Return a Torrent object from a corresponding ID
85
+ def self.find_by_id(id)
86
+ raise "Invalid torrent ID format. Must be an integer" if id.class != Fixnum
87
+ html = Nokogiri::HTML(open(Pirata::Config::BASE_URL + "/torrent" + "/#{URI.escape(id.to_s)}"))
88
+ results_hash = parse_torrent_page(html)
89
+ Pirata::Torrent.new(results_hash)
90
+ end
91
+
92
+ def update_params
93
+ html = Nokogiri::HTML(open(@params[:url]))
94
+ updated_params = Pirata::Torrent.parse_torrent_page(html)
95
+ @params.merge!(updated_params)
96
+ end
97
+
98
+ private #-------------------------------------------------
99
+
100
+ def self.parse_torrent_page(html)
101
+ if html.css("#err").text.include?("404")
102
+ raise "No torrent for supplied ID found"
103
+ else
104
+ row = html.css('#detailsframe').first
105
+ h = {
106
+ :title => row.search('#title')[0].text.strip,
107
+ :files => row.search('dd a')[1].text.to_i,
108
+ :size => row.search('dd')[2].child.text,
109
+ :date => Time.parse(row.search('.col2 dd')[0].text),
110
+ :uploader => row.search('dd')[4].text.strip,
111
+ :seeders => row.search('dd')[5].text.to_i,
112
+ :leechers => row.search('dd')[6].text.to_i,
113
+ :comments => row.search('dd')[7].text.to_i,
114
+ :hash => row.search('dl').text.split.last.strip
115
+ }
116
+ return h
117
+ end
118
+ nil
119
+ end
120
+
121
+ end
122
+ end
123
+
124
+ Pirata::Torrent.find_by_id(10080116)
@@ -0,0 +1,13 @@
1
+ module Pirata
2
+ class User
3
+
4
+ attr_reader :profile_url, :username
5
+
6
+ def initialize(username, base_url)
7
+ @username = username
8
+ @base_url = base_url
9
+ @profile_url = "#{@base_url}/user/#{@username}"
10
+ end
11
+
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pirata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Colin Lindsay
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-05-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &17568320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *17568320
25
+ description: A Ruby gem that exposes an API for using The Pirate Bay torrent tracker
26
+ service.
27
+ email: clindsay107@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/pirata.rb
33
+ - lib/pirata/search.rb
34
+ - lib/pirata/category.rb
35
+ - lib/pirata/sort.rb
36
+ - lib/pirata/config.rb
37
+ - lib/pirata/torrent.rb
38
+ - lib/pirata/user.rb
39
+ homepage:
40
+ licenses:
41
+ - GPL
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ - lib/pirata
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.11
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Pirata - a Ruby API for The Pirate Bay
65
+ test_files: []