torckapi 0.0.12 → 0.0.13

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: 521381cc5f7043c7b0895498adb494875ab07247
4
- data.tar.gz: 0d7a63fd41723fc794232936489e56fac2fd7d87
3
+ metadata.gz: 979d317e486ddd5e74790d41d1f987a637eb12ef
4
+ data.tar.gz: 9a46f9e9944e82e5087e026c1feab81801e94922
5
5
  SHA512:
6
- metadata.gz: 6854a5a7625fd0b28ec785702eccb9e8a789491056f2c2ea1767f92567d30c86a11b655f4a45f18235a249037a7e902f6f1d605cc0746e610f3c992f0437d2dc
7
- data.tar.gz: 8a52f192ad8183ce3795824a3c86da5235daf89c0691efa7a2acc3c189aa97aa661891ad7352ea43d3a28b8ed067fd448fa1753b9dd7880ff19880246ff08f6e
6
+ metadata.gz: acffe09aa8a0c9170b82e6d8d96c1b0fb03ee6c24369353941447e8aa748f49e3d5bcc160a3c1c3a16883155f24eb97c9ffdd3d2185b459b33eb0fa2d881bf85
7
+ data.tar.gz: 7556ff3679e20b19aded7e54cccc42f1458433d2de7f88ecdd96ef8d4984698daf6ecfc76680294899c7b31ce7ba111578c6b2a9bf9fccb0dac6309be0831279
@@ -1,6 +1,7 @@
1
1
  module Torckapi
2
2
  class Error < StandardError; end
3
3
  class InvalidInfohashError < Error; end
4
+ class ArgumentError < Error; end
4
5
 
5
6
  module Tracker
6
7
  class Error < Torckapi::Error; end
@@ -12,9 +13,4 @@ module Torckapi
12
13
  class MalformedResponseError < CommunicationError; end
13
14
  class TransactionIdMismatchError < CommunicationError; end
14
15
  end
15
-
16
- module Response
17
- class Error < Torckapi::Error; end
18
- class ArgumentError < Error; end
19
- end
20
16
  end
@@ -10,7 +10,7 @@ module Torckapi
10
10
  attr_reader :info_hash, :info_hashes, :message
11
11
 
12
12
  # Construct response object from udp response data
13
- # @param info_hash [String] 40-char hexadecimal string
13
+ # @param info_hashes [String, Array<String>] a 40-char hexadecimal string or an array of those
14
14
  # @param data [String] UDP response data (omit action and transaction_id)
15
15
  # @return [Torckapi::Response::Error] response
16
16
  def self.from_udp info_hashes, data
@@ -1,3 +1,5 @@
1
+ require 'bencode'
2
+
1
3
  module Torckapi
2
4
  module Response
3
5
 
@@ -11,12 +13,30 @@ module Torckapi
11
13
  # @param data [String] UDP response data (omit action and transaction_id)
12
14
  # @return [Torckapi::Response::Scrape] response
13
15
  def self.from_udp info_hashes, data
14
- raise ArgumentError, "data does not match info_hashes" if data.length != info_hashes.count * 12
15
- 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)] })]
16
+ raise Torckapi::ArgumentError, "data does not match info_hashes" if data.length != info_hashes.count * 12
17
+ new Hash[info_hashes.zip(data.unpack('a12' * info_hashes.count).map { |i| counts_unpacked(i) })]
18
+ end
19
+
20
+ def self.from_http data
21
+ bdecoded_data = BEncode.load(data)
22
+ raise Torckapi::ArgumentError, "invalid data" unless bdecoded_data.is_a? Hash and bdecoded_data.has_key? 'files'
23
+ new Hash[bdecoded_data["files"].map { |info_hash, counts| [info_hash.unpack('H*').join, counts_translated(counts) ]}]
16
24
  end
17
25
 
18
26
  private
19
27
 
28
+ def self.counts_unpacked data
29
+ counts_with_block(data, lambda { |data| data.unpack('L>3').map(&:to_i) })
30
+ end
31
+
32
+ def self.counts_translated data
33
+ counts_with_block(data, lambda { |data| data.values_at("complete", "downloaded", "incomplete") })
34
+ end
35
+
36
+ def self.counts_with_block data, block
37
+ Hash[[:seeders, :completed, :leechers].zip(block.call(data))]
38
+ end
39
+
20
40
  def initialize data
21
41
  @data = data
22
42
  end
@@ -9,9 +9,7 @@ module Torckapi
9
9
  # (see Base#announce)
10
10
  def announce info_hash
11
11
  super info_hash
12
-
13
- @url.query ||= ""
14
- @url.query += "info_hash=%s" % URI.encode([info_hash].pack('H*'))
12
+ @url.query += info_hash_params [info_hash]
15
13
 
16
14
  Torckapi::Response::Announce.from_http info_hash, Net::HTTP.get(@url)
17
15
  end
@@ -19,11 +17,23 @@ module Torckapi
19
17
  # (see Base#scrape)
20
18
  def scrape info_hashes=[]
21
19
  super info_hashes
20
+ @url.query += info_hash_params info_hashes
21
+ @url.path.gsub!(/announce/, 'scrape')
22
22
 
23
- raise NotImplementedError
23
+ Torckapi::Response::Scrape.from_http Net::HTTP.get(@url)
24
24
  end
25
25
 
26
26
  private
27
+
28
+ def initialize url, options={}
29
+ super url, options
30
+
31
+ @url.query ||= ""
32
+ end
33
+
34
+ def info_hash_params info_hashes
35
+ info_hashes.map { |i| "info_hash=%s" % URI.encode([i].pack('H*')) }.join('&')
36
+ end
27
37
  end
28
38
  end
29
39
  end
@@ -1,3 +1,3 @@
1
1
  module Torckapi
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
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.12
4
+ version: 0.0.13
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-12 00:00:00.000000000 Z
11
+ date: 2013-04-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake