pirata 1.3.2 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57d0e5a37885ba0286f17269ce11d0e057dba9fb
4
- data.tar.gz: 71d1cc868d248c8fe183ed79258471c69d8bc0ee
3
+ metadata.gz: 4728dafd1502dbc0f3386dbea074eb158d3a4f28
4
+ data.tar.gz: 65f6380e34378b57d4db34ab697719318bfca0e1
5
5
  SHA512:
6
- metadata.gz: 29934ca3f570359e5afb626fff906e347d74104e0fb58cf4250392093617987c465f4bcfb779de12b50c8dfca51c8b96fff5fd0d04898e68f48ea242195c0b7d
7
- data.tar.gz: 71dfb01708a2a5d3e3aad7dc909de0c9d596d6ff4eb1c92e851624c14ee6b73404d8d14584f4f1b6a015deca5db2bb4e99a9711bace74ce094e726826eb3f9bd
6
+ metadata.gz: 7f60f5cecf40cf03c28815d3d4594610a233b1a7238f50e8fecf71ce7a24f1032acacb2c5ccf1b4df87799dd361473c6b40abf43a42ddd4d6e3c6efb0c94f601
7
+ data.tar.gz: 098b2c073553db0f4e34d730a8bf9eed18dedb49748e57c2e158cbfec9fa1aae2c407ca30e25188293aaa991207a7159df687d069d127fe075afbde6abe2ae5f
data/lib/pirata.rb CHANGED
@@ -1,4 +1,30 @@
1
1
  require 'pirata/search'
2
2
  require 'pirata/torrent'
3
3
  require 'pirata/category'
4
- require 'pirata/config'
4
+ require 'yaml'
5
+
6
+ module Pirata
7
+ @config = {
8
+ :base_url => 'https://pirateproxy.tv/',
9
+ :redirect => :all
10
+ }
11
+
12
+ def self.valid_key?(key)
13
+ @config.keys.include?(key.to_sym)
14
+ end
15
+
16
+ def self.configure(opts = nil)
17
+ return if opts.nil?
18
+
19
+ if opts.class == Hash
20
+ opts.each do |k, v|
21
+ @config[k.to_sym] = v if self.valid_key?(k)
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.config
27
+ @config
28
+ end
29
+
30
+ end
data/lib/pirata/search.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'nokogiri'
2
2
  require 'open_uri_redirections'
3
3
  require 'open-uri'
4
- require 'pirata/config'
5
4
  require 'pirata/torrent'
6
5
  require 'pirata/user'
7
6
  require 'pirata/category'
@@ -24,8 +23,8 @@ module Pirata
24
23
  # Perform a search and return an array of Torrent objects
25
24
  def search(page = 0)
26
25
  #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, :allow_redirections => Pirata::Config::REDIRECT))
26
+ url = Pirata.config[:base_url] + "/search/#{URI.escape(@query)}" + "/#{page.to_s}" + "/#{@sort_type}" + "/#{@category}"
27
+ html = Pirata::Search.parse_html(url)
29
28
  Pirata::Search::parse_search_page(html, self)
30
29
  end
31
30
 
@@ -41,15 +40,15 @@ module Pirata
41
40
  # Return the n most recent torrents from a category
42
41
  # Searches all categories if none supplied
43
42
  def self.top(category = "all")
44
- url = Pirata::Config::BASE_URL + '/top/' + URI.escape(category)
45
- html = Nokogiri::HTML(open(url, :allow_redirections => Pirata::Config::REDIRECT))
43
+ url = Pirata.config[:base_url] + '/top/' + URI.escape(category)
44
+ html = self.parse_html(url)
46
45
  Pirata::Search::parse_search_page(html)
47
46
  end
48
47
 
49
48
  # Return an array of the 30 most recent Torrents
50
49
  def self.recent
51
- url = Pirata::Config::BASE_URL + '/recent'
52
- html = Nokogiri::HTML(open(url, :allow_redirections => Pirata::Config::REDIRECT))
50
+ url = Pirata.config[:base_url] + '/recent'
51
+ html = self.parse_html(url)
53
52
  Pirata::Search::parse_search_page(html)
54
53
  end
55
54
 
@@ -59,6 +58,20 @@ module Pirata
59
58
 
60
59
  private #---------------------------------------------
61
60
 
61
+ # Parse HTML body of a supplied URL
62
+ class << self
63
+ def parse_html(url)
64
+ response = open(url, :allow_redirections => Pirata.config[:redirect])
65
+ Nokogiri::HTML(response)
66
+ end
67
+ end
68
+
69
+ # Parse HTML body of a supplied URL
70
+ def parse_html(url)
71
+ response = open(url, :allow_redirections => Pirata.config[:redirect])
72
+ Nokogiri::HTML(response)
73
+ end
74
+
62
75
  # From a results table, collect and build all Torrents
