opentracker-ws 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bin/opentracker-ws +14 -5
- data/config.ru +1 -1
- data/lib/helpers.rb +36 -17
- data/lib/opentracker-ws.rb +3 -5
- data/opentracker-ws.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/bin/opentracker-ws
CHANGED
@@ -3,10 +3,19 @@
|
|
3
3
|
require File.dirname(__FILE__) + '/../lib/opentracker-ws'
|
4
4
|
require 'thin'
|
5
5
|
|
6
|
-
|
6
|
+
begin
|
7
|
+
config_file = File.read(ENV['HOME'] + '/.opentracker-ws.yml')
|
8
|
+
YAML.load(config_file)
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
argv
|
11
|
-
|
10
|
+
rackup_file = "#{File.dirname(__FILE__)}/../config.ru"
|
11
|
+
|
12
|
+
argv = ARGV
|
13
|
+
argv << ['-R', rackup_file] unless ARGV.include?('-R')
|
14
|
+
argv << ['-p', '2323'] unless ARGV.include?('-p')
|
15
|
+
argv << ['-e', 'production'] unless ARGV.include?('-e')
|
16
|
+
Thin::Runner.new(argv.flatten).run!
|
17
|
+
|
18
|
+
rescue Errno::ENOENT => e
|
19
|
+
puts e.message
|
20
|
+
end
|
12
21
|
|
data/config.ru
CHANGED
data/lib/helpers.rb
CHANGED
@@ -6,9 +6,21 @@ require 'yaml'
|
|
6
6
|
|
7
7
|
|
8
8
|
module OpenTrackerWsHelpers
|
9
|
+
|
10
|
+
def stats_for_torrent(hash)
|
11
|
+
response = {}
|
12
|
+
if data = prepare_data(hash)
|
13
|
+
response['seeders'] = get_seeders(data)
|
14
|
+
response['leechers'] = get_leechers(data)
|
15
|
+
response['downloaded'] = get_downloaded(data)
|
16
|
+
end
|
17
|
+
|
18
|
+
return response
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
9
22
|
|
10
|
-
def get_seeders(
|
11
|
-
data = prepare_data(hash)
|
23
|
+
def get_seeders(data)
|
12
24
|
if !data['files'].empty?
|
13
25
|
data['files'].values.first['complete']
|
14
26
|
else
|
@@ -16,37 +28,44 @@ module OpenTrackerWsHelpers
|
|
16
28
|
end
|
17
29
|
end
|
18
30
|
|
19
|
-
def get_leechers(
|
20
|
-
data = prepare_data(hash)
|
31
|
+
def get_leechers(data)
|
21
32
|
if !data['files'].empty?
|
22
33
|
data['files'].values.first['incomplete']
|
23
34
|
else
|
24
35
|
"N/A"
|
25
36
|
end
|
26
37
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
38
|
+
|
39
|
+
def get_downloaded(data)
|
40
|
+
if !data['files'].empty?
|
41
|
+
data['files'].values.first['downloaded']
|
42
|
+
else
|
43
|
+
"N/A"
|
44
|
+
end
|
45
|
+
end
|
30
46
|
|
31
47
|
def prepare_data(hash)
|
32
|
-
data = connect_to_tracker(hash)
|
33
|
-
|
48
|
+
if data = connect_to_tracker(hash)
|
49
|
+
BEncode.load(data)
|
50
|
+
else
|
51
|
+
return nil
|
52
|
+
end
|
34
53
|
end
|
35
54
|
|
36
55
|
def connect_to_tracker(hash)
|
37
56
|
config = load_config
|
38
57
|
info_hash_escaped = CGI::escape(hash.lines.to_a.pack("H*"))
|
39
|
-
|
40
|
-
|
58
|
+
begin
|
59
|
+
open("http://#{config['opentracker']['host']}:#{config['opentracker']['port']}" +
|
60
|
+
"/scrape?info_hash=#{info_hash_escaped}").read
|
61
|
+
rescue OpenURI::HTTPError
|
62
|
+
return nil
|
63
|
+
end
|
41
64
|
end
|
42
65
|
|
43
66
|
def load_config
|
44
|
-
|
45
|
-
|
46
|
-
YAML.load(config_file)
|
47
|
-
rescue Errno::ENOENT => e
|
48
|
-
puts e.message
|
49
|
-
end
|
67
|
+
config_file = File.read(ENV['HOME'] + '/.opentracker-ws.yml')
|
68
|
+
YAML.load(config_file)
|
50
69
|
end
|
51
70
|
end
|
52
71
|
|
data/lib/opentracker-ws.rb
CHANGED
@@ -12,11 +12,9 @@ class OpenTrackerWs < Sinatra::Base
|
|
12
12
|
end
|
13
13
|
|
14
14
|
get '/torrent/:hash' do
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
return JSON.generate(response)
|
15
|
+
# In production send JSON with correct content_type
|
16
|
+
#content_type 'application/json', :charset => 'utf-8'
|
17
|
+
return JSON.generate(stats_for_torrent(params[:hash]))
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
data/opentracker-ws.gemspec
CHANGED