torckapi 0.0.6 → 0.0.7

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: cca36f5eb458537d0180530ca4c499fd1463825d
4
- data.tar.gz: f41250059a73d19bbc6d3b09b9c98ed721ba3854
3
+ metadata.gz: 2de6d595520b549bc5037203130531b17d001eb0
4
+ data.tar.gz: ce7fd8f6589710c14c4d078e3429418f03bc869d
5
5
  SHA512:
6
- metadata.gz: a2cf422fceb42b235e17be8374355aa11e9b20ae4a86e4ad13194c60d76e5fa1b80ca203153ccb278edd25407655a998a1ceeec836fbdd652f004b536949b3d7
7
- data.tar.gz: 83a83ce580aef1b5b030f7c945a061691dabaecc704881c4ca7eebf91a805b65682c69492350b00c0c690ac9b61f8bc4a89e3e3347da186e4a3260afffbaeca8
6
+ metadata.gz: 854bc8b226f4def7aa869860e730c946f54004a0773cbcbca6cdd967242edfa2d2eed537bcf0e9b917170753f9217c6cfd18b3194b91664cf344651810ef1184
7
+ data.tar.gz: cfda3433018c2178e719dd27c44feeeac68e60f1ac5ea57213438488613fb4b988ce93bc55854e7df3e17677aab19c1439b32e23d2a4c6862543fff4b1e60e0a
@@ -19,11 +19,7 @@ module Torckapi
19
19
  # @param data [String] UDP response data (omit action and transaction_id)
20
20
  # @return [Torckapi::Response::Announce] response
21
21
  def self.from_udp info_hash, data
22
- return new(info_hash, nil, nil, []) unless data
23
-
24
- leechers, seeders = data[0..7].unpack('L>2')
25
-
26
- new info_hash, leechers, seeders, peers_from_compact(data[8..-1])
22
+ new info_hash, *data[4..11].unpack('L>2'), peers_from_compact(data[12..-1])
27
23
  end
28
24
 
29
25
  # Construct response object from http response data
@@ -32,12 +28,8 @@ module Torckapi
32
28
  # @param compact [true, false] is peer data in compact format?
33
29
  # @return [Torckapi::Response::Announce] response
34
30
  def self.from_http info_hash, data, compact=true
35
- return new(info_hash, nil, nil, []) unless data
36
-
37
31
  bdecoded_data = BEncode.load(data)
38
- leechers, seeders = bdecoded_data.values_at("incomplete", "complete")
39
-
40
- new info_hash, leechers, seeders, peers_from_compact(bdecoded_data["peers"])
32
+ new info_hash, *bdecoded_data.values_at("incomplete", "complete"), peers_from_compact(bdecoded_data["peers"])
41
33
  end
42
34
 
43
35
  private
@@ -0,0 +1,26 @@
1
+ module Torckapi
2
+ module Response
3
+ class Error
4
+ # @!attribute [r] info_hash
5
+ # @return [String] 40-char hexadecimal string
6
+ # @!attribute [r] message
7
+ # @return [String] error description
8
+ attr_reader :info_hash, :message
9
+
10
+ # Construct response object from udp response data
11
+ # @param info_hash [String] 40-char hexadecimal string
12
+ # @param data [String] UDP response data (omit action and transaction_id)
13
+ # @return [Torckapi::Response::Error] response
14
+ def self.from_udp info_hash, data
15
+ new info_hash, data
16
+ end
17
+
18
+ private
19
+
20
+ def initialize info_hash, message
21
+ @info_hash = info_hash
22
+ @message = message
23
+ end
24
+ end
25
+ end
26
+ end
@@ -11,8 +11,6 @@ module Torckapi
11
11
  # @param data [String] UDP response data (omit action and transaction_id)
12
12
  # @return [Torckapi::Response::Scrape] response
13
13
  def self.from_udp info_hashes, data
14
- return new({}) unless data
15
-
16
14
  new Hash[info_hashes.zip(data.unpack('a12' * (info_hashes.count)).map { |i| Hash[[:seeders, :completed, :leechers].zip i.unpack('L>3').map(&:to_i)] })]
17
15
  end
18
16
 
@@ -23,4 +21,4 @@ module Torckapi
23
21
  end
24
22
  end
25
23
  end
26
- end
24
+ end
@@ -5,16 +5,16 @@ module Torckapi
5
5
  class Base
6
6
  # Announce Request
7
7
  # @param info_hash [String] 40-char hexadecimal string
8
- # @return [Torckapi::AnnounceResponse] a response object
8
+ # @return [Torckapi::Response::Announce] a response object
9
9
  def announce info_hash
10
10
  raise Torckapi::InvalidInfohashError if info_hash !~ /\A[0-9a-f]{40}\z/i
11
11
  end
12
12
 
13
13
  # Scrape Request
14
- # @param info_hashes [String, Array<String>] A single 40-char hexadecimal string or an array of those
15
- # @return [Torckapi::ScrapeResponse] a response object
14
+ # @param info_hashes [Array<String>] An array of 40-char hexadecimal strings
15
+ # @return [Torckapi::Response::Scrape] a response object
16
16
  def scrape info_hashes=[]
17
- raise Torckapi::InvalidInfohashError if [*info_hashes].any? { |i| i !~ /\A[0-9a-f]{40}\z/i }
17
+ raise Torckapi::InvalidInfohashError if info_hashes.any? { |i| i !~ /\A[0-9a-f]{40}\z/i }
18
18
  end
19
19
 
20
20
  private
@@ -11,11 +11,12 @@ module Torckapi
11
11
  super info_hash
12
12
 
13
13
  @url.query ||= ""
14
- @url.query += "info_hash=%s&num_want=500" % URI.encode([info_hash].pack('H*'))
14
+ @url.query += "info_hash=%s" % URI.encode([info_hash].pack('H*'))
15
15
 
16
16
  Torckapi::Response::Announce.from_http info_hash, Net::HTTP.get(@url)
17
17
  end
18
18
 
19
+ # (see Base#scrape)
19
20
  def scrape info_hashes=[]
20
21
  super info_hashes
21
22
 
@@ -10,30 +10,41 @@ module Torckapi
10
10
  class UDP < Base
11
11
  CONNECTION_TIMEOUT = 60
12
12
 
13
- # @see Base#announce
13
+ # (see Base#announce)
14
14
  def announce info_hash
15
15
  super info_hash
16
-
17
- data = [[info_hash].pack('H*'), SecureRandom.random_bytes(20), [0, 0, 0, 0, 0, 0, -1, 0].pack('Q>3L>4S>')].join
18
-
19
- connect
20
- response_body = communicate(1, data)[0] # announce
21
-
22
- Torckapi::Response::Announce.from_udp info_hash, response_body[12..-1]
16
+ perform_request 1, announce_request_data(info_hash), info_hash
23
17
  end
24
18
 
19
+ # (see Base#scrape)
25
20
  def scrape info_hashes=[]
26
21
  super info_hashes
22
+ perform_request 2, scrape_request_data(info_hashes), info_hashes
23
+ end
27
24
 
28
- data = [*info_hashes].map { |i| [i].pack('H*') }.join
25
+ private
29
26
 
27
+ def perform_request action, data, *args
30
28
  connect
31
- response_body = communicate(2, data)[0] # scrape
29
+ response = communicate action, data
30
+
31
+ case response[0][0..3].unpack('L>')[0] # action
32
+ when 1
33
+ Torckapi::Response::Announce.from_udp(*args, response[0][8..-1])
34
+ when 2
35
+ Torckapi::Response::Scrape.from_udp(*args, response[0][8..-1])
36
+ when 3
37
+ Torckapi::Response::Error.from_udp(*args, response[0][8..-1])
38
+ end
39
+ end
32
40
 
33
- Torckapi::Response::Scrape.from_udp [*info_hashes], response_body[8..-1]
41
+ def announce_request_data info_hash
42
+ [[info_hash].pack('H*'), SecureRandom.random_bytes(20), [0, 0, 0, 0, 0, 0, -1, 0].pack('Q>3L>4S>')].join
34
43
  end
35
44
 
36
- private
45
+ def scrape_request_data info_hashes
46
+ info_hashes.map { |i| [i].pack('H*') }.join
47
+ end
37
48
 
38
49
  def connect
39
50
  return if @connection_id && @communicated_at.to_i >= Time.now.to_i - CONNECTION_TIMEOUT
@@ -56,7 +67,6 @@ module Torckapi
56
67
  @socket.send(packet, 0, @url.host, @url.port)
57
68
  response = @socket.recvfrom(65536)
58
69
  raise TransactionIdMismatchError if transaction_id != response[0][4..7]
59
- raise Torckapi::Tracker::Error.new(response[8..-1]) if 3 == response[0][0..3].unpack('L>')[0] # tracker sent error
60
70
  @communicated_at = Time.now
61
71
  end
62
72
  rescue CommunicationTimeoutError
@@ -1,3 +1,3 @@
1
1
  module Torckapi
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torckapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Krupenik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-11 00:00:00.000000000 Z
11
+ date: 2013-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -66,6 +66,7 @@ files:
66
66
  - lib/torckapi.rb
67
67
  - lib/torckapi/errors.rb
68
68
  - lib/torckapi/response/announce.rb
69
+ - lib/torckapi/response/error.rb
69
70
  - lib/torckapi/response/scrape.rb
70
71
  - lib/torckapi/tracker/base.rb
71
72
  - lib/torckapi/tracker/http.rb