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 CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.0.5
@@ -3,10 +3,19 @@
3
3
  require File.dirname(__FILE__) + '/../lib/opentracker-ws'
4
4
  require 'thin'
5
5
 
6
- rackup_file = "#{File.dirname(__FILE__)}/../config.ru"
6
+ begin
7
+ config_file = File.read(ENV['HOME'] + '/.opentracker-ws.yml')
8
+ YAML.load(config_file)
7
9
 
8
- argv = ARGV
9
- argv << ['-R', rackup_file] unless ARGV.include?('-R')
10
- argv << ['-p', '2323'] unless ARGV.include?('-p')
11
- Thin::Runner.new(argv.flatten).run!
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
@@ -1,3 +1,3 @@
1
1
  require File.dirname(__FILE__) + '/lib/opentracker-ws'
2
- OpenTrackerWs.run! :port => 2323
2
+ OpenTrackerWs.run! :port => 2323, :environment => 'production'
3
3
 
@@ -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(hash)
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(hash)
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
- private
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
- BEncode.load(data)
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
- open("http://#{config['opentracker']['host']}:#{config['opentracker']['port']}" +
40
- "/scrape?info_hash=#{info_hash_escaped}").read
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
- begin
45
- config_file = File.read(ENV['HOME'] + '/.opentracker-ws.yml')
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
 
@@ -12,11 +12,9 @@ class OpenTrackerWs < Sinatra::Base
12
12
  end
13
13
 
14
14
  get '/torrent/:hash' do
15
- response = {}
16
- response['seeders'] = get_seeders(params[:hash])
17
- response['leechers'] = get_leechers(params[:hash])
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
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{opentracker-ws}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Antonio Pardo"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Antonio Pardo