rtorrent_xmlrpc 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: daca72ab047033fdbdb755df64975cbf3ec9fb83
4
- data.tar.gz: 19f9811596b204dbef819081946ccd98d15044f2
3
+ metadata.gz: 2f0bfd0edd180624bed08aa55707e4c47c065b0b
4
+ data.tar.gz: 09d7015d51db1581c6215eeb91c2bd44cf04b58d
5
5
  SHA512:
6
- metadata.gz: 7444dae6f5fa0678b77b5937cfb08f137037a7658975f5abcf04749d197a0e42ff7e66152c6ae93cc9b35cab40273b52d85cdc6c6096111347ee89636df8a4bd
7
- data.tar.gz: b2b992429973f84dfcc44fed4a7579627134571c91d476797014ed8f45a6995ec5eb4208e4dbd4508d63a92752ef3ab1d94ca99165f8228ce6ee6969506cf9be
6
+ metadata.gz: 8d1a568e51254c94817b3893855982876241d6e5c3a20cd0ef55a47bdb03abfec86a15ccaed94626408fbed0481eb17fe818e22c9ef21ee665ba43b19b3bd4c5
7
+ data.tar.gz: 720660e8abf7a30f9c4396c210d9ac65af71f739f58fec5435a4e09d528e1ab54dbee694393ee898f81cbf0c3ad599288a26ae9f166f20f7b36184fe4ca513a9
data/Gemfile CHANGED
@@ -1,5 +1,3 @@
1
1
  source 'https://rubygems.org/'
2
2
 
3
- gem 'rake', '~> 10.4', '>= 10.4.2'
4
-
5
3
  gemspec
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # rtorrent_xmlrpc
2
+
3
+ **I seriously recommend you do NOT use this library until it has a v1.0
4
+ release. I am constantly changing things in it that will break functionality.**
5
+
6
+ This is a simple ruby library designed to pull basic information about torrents
7
+ from a remote rtorrent server using xmlrpc. It is not designed have complete
8
+ coverage of all features and methods provided by the xmlrpc interface. It is
9
+ only designed to have enough to manage your torrents from scripts. Included in
10
+ this gem is a binary that provides some basic functionality from the command
11
+ line in case you need to use it from non-ruby scripts.
data/Rakefile CHANGED
@@ -16,7 +16,23 @@ task :push do
16
16
  end
17
17
 
18
18
  task :console do
19
- exec "irb -I ./lib"
19
+ require 'hashie'
20
+ require 'pry'
21
+ require 'rtorrent_xmlrpc'
22
+
23
+ def load_xmlrpc
24
+ config = Hashie::Mash.load(File.expand_path('~/.config/rtorrent_xmlrpc.conf'))
25
+ RTorrent::XMLRPC.new_from_hash(config)
26
+ end
27
+
28
+ def reload!
29
+ # Change 'gem_name' here too:
30
+ files = $LOADED_FEATURES.select { |feat| feat =~ /\/rtorrent_xmlrpc\// }
31
+ files.each { |file| load file }
32
+ end
33
+
34
+ ARGV.clear
35
+ Pry.start
20
36
  end
21
37
 
22
38
  task :run do
data/bin/rtorrent_xmlrpc CHANGED
@@ -7,8 +7,6 @@ require 'thor'
7
7
 
8
8
  class RTorrent_XMLRPC < Thor
9
9
 
10
- class_option '--json', type: :boolean
11
-
12
10
  def initialize(*args)
13
11
  super
14
12
  # Read in config file
@@ -23,25 +21,28 @@ class RTorrent_XMLRPC < Thor
23
21
  config = Hashie::Mash.load(config_file)
24
22
  # Connect to xmlrpc service
25
23
  @xmlrpc = RTorrent::XMLRPC.new_from_hash(config.to_h)
26
- @xmlrpc.connect
27
- @xmlrpc.fetch_torrents
28
24
  end
29
25
 
30
- no_tasks do
31
- def print(torrents)
32
- if options[:json]
33
- puts torrents.to_json
34
- else
35
- torrents.each { |torrent| torrent.pp(true) }
36
- end
26
+ desc "print", "Print the torrent information"
27
+ option :any, aliases: '-a', type: :array
28
+ option :complete, aliases: '-c', type: :boolean
29
+ option :files, aliases: '-f', type: :boolean
30
+ option :incomplete, aliases: '-i', type: :boolean
31
+ option :json, aliases: '-j', type: :boolean
32
+ option :labels, aliases: '-l', type: :array
33
+ def print
34
+ torrents = @xmlrpc.torrents
35
+ torrents = torrents.complete if options[:complete]
36
+ torrents = torrents.incomplete if options[:incomplete]
37
+ torrents = torrents.with_labels options[:labels] if options[:labels]
38
+ torrents = torrents.with_any_labels options[:any] if options[:any]
39
+ if options[:json]
40
+ puts torrents.to_json
41
+ else
42
+ torrents.each { |hash, torrent| torrent.pp(options[:files]) }
37
43
  end
38
44
  end
39
45
 
40
- desc "list", "Pretty print all the torrents on the server."
41
- def list
42
- self.print @xmlrpc.torrents
43
- end
44
-
45
46
  desc "start HASH", "Start torrent with HASH"
46
47
  def start(hash)
47
48
  @xmlrpc.start hash
@@ -57,25 +58,21 @@ class RTorrent_XMLRPC < Thor
57
58
  @xmlrpc.pause hash
58
59
  end
59
60
 
60
- desc "complete", "Print all torrents that are complete."
61
- def complete
62
- self.print @xmlrpc.torrents.completed
61
+ desc "add_labels HASH LABELS", "Add labels to torrent identified by HASH"
62
+ def add_labels(hash, *labels)
63
+ @xmlrpc.add_labels(hash, labels)
63
64
  end
64
65
 
65
- desc "incomplete", "Print all incomplete torrents."
66
- def incomplete
67
- self.print @xmlrpc.incomplete
66
+ desc "remove_labels HASH LABELS", "Remove labels from torrent identified by HASH"
67
+ def remove_labels(hash, *labels)
68
+ @xmlrpc.remove_labels(hash, labels)
68
69
  end
69
70
 
70
- desc "labeled LABEL", "Print all torrents with label"
71
- def labeled(label)
72
- self.print @xmlrpc.torrents.with_label label
71
+ desc "set_labels HASH LABELS", "Label the torrent identified by HASH with LABELS"
72
+ def set_labels(hash, *labels)
73
+ @xmlrpc.set_labels(hash, labels)
73
74
  end
74
75
 
75
- desc "files HASH", "Print the torrent information with files."
76
- def files(hash)
77
- # TODO:
78
- end
79
76
  end
80
77
 
81
78
  RTorrent_XMLRPC.start(ARGV)
@@ -7,26 +7,41 @@ module RTorrent
7
7
 
8
8
  class Torrent
9
9
 
10
+ PRIORITIES = ['Off', 'Low', 'Normal', 'High']
11
+ PRIORITY_OFF = 0
12
+ PRIORITY_LOW = 1
13
+ PRIORITY_NORMAL = 2
14
+ PRIORITY_HIGH = 3
15
+
10
16
  def initialize
11
17
  @labels = []
12
18
  @files= []
13
19
  end
14
20
 
15
21
  # Attributes
16
- attr_accessor :base_filename, :base_path, :completed, :files, :hash, :is_multi_file, :name, :tied_to_file
17
- attr_reader :down_total, :labels, :priority, :ratio, :size, :up_total
22
+ attr_accessor :base_filename, :base_path, :completed, :down_total, :files, :hash, :is_multi_file, :name, :size, :tied_to_file, :up_total
23
+ attr_reader :labels, :priority, :ratio
18
24
 
19
25
  def completed?
20
26
  self.completed
21
27
  end
22
28
 
23
- def down_total=(down_total) # :nodoc:
24
- @down_total = Filesize.new(down_total)
29
+ def add_labels(labels)
30
+ labels = Array(labels)
31
+ labels.map!(&:strip)
32
+ self.labels = @labels | labels
33
+ end
34
+
35
+ def remove_labels(labels)
36
+ labels = Array(labels)
37
+ labels.map!(&:strip)
38
+ self.labels = @labels - labels
25
39
  end
26
40
 
27
41
  def labels=(labels) # :nodoc:
28
- @labels = labels.split(',')
29
- @labels.map! { |label| label.chomp }
42
+ labels = labels.split(',') if labels.is_a? String
43
+ labels.map!(&:strip)
44
+ @labels = labels
30
45
  end
31
46
 
32
47
  def labels_str
@@ -43,34 +58,11 @@ module RTorrent
43
58
 
44
59
  # Return priority converted to a human readable string
45
60
  def priority_str
46
- case @priority
47
- when 0
48
- "Off"
49
- when 1
50
- "Low"
51
- when 2
52
- "Normal"
53
- when 3
54
- "High"
55
- else
56
- "Unknown"
57
- end
61
+ PRIORITIES[@priority]
58
62
  end
59
63
 
60
64
  def ratio=(ratio) # :nodoc:
61
- begin
62
- @ratio = ratio.to_f
63
- rescue
64
- @ratio = 0.0
65
- end
66
- end
67
-
68
- def size=(size) # :nodoc:
69
- @size = Filesize.new(size)
70
- end
71
-
72
- def up_total=(up_total) # :nodoc:
73
- @up_total = Filesize.new(up_total)
65
+ @ratio = ratio.to_f
74
66
  end
75
67
 
76
68
  # Return hash of all values in Torrent
@@ -98,19 +90,20 @@ module RTorrent
98
90
 
99
91
  # All torrent data dumped to screen in color
100
92
  def pp(with_files = false)
101
- puts "-------------- ".red
102
- puts " hash: ".blue + self.hash.green
103
- puts " name: ".blue + self.name.green
104
- puts " size: ".blue + self.size.pretty.green
105
- puts " downloaded: ".blue + self.down_total.pretty.green
106
- puts " uploaded: ".blue + self.up_total.pretty.green
107
- puts " ratio: ".blue + self.ratio.to_s.green
108
- puts " priority: ".blue + self.priority_str.green
109
- puts " labels: ".blue + self.labels_str.green
110
- puts " completed: ".blue + self.completed.to_s.green
93
+ i = 12
94
+ puts ("-" * i).red
95
+ puts "%s %s" % ['hash:'.rjust(i).blue, self.hash.green]
96
+ puts "%s %s" % ['name:'.rjust(i).blue, self.name.green]
97
+ puts "%s %s" % ['size:'.rjust(i).blue, Filesize.new(self.size).pretty.green]
98
+ puts "%s %s" % ['downloaded:'.rjust(i).blue, Filesize.new(self.down_total).pretty.green]
99
+ puts "%s %s" % ['uploaded:'.rjust(i).blue, Filesize.new(self.up_total).pretty.green]
100
+ puts "%s %s" % ['ratio:'.rjust(i).blue, self.ratio.to_s.green]
101
+ puts "%s %s" % ['priority:'.rjust(i).blue, self.priority_str.green]
102
+ puts "%s %s" % ['labels:'.rjust(i).blue, self.labels_str.green]
103
+ puts "%s %s" % ['completed:'.rjust(i).blue, self.completed.to_s.green]
111
104
  if with_files
112
- puts " files: ".blue
113
- @files.each { |file| puts " " + file.green }
105
+ puts "%s %s" % ['files:'.rjust(i).blue, @files.first.green]
106
+ @files.each { |file| puts "%s %s" % [' ' * i, file.green] unless file == @files.first }
114
107
  end
115
108
  end
116
109
 
@@ -121,6 +114,11 @@ module RTorrent
121
114
  @labels.include? label.to_s
122
115
  end
123
116
 
117
+ # Test if torrent has any of the labels
118
+ def has_any_labels?(labels)
119
+ ! (@labels & labels).empty?
120
+ end
121
+
124
122
  end
125
123
 
126
124
  end
@@ -1,20 +1,33 @@
1
1
  module RTorrent
2
2
 
3
- class Torrents < Array
3
+ class Torrents < Hash
4
+
4
5
  def completed
5
6
  result = Torrents.new
6
- self.each do |torrent|
7
- result << torrent if torrent.completed?
7
+ self.each do |hash, torrent|
8
+ result[torrent.hash] = torrent if torrent.completed?
9
+ end
10
+ return result
11
+ end
12
+
13
+ def with_labels(labels)
14
+ labels = [labels] unless labels.is_a? Array
15
+ result = Torrents.new
16
+ self.each do |hash, torrent|
17
+ result[torrent.hash] = torrent if torrent.has_labels? labels
8
18
  end
9
19
  return result
10
20
  end
11
- def with_label(label)
21
+
22
+ def with_any_labels(labels)
23
+ labels = [labels] unless labels.is_a? Array
12
24
  result = Torrents.new
13
- self.each do |torrent|
14
- result << torrent if torrent.has_label? label
25
+ self.each do |hash, torrent|
26
+ result[torrent.hash] = torrent if torrent.has_any_labels? labels
15
27
  end
16
28
  return result
17
29
  end
30
+
18
31
  end
19
32
 
20
33
  end
@@ -1,4 +1,3 @@
1
- require 'hashie'
2
1
  require 'rtorrent_xmlrpc/torrent'
3
2
  require 'rtorrent_xmlrpc/torrents'
4
3
  require 'xmlrpc/client'
@@ -6,8 +5,8 @@ require 'xmlrpc/client'
6
5
  module RTorrent
7
6
 
8
7
  class XMLRPC
9
- attr_accessor :host, :user, :password, :path
10
- attr_reader :port, :use_ssl, :server, :torrents
8
+ attr_accessor :host, :user, :password, :path, :port
9
+ attr_reader :connection_options, :use_ssl, :server, :torrents
11
10
 
12
11
  def initialize(host: 'localhost', port: nil, user: nil, password: nil, path: '/xmlrpc', use_ssl: false)
13
12
  unless port
@@ -25,6 +24,8 @@ module RTorrent
25
24
  self.use_ssl = use_ssl
26
25
  @torrents = Torrents.new
27
26
  @status = :initialized
27
+ connect
28
+ fetch_torrents
28
29
  end
29
30
 
30
31
  def self.new_from_hash(hash = {})
@@ -33,18 +34,14 @@ module RTorrent
33
34
  self.new(host: h[:host], port: h[:port], user: h[:user], password: h[:password], path: h[:path], use_ssl: h[:use_ssl])
34
35
  end
35
36
 
36
- def port=(port)
37
- fail unless port.is_a? Integer
38
- @port = port
39
- end
40
-
41
37
  def use_ssl=(use_ssl)
42
- fail unless use_ssl.is_a?(TrueClass) || use_ssl.is_a?(FalseClass)
38
+ fail "use_ssl must be a boolean value" unless use_ssl.is_a?(TrueClass) || use_ssl.is_a?(FalseClass)
43
39
  @use_ssl = use_ssl
44
40
  end
45
41
 
46
42
  # Connect to rtorrent xmlrpc service
47
43
  def connect
44
+ return true if @status == :connected
48
45
  connection_options = {
49
46
  host: @host,
50
47
  port: @port,
@@ -57,6 +54,12 @@ module RTorrent
57
54
  @status = :connected
58
55
  end
59
56
 
57
+ # Disconnect from xmlrpc service
58
+ def disconnect
59
+ @server = nil
60
+ @status = :disconnected
61
+ end
62
+
60
63
  # Grab list of torrents from server
61
64
  def fetch_torrents
62
65
  self.connect unless @status == :connected
@@ -84,8 +87,8 @@ module RTorrent
84
87
  torrent.name = stats[1]
85
88
  torrent.labels = stats[2]
86
89
  torrent.completed = stats[3] == 1 ? true : false
87
- torrent.base_filename = stats[4].gsub(/^#{@trim_suffix}/, '')
88
- torrent.base_path = stats[5].gsub(/^#{@trim_suffix}/, '')
90
+ torrent.base_filename = stats[4]
91
+ torrent.base_path = stats[5]
89
92
  torrent.is_multi_file = stats[6] == 1 ? true: false
90
93
  torrent.tied_to_file = stats[7]
91
94
  torrent.size = stats[8]
@@ -93,8 +96,8 @@ module RTorrent
93
96
  torrent.up_total = stats[10]
94
97
  torrent.ratio = stats[11].to_f / 1000
95
98
  torrent.priority = stats[12]
96
- torrent.files = @server.call('f.multicall', torrent.hash, '', 'f.get_path=').flatten.map { |file| file.gsub(/^#{@trim_suffix}/, '') }
97
- @torrents << torrent
99
+ torrent.files = @server.call('f.multicall', torrent.hash, '', 'f.get_path=').flatten
100
+ @torrents[torrent.hash] = torrent
98
101
  end
99
102
  end
100
103
 
@@ -113,9 +116,23 @@ module RTorrent
113
116
  @server.call('d.stop', hash)
114
117
  end
115
118
 
119
+ # Add labels to torrent
120
+ def add_labels(hash, labels)
121
+ self.torrents[hash].add_labels(labels)
122
+ @server.call('d.set_custom1', hash, self.torrents[hash].labels_str)
123
+ end
124
+
125
+ # Remove labels from torrent
126
+ def remove_labels(hash, labels)
127
+ self.torrents[hash].remove_labels(labels)
128
+ @server.call('d.set_custom1', hash, self.torrents[hash].labels_str)
129
+ end
130
+
116
131
  # Set the custom1 (label) field for a torrent
117
132
  def set_labels(hash, labels)
118
- @server.call('d.set_custom1', hash, labels)
133
+ labels = [labels] unless labels.is_a? Array
134
+ @server.call('d.set_custom1', hash, labels.join(', '))
135
+ self.torrents[hash].labels = labels
119
136
  end
120
137
  end
121
138
 
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rtorrent_xmlrpc'
3
- s.version = '0.2.2'
4
- s.date = '2015-07-03'
3
+ s.version = '0.2.3'
4
+ s.date = '2015-07-04'
5
5
  s.summary = 'A library and tool to query an rtorrent xmlrpc service.'
6
6
  s.description = 'This is a library to get torrent information from a remote rtorrent server.'
7
7
  s.authors = ['Zan Loy']
@@ -16,4 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.add_dependency 'filesize', '~> 0.1', '>= 0.1.0'
17
17
  s.add_dependency 'thor', '~> 0.19', '>= 0.19.1'
18
18
 
19
+ s.add_development_dependency 'awesome_print', '~> 1.6', '>= 1.6.1'
20
+ s.add_development_dependency 'pry', '~> 0.10', '>= 0.10.1'
21
+ s.add_development_dependency 'rake', '~> 10.4', '>= 10.4.2'
19
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rtorrent_xmlrpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zan Loy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-03 00:00:00.000000000 Z
11
+ date: 2015-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -90,6 +90,66 @@ dependencies:
90
90
  - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: 0.19.1
93
+ - !ruby/object:Gem::Dependency
94
+ name: awesome_print
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '1.6'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 1.6.1
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '1.6'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 1.6.1
113
+ - !ruby/object:Gem::Dependency
114
+ name: pry
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '0.10'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 0.10.1
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.10'
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 0.10.1
133
+ - !ruby/object:Gem::Dependency
134
+ name: rake
135
+ requirement: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '10.4'
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 10.4.2
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - "~>"
148
+ - !ruby/object:Gem::Version
149
+ version: '10.4'
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: 10.4.2
93
153
  description: This is a library to get torrent information from a remote rtorrent server.
94
154
  email: zan.loy@gmail.com
95
155
  executables:
@@ -98,14 +158,13 @@ extensions: []
98
158
  extra_rdoc_files: []
99
159
  files:
100
160
  - Gemfile
101
- - Gemfile.lock
161
+ - README.md
102
162
  - Rakefile
103
163
  - bin/rtorrent_xmlrpc
104
164
  - lib/rtorrent_xmlrpc.rb
105
165
  - lib/rtorrent_xmlrpc/torrent.rb
106
166
  - lib/rtorrent_xmlrpc/torrents.rb
107
167
  - lib/rtorrent_xmlrpc/xmlrpc.rb
108
- - methods.txt
109
168
  - rtorrent_xmlrpc.conf.sample
110
169
  - rtorrent_xmlrpc.gemspec
111
170
  homepage: http://zanloy.com/ruby/rtorrent_xmlrpc/
data/Gemfile.lock DELETED
@@ -1,27 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- rtorrent_xmlrpc (0.2.1)
5
- colorize (~> 0.7, >= 0.7.7)
6
- filesize (~> 0.1, >= 0.1.0)
7
- hashie (~> 3.4, >= 3.4.2)
8
- thor (~> 0.19, >= 0.19.1)
9
-
10
- GEM
11
- remote: https://rubygems.org/
12
- specs:
13
- colorize (0.7.7)
14
- filesize (0.1.0)
15
- hashie (3.4.2)
16
- rake (10.4.2)
17
- thor (0.19.1)
18
-
19
- PLATFORMS
20
- ruby
21
-
22
- DEPENDENCIES
23
- rake (~> 10.4, >= 10.4.2)
24
- rtorrent_xmlrpc!
25
-
26
- BUNDLED WITH
27
- 1.10.5