the_pirate_bay 0.0.2 → 0.0.3

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.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # ThePirateBay
1
+ # ThePirateBay [![Build Status](https://secure.travis-ci.org/jassa/the_pirate_bay.png?branch=master)](https://travis-ci.org/jassa/the_pirate_bay) [![Dependency Status](https://gemnasium.com/jassa/the_pirate_bay.png)](https://gemnasium.com/jassa/the_pirate_bay) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/jassa/the_pirate_bay)
2
2
 
3
3
  Ruby Client for ThePirateBay.se
4
4
 
@@ -16,7 +16,29 @@ And then execute:
16
16
 
17
17
  ## Usage
18
18
 
19
- TODO: Write usage instructions here
19
+ ```ruby
20
+ require "the_pirate_bay"
21
+
22
+ api = ThePirateBay.new
23
+
24
+ # You can search for a torrent by keyword
25
+ api.series.search("Fringe")
26
+ => [#<ThePirateBay::Torrent::Collection id: "7810640", name: "Fringe S05E06 Season 5 Episode 6 HDTV x264 [GlowGaz...", seeders: "408", leechers: "116", magnet_uri: "magnet:?xt=urn:btih:0f4e3c1a4618b6d9658427e7778c602...", size: "303.89 MB", type: "Video > TV shows", uploaded_at: "Today 04:11, Size 303.89 MiB", comments_count: "2", uploader: "GlowGaze">]
27
+
28
+ # Once you have a torrent id, you can use it to find more information
29
+ torrent = api.torrents.find("7810640")
30
+ ```
31
+
32
+ ## Work in Progress
33
+
34
+ * Find torrents by id
35
+ * Handle users
36
+ * Sort results by relevance, seeders, leechers, size, date, user and type
37
+ * Get results from other pages
38
+
39
+ ## Motivation
40
+
41
+ ThePirateBay.se is a very powerful engine, and it requires a powerful and specialized gem that can handle all it has to offer, seamlessly.
20
42
 
21
43
  ## Contributing
22
44
 
@@ -1,3 +1,4 @@
1
+ require "the_pirate_bay/errors"
1
2
  require "the_pirate_bay/version"
2
3
  require "the_pirate_bay/api"
3
4
  require "the_pirate_bay/model"
@@ -1,4 +1,5 @@
1
1
  require "the_pirate_bay/torrent"
2
+ require "the_pirate_bay/torrent/collection"
2
3
 
3
4
  module ThePirateBay
4
5
  class Client < API
@@ -1,4 +1,6 @@
1
1
  require "faraday"
2
+ require "the_pirate_bay/response"
3
+ require "the_pirate_bay/response/htmlize"
2
4
 
3
5
  module ThePirateBay
4
6
  module Connection
@@ -10,6 +12,7 @@ module ThePirateBay
10
12
  @connection ||= Faraday.new(:url => ThePirateBay::ENDPOINT) do |faraday|
11
13
  faraday.response :logger if ENV['DEBUG'] # log requests to STDOUT
12
14
  faraday.adapter :net_http # make requests with NetHTTP
15
+ faraday.use ThePirateBay::Response::Htmlize
13
16
  end
14
17
  end
15
18
 
@@ -0,0 +1,18 @@
1
+ module ThePirateBay
2
+
3
+ # Generic ThePirateBay exception class.
4
+ class ThePirateBayError < StandardError
5
+ end
6
+
7
+ # Raised when a connection to the api can't be established.
8
+ class ConnectionNotEstablished < StandardError
9
+ end
10
+
11
+ # Raised when the record was not found given an id or set of ids.
12
+ class RecordNotFound < StandardError
13
+ end
14
+
15
+ # Raised when unknown attributes are supplied via mass assignment.
16
+ class UnknownAttributeError < NoMethodError
17
+ end
18
+ end
@@ -11,7 +11,7 @@ module ThePirateBay
11
11
  def attributes=(params=nil)
12
12
  params.each do |attr, value|
13
13
  begin