63
76
  # into an array
64
77
  def self.parse_search_page(html, search_object = nil)
@@ -77,7 +90,7 @@ module Pirata
77
90
  begin
78
91
  h[:title] = title
79
92
  h[:category] = row.search('td a')[0].text
80
- url = Pirata::Config::BASE_URL + row.search('.detLink').attribute('href').to_s
93
+ url = Pirata.config[:base_url] + row.search('.detLink').attribute('href').to_s
81
94
  h[:url] = url.gsub("[", "%5B").gsub("]", "%5D")
82
95
  h[:id] = h[:url].split('/')[4].to_i
83
96
  h[:magnet] = row.search('td a')[3]['href']
@@ -2,7 +2,6 @@ require 'nokogiri'
2
2
  require 'open-uri'
3
3
  require 'open_uri_redirections'
4
4
  require 'time'
5
- require 'pirata/config'
6
5
 
7
6
  module Pirata
8
7
  class Torrent
@@ -23,20 +22,31 @@ module Pirata
23
22
  # Return a Torrent object from a corresponding ID
24
23
  def self.find_by_id(id)
25
24
  raise "Invalid torrent ID format. Must be an integer" if id.class != Fixnum
26
- url = Pirata::Config::BASE_URL + "/torrent" + "/#{URI.escape(id.to_s)}"
27
- html = Nokogiri::HTML(open(url, :allow_redirections => Pirata::Config::REDIRECT))
25
+
26
+ url = Pirata.config[:base_url] + "/torrent" + "/#{URI.escape(id.to_s)}"
27
+ html = self.parse_html(url)
28
28
  results_hash = parse_torrent_page(html)
29
+
29
30
  Pirata::Torrent.new(results_hash)
30
31
  end
31
32
 
32
33
  def update_params!
33
- html = Nokogiri::HTML(open(@params[:url], :allow_redirections => Pirata::Config::REDIRECT))
34
+ html = Pirata::Torrent.parse_html(url)
35
+
34
36
  updated_params = Pirata::Torrent.parse_torrent_page(html)
35
37
  @params.merge!(updated_params)
36
38
  end
37
39
 
38
40
  private #-------------------------------------------------
39
41
 
42
+ # Parse HTML body of a supplied URL
43
+ class << self
44
+ def parse_html(url)
45
+ response = open(url, :allow_redirections => Pirata.config[:redirect])
46
+ Nokogiri::HTML(response)
47
+ end
48
+ end
49
+
40
50
  # Initialize getters for +1 request variables, fetching them if we need them
41
51
  def build_getters
42
52
  [:seeders, :leechers, :uploader, :files, :size, :date, :comments, :hash].each do |m|
data/lib/pirata/user.rb CHANGED
@@ -1,5 +1,3 @@
1
- require 'pirata/config'
2
-
3
1
  module Pirata
4
2
  class User
5
3
 
@@ -7,7 +5,7 @@ module Pirata
7
5
 
8
6
  def initialize(username)
9
7
  @username = username
10
- @profile_url = "#{Pirata::Config::BASE_URL}/user/#{@username}"
8
+ @profile_url = "#{Pirata.config[:base_url]}/user/#{@username}"
11
9
  end
12
10
 
13
11
  def to_s
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pirata
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Lindsay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-26 00:00:00.000000000 Z
11
+ date: 2016-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -67,7 +67,6 @@ extra_rdoc_files: []
67
67
  files:
68
68
  - lib/pirata.rb
69
69
  - lib/pirata/category.rb
70
- - lib/pirata/config.rb
71
70
  - lib/pirata/search.rb
72
71
  - lib/pirata/sort.rb
73
72
  - lib/pirata/torrent.rb
@@ -93,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
92
  version: '0'
94
93
  requirements: []
95
94
  rubyforge_project:
96
- rubygems_version: 2.4.8
95
+ rubygems_version: 2.4.6
97
96
  signing_key:
98
97
  specification_version: 4
99
98
  summary: Pirata - a Ruby API for The Pirate Bay
data/lib/pirata/config.rb DELETED
@@ -1,23 +0,0 @@
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
- # https://proxybay.la/
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 = "https://ahoy.re"
14
-
15
- # This is the rule used when following HTTPS <-> HTTP redirects.
16
- # It accepts :all and :safe
17
- # :safe will allow HTTP -> HTTPS redirections
18
- # :all will allow both HTTP -> HTTPS redirections as well as HTTPS -> HTTP
19
-
20
- REDIRECT = :all
21
-
22
- end
23
- end