14
- self.public_send("#{klass::ATTRS_MAP.key(attr)}=", value)
14
+ self.public_send("#{attr}=", value)
15
15
  rescue NoMethodError
16
16
  raise UnknownAttributeError, "unknown attribute: #{attr}"
17
17
  end
@@ -20,7 +20,7 @@ module ThePirateBay
20
20
 
21
21
  def attributes
22
22
  attrs = {}
23
- klass::ATTRS_MAP.keys.each do |name|
23
+ class_attributes.each do |name|
24
24
  attrs[name] = send(name)
25
25
  end
26
26
  attrs
@@ -28,7 +28,7 @@ module ThePirateBay
28
28
 
29
29
  def inspect
30
30
  inspection = unless id.nil?
31
- klass::ATTRS_MAP.keys.collect { |name|
31
+ class_attributes.collect { |name|
32
32
  "#{name}: #{attribute_for_inspect(name)}"
33
33
  }.compact.join(", ")
34
34
  else
@@ -49,5 +49,9 @@ module ThePirateBay
49
49
  end
50
50
  end
51
51
 
52
+ def class_attributes
53
+ klass::ATTRS
54
+ end
55
+
52
56
  end # Model
53
57
  end # ThePirateBay
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ module ThePirateBay
4
+ # Contains methods and attributes that act on the response returned from the
5
+ # request
6
+ class Response < Faraday::Response::Middleware
7
+ CONTENT_TYPE = 'Content-Type'.freeze
8
+
9
+ class << self
10
+ attr_accessor :parser
11
+ end
12
+
13
+ def self.define_parser(&block)
14
+ @parser = block
15
+ end
16
+
17
+ def response_type(env)
18
+ env[:response_headers][CONTENT_TYPE].to_s
19
+ end
20
+
21
+ def parse_response?(env)
22
+ env[:body].respond_to? :to_str
23
+ end
24
+
25
+ end # Response
26
+ end # ThePirateBay
@@ -0,0 +1,19 @@
1
+ module ThePirateBay
2
+ class Response::Htmlize < Response
3
+ dependency 'nokogiri'
4
+
5
+ define_parser do |body|
6
+ ::Nokogiri::HTML body
7
+ end
8
+
9
+ def parse(body)
10
+ case body
11
+ when String
12
+ self.class.parser.call body
13
+ else
14
+ body
15
+ end
16
+ end
17
+
18
+ end # Response::Htmlize
19
+ end # ThePirateBay
@@ -9,7 +9,14 @@ module ThePirateBay
9
9
 
10
10
  class << self
11
11
  def search(query)
12
- data = request("search/#{query}/0/99/0")
12
+ # Returns search html
13
+ doc = request("search/#{query}/0/99/0")
14
+
15
+ # Get torrents table rows
16
+ # and return as ruby objects
17
+ doc.xpath("//table[@id='searchResult']/tr").collect do |torrent|
18
+ Torrent::Collection.new(torrent)
19
+ end
13
20
  end
14
21
 
15
22
  def find(id)
@@ -17,6 +24,6 @@ module ThePirateBay
17
24
  end
18
25
  alias :get :find
19
26
  end
20
-
27
+
21
28
  end # Torrent
22
29
  end # ThePirateBay
@@ -0,0 +1,64 @@
1
+ module ThePirateBay
2
+ class Torrent::Collection
3
+ include Model
4
+
5
+ ATTRS = [:id, :name, :seeders, :leechers, :magnet_uri,
6
+ :size, :type, :uploaded_at, :comments_count, :uploader].freeze
7
+
8
+ attr_accessor *ATTRS, :torrent
9
+
10
+ def initialize(torrent)
11
+ @torrent = torrent
12
+ class_attributes.collect do |attr|
13
+ self.public_send("#{attr}=", send(attr))
14
+ end
15
+ end
16
+
17
+ def class_attributes
18
+ ATTRS
19
+ end
20
+
21
+ def id
22
+ torrent.css('td[2] div.detName').inner_html.match(/\/torrent\/(\d+)\//)[1]
23
+ end
24
+
25
+ def name
26
+ torrent.css('td[2] div.detName a').text
27
+ end
28
+
29
+ def type
30
+ torrent.css('td[1] a').map(&:text).join(" > ")
31
+ end
32
+
33
+ def size
34
+ torrent.css('td[2] font.detDesc').text.match(/Size (.*),/)[1].gsub('i', '')
35
+ end
36
+
37
+ def seeders
38
+ torrent.css('td[3]').text
39
+ end
40
+
41
+ def leechers
42
+ torrent.css('td[4]').text
43
+ end
44
+
45
+ def uploader
46
+ torrent.css('td[2] font.detDesc a').text
47
+ end
48
+
49
+ def magnet_uri
50
+ torrent.css('td[2] div.detName + a').attribute("href").value
51
+ end
52
+
53
+ def comments_count
54
+ unless (comments = torrent.css('td[2] img[@src*="comment.gif"]')).empty?
55
+ comments.attribute("alt").value.match(/\d+/)[0]
56
+ end
57
+ end
58
+
59
+ def uploaded_at
60
+ torrent.css('td[2] font.detDesc').text.match(/Uploaded (.*),/)[1]
61
+ end
62
+
63
+ end # Torrent::Collection
64
+ end # ThePirateBay
@@ -1,3 +1,3 @@
1
1
  module ThePirateBay
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -3,14 +3,24 @@ require "spec_helper"
3
3
  describe ThePirateBay::Torrent do
4
4
  let(:api) { ThePirateBay.new }
5
5
 
6
- context ".search" do
7
- it "returns html torrents results" do
8
- api.torrents.search("Fringe").should include("Fringe")
6
+ context ".search", focus: true do
7
+ let(:torrents) { api.torrents.search("Fringe") }
8
+
9
+ it "returns torrents results" do
10
+ torrents.class.should == Array
11
+ end
12
+
13
+ it "instantiates torrents collection objects" do
14
+ torrents.first.class.should == ThePirateBay::Torrent::Collection
15
+ end
16
+
17
+ it "assigns torrents attributes" do
18
+ torrents.first.id.should == "7811310"
9
19
  end
10
20
  end
11
21
 
12
22
  context ".find" do
13
- it "returns a particular torrent html" do
23
+ it "returns a particular torrent" do
14
24
  api.torrents.find("7723168").should include("Fringe S05E03 HDTV")
15
25
  end
16
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: the_pirate_bay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-07 00:00:00.000000000 Z
12
+ date: 2012-11-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
16
- requirement: &70282189528700 !ruby/object:Gem::Requirement
16
+ requirement: &70181583441780 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.8.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70282189528700
24
+ version_requirements: *70181583441780
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: nokogiri
27
- requirement: &70282189528200 !ruby/object:Gem::Requirement
27
+ requirement: &70181583469940 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.5.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70282189528200
35
+ version_requirements: *70181583469940
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70282189527740 !ruby/object:Gem::Requirement
38
+ requirement: &70181583469480 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 2.11.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70282189527740
46
+ version_requirements: *70181583469480
47
47
  description: Ruby Client for ThePirateBay.se
48
48
  email:
49
49
  - javier@tractical.com
@@ -62,8 +62,12 @@ files:
62
62
  - lib/the_pirate_bay/api.rb
63
63
  - lib/the_pirate_bay/client.rb
64
64
  - lib/the_pirate_bay/connection.rb
65
+ - lib/the_pirate_bay/errors.rb
65
66
  - lib/the_pirate_bay/model.rb
67
+ - lib/the_pirate_bay/response.rb
68
+ - lib/the_pirate_bay/response/htmlize.rb
66
69
  - lib/the_pirate_bay/torrent.rb
70
+ - lib/the_pirate_bay/torrent/collection.rb
67
71
  - lib/the_pirate_bay/version.rb
68
72
  - spec/spec_helper.rb
69
73
  - spec/the_pirate_bay/torrent_spec.rb