jmoses-transmission-client 0.0.2

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+ transmission-client.gemspec
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dominik Sander
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,60 @@
1
+ # transmission-client: A Transmission RPC Client
2
+
3
+ **Please note, with the current release i dropped support for the blocking api. Eventmachine is now required.**
4
+
5
+ The goal is to support all requests described in the Transmission [RPC Specifications](http://trac.transmissionbt.com/browser/trunk/doc/rpc-spec.txt).
6
+
7
+ ## Installing
8
+ You need to have http://gemcutter.org in you gem sources. To add it you can execute either
9
+
10
+ sudo gem install gemcutter
11
+ sudo gem tumble
12
+
13
+ or
14
+
15
+ sudo gem source -a http://gemcutter.org
16
+
17
+ To install transmission-client:
18
+
19
+ sudo gem install transmission-client
20
+
21
+ ## Usage
22
+ Get a list of torrents and print its file names:
23
+
24
+ require 'transmission-client'
25
+
26
+ EventMachine.run do
27
+ t = Transmission::Client.new
28
+ EM.add_periodic_timer(1) do
29
+ t.torrents do |torrents|
30
+ torrents.each do |tor|
31
+ puts tor.percentDone
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ Authentication support (thanks hornairs):
38
+
39
+ t = Transmission::Client.new('127.0.0.1', 9091, 'username', 'password')
40
+
41
+ Callbacks:
42
+
43
+ EventMachine.run do
44
+ t = Transmission::Client.new
45
+
46
+ t.on_download_finished do |torrent|
47
+ puts "Wha torrent finished"
48
+ end
49
+ t.on_torrent_stopped do |torrent|
50
+ puts "Oooh torrent stopped"
51
+ end
52
+ t.on_torrent_started do |torrent|
53
+ puts "Torrent started."
54
+ end
55
+ t.on_torrent_removed do |torrent|
56
+ puts "Darn torrent deleted."
57
+ end
58
+ end
59
+
60
+ RDoc is still to be written, at the meantime have a look at the code to find out which methods are supported.
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "jmoses-transmission-client"
8
+ gem.summary = %Q{A Transmission RPC Client}
9
+ #gem.description = %Q{}
10
+ gem.email = "jon@burningbush.us"
11
+ gem.homepage = "http://github.com/jmoses/transmission-client"
12
+ gem.authors = ["Dominik Sander", "Jon Moses"]
13
+ gem.add_dependency "em-http-request"
14
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
15
+ gem.files += Dir['lib/**/*.rb','README.markdown']
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "transmission-rpc #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,12 @@
1
+ require 'net/http'
2
+ require 'singleton'
3
+ require 'json'
4
+ require 'rubygems'
5
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
6
+
7
+ require 'transmission-client/client'
8
+ require 'em-http'
9
+ require 'transmission-client/em-connection'
10
+ require 'transmission-client/torrent'
11
+ require 'transmission-client/session'
12
+
@@ -0,0 +1,106 @@
1
+ module Transmission
2
+ class Client
3
+ def on_download_finished(&blk); @on_download_finished = blk; callback_initialized; end
4
+ def on_torrent_added(&blk); @on_torrent_added = blk; callback_initialized; end
5
+ def on_torrent_stopped(&blk); @on_torrent_stopped = blk; callback_initialized; end
6
+ def on_torrent_started(&blk); @on_torrent_started = blk; callback_initialized; end
7
+ def on_torrent_removed(&blk); @on_torrent_removed = blk; callback_initialized; end
8
+
9
+ def initialize(host='localhost',port=9091, username = nil, password = nil)
10
+ Connection.init(host, port, username, password)
11
+ @torrents = nil
12
+ end
13
+
14
+ def start_all &cb
15
+ Connection.send('torrent-start')
16
+ end
17
+
18
+ def start(id)
19
+ Connection.send('torrent-start', {'ids' => id.class == Array ? id : [id]})
20
+ end
21
+
22
+ def stop(id)
23
+ Connection.send('torrent-stop', {'ids' => id.class == Array ? id : [id]})
24
+ end
25
+
26
+ def stop_all &cb
27
+ Connection.send('torrent-stop')
28
+ end
29
+
30
+ def remove(id, delete_data = false)
31
+ Connection.send('torrent-remove', {'ids' => id.class == Array ? id : [id], 'delete-local-data' => delete_data })
32
+ end
33
+
34
+ def remove_all(delete_data = false)
35
+ Connection.send('torrent-remove', {'delete-local-data' => delete_data })
36
+ end
37
+
38
+ def add_torrent(a)
39
+ if a['filename'].nil? && a['metainfo'].nil?
40
+ raise "You need to provide either a 'filename' or 'metainfo'."
41
+ end
42
+ Connection.send('torrent-add', a)
43
+ end
44
+
45
+ def add_torrent_by_file(filename)
46
+ add_torrent({'filename' => filename})
47
+ end
48
+
49
+ def add_torrent_by_data(data)
50
+ add_torrent({'metainfo' => data})
51
+ end
52
+
53
+ def session
54
+ Connection.request('session-get') { |resp| yield Session.new resp }
55
+ end
56
+
57
+ def torrents(fields = nil)
58
+ Connection.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES}) { |resp|
59
+ torrs = []
60
+ resp['torrents'].each do |t|
61
+ torrs << Torrent.new(t)
62
+ end
63
+ yield torrs
64
+ }
65
+ end
66
+
67
+ private
68
+ def callback_initialized
69
+ return if @torrent_poller
70
+ @torrent_poller = EM.add_periodic_timer(1) do
71
+ updated_torrents = {}
72
+ self.torrents do |tors|
73
+ tors.each do |torrent|
74
+ updated_torrents[torrent.id] = torrent
75
+ end
76
+ compare_torrent_status updated_torrents
77
+ @torrents = updated_torrents.dup
78
+ end
79
+
80
+
81
+ end
82
+ end
83
+
84
+ def compare_torrent_status updated_torrents
85
+ return false unless @torrents
86
+ updated_torrents.each_pair do |id, t|
87
+ old = @torrents[t.id] if @torrents[t.id]
88
+ if old == nil
89
+ @on_torrent_started.call t if @on_torrent_started
90
+ elsif old.downloading? && t.seeding?
91
+ @on_download_finished.call t if @on_download_finished
92
+ elsif old.stopped? && !t.stopped?
93
+ @on_torrent_started.call t if @on_torrent_started
94
+ elsif !old.stopped? && t.stopped?
95
+ @on_torrent_stopped.call t if @on_torrent_stopped
96
+ end
97
+ @torrents.delete t.id
98
+ end
99
+ if @torrents.length > 0 && @on_torrent_removed
100
+ @torrents.values.each do |t|
101
+ @on_torrent_removed.call t
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,57 @@
1
+ module Transmission
2
+ class Connection
3
+ class <<self
4
+ def init(host, port, username = nil, password = nil)
5
+ @host = host
6
+ @port = port
7
+ @header = username.nil? ? {} : {'authorization' => [username, password]}
8
+ uri = URI.parse("http://#{@host}:#{@port}/transmission/rpc")
9
+ @conn = EventMachine::HttpRequest.new(uri)
10
+ end
11
+
12
+ def request(method, attributes={})
13
+ req = @conn.post(:body => build_json(method,attributes), :head => @header )
14
+ req.callback {
15
+ case req.response_header.status
16
+ when 401
17
+ raise SecurityError, 'The client was not able to authenticate, is your username or password wrong?'
18
+ when 409 #&& @header['x-transmission-session-id'].nil?
19
+ @header['x-transmission-session-id'] = req.response_header['X_TRANSMISSION_SESSION_ID']
20
+ request(method,attributes) do |resp|
21
+ yield resp
22
+ end
23
+ when 200
24
+ resp = JSON.parse(req.response)
25
+ if resp["result"] == 'success'
26
+ yield resp['arguments']
27
+ else
28
+ yield resp
29
+ end
30
+ end
31
+ }
32
+ req.errback {
33
+ STDERR.puts "Response Start ->"
34
+ STDERR.puts req.response
35
+ STDERR.puts "<- Response End"
36
+
37
+ raise "Unknown response."
38
+ }
39
+ end
40
+
41
+ def send(method, attributes={})
42
+ request(method, attributes) do |resp|
43
+ yield resp
44
+ end
45
+ end
46
+
47
+ def build_json(method,attributes = {})
48
+ if attributes.length == 0
49
+ {'method' => method}.to_json
50
+ else
51
+ {'method' => method, 'arguments' => attributes }.to_json
52
+ end
53
+ end
54
+
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,22 @@
1
+ module Transmission
2
+ class Session
3
+ ATTRIBUTES = ['alt-speed-down', 'alt-speed-enabled', 'alt-speed-time-begin', 'alt-speed-time-enabled', 'alt-speed-time-end', 'alt-speed-time-day', 'alt-speed-up', 'blocklist-enabled', 'blocklist-size', 'download-dir', 'dht-enabled', 'encryption', 'incomplete-dir', 'incomplete-dir-enabled', 'peer-limit-global', 'peer-limit-per-torrent', 'pex-enabled', 'peer-port', 'peer-port-random-on-start', 'port-forwarding-enabled', 'rpc-version', 'rpc-version-minimum', 'seedRatioLimit', 'seedRatioLimited', 'speed-limit-down', 'speed-limit-down-enabled', 'speed-limit-up', 'speed-limit-up-enabled', 'version']
4
+ def initialize(attributes)
5
+ @attributes = attributes
6
+ end
7
+
8
+ def method_missing(m, *args, &block)
9
+ m = m.to_s.gsub('_','-')
10
+ if ATTRIBUTES.include? m
11
+ return @attributes[m]
12
+ elsif m[-1..-1] == '='
13
+ if ["blocklist-size","rpc-version", "rpc-version-minimum", "version"].include? m[0..-2]
14
+ raise "Invalid Attribute."
15
+ end
16
+ return Connection.instance.send('session-set', {m[0..-2] => args.first})
17
+ else
18
+ raise "Invalid Attribute."
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,74 @@
1
+ module Transmission
2
+ class Torrent
3
+ ATTRIBUTES = ['activityDate', 'addedDate', 'bandwidthPriority', 'comment', 'corruptEver', 'creator', 'dateCreated', 'desiredAvailable', 'doneDate', 'downloadDir', 'downloadedEver', 'downloadLimit', 'downloadLimited', 'error', 'errorString', 'eta', 'hashString', 'haveUnchecked', 'haveValid', 'honorsSessionLimits', 'id', 'isPrivate', 'leftUntilDone', 'manualAnnounceTime', 'maxConnectedPeers', 'name', 'peer-limit', 'peersConnected', 'peersGettingFromUs', 'peersKnown', 'peersSendingToUs', 'percentDone', 'pieces', 'pieceCount', 'pieceSize', 'rateDownload', 'rateUpload', 'recheckProgress', 'seedRatioLimit', 'seedRatioMode', 'sizeWhenDone', 'startDate', 'status', 'swarmSpeed', 'totalSize', 'torrentFile', 'uploadedEver', 'uploadLimit', 'uploadLimited', 'uploadRatio', 'webseedsSendingToUs', 'files']
4
+ ADV_ATTRIBUTES = ['fileStats', 'peers', 'peersFrom', 'priorities', 'trackers', 'trackerStats', 'wanted', 'webseeds']
5
+ SETABLE_ATTRIBUTES = ['bandwidthPriority', 'downloadLimit', 'downloadLimited', 'files-wanted', 'files-unwanted', 'honorsSessionLimits', 'ids', 'location', 'peer-limit', 'priority-high', 'priority-low', 'priority-normal', 'seedRatioLimit', 'seedRatioMode', 'uploadLimit', 'uploadLimited']
6
+ CHECK_WAIT = 1
7
+ CHECK = 2
8
+ DOWNLOAD = 4
9
+ SEED = 8
10
+ STOPPED = 16
11
+
12
+ def initialize(attributes)
13
+ @attributes = attributes
14
+ @adv_attributes = {}
15
+ end
16
+
17
+ def start
18
+ Connection.send('torrent-start', {'ids' => @attributes['id']})
19
+ end
20
+
21
+ def stop
22
+ Connection.send('torrent-stop', {'ids' => @attributes['id']})
23
+ end
24
+
25
+ def verify
26
+ Connection.send('torrent-verify', {'ids' => @attributes['id']})
27
+ end
28
+
29
+ def reannounce
30
+ Connection.send('torrent-reannounce', {'ids' => @attributes['id']})
31
+ end
32
+
33
+ def remove(delete_data = false)
34
+ Connection.send('torrent-remove', {'ids' => @attributes['id'], 'delete-local-data' => delete_data })
35
+ end
36
+
37
+ def downloading?
38
+ self.status == DOWNLOAD
39
+ end
40
+
41
+ def stopped?
42
+ self.status == STOPPED
43
+ end
44
+
45
+ def checking?
46
+ self.status == CHECK || self.status == CHECK_WAIT
47
+ end
48
+
49
+ def seeding?
50
+ self.status == SEED
51
+ end
52
+
53
+ def id
54
+ @attributes['id']
55
+ end
56
+
57
+
58
+ def method_missing(m, *args, &block)
59
+ if ATTRIBUTES.include? m.to_s
60
+ return @attributes[m.to_s]
61
+ elsif ADV_ATTRIBUTES.include? m.to_s
62
+ raise "Can't access that yet."
63
+ elsif m[-1..-1] == '='
64
+ if SETABLE_ATTRIBUTES.include? m[0..-2]
65
+ Connection.send('torrent-set', {'ids' => [@attributes['id']], m[0..-2] => args.first})
66
+ else
67
+ raise "Invalid Attribute."
68
+ end
69
+ else
70
+ raise "Invalid Attribute."
71
+ end
72
+ end
73
+ end # end class
74
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'transmission-rpc'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestTransmissionRpc < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jmoses-transmission-client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Dominik Sander
14
+ - Jon Moses
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-10-05 00:00:00 -04:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ name: em-http-request
24
+ prerelease: false
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: thoughtbot-shoulda
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description:
51
+ email: jon@burningbush.us
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files:
57
+ - LICENSE
58
+ - README.markdown
59
+ files:
60
+ - .document
61
+ - .gitignore
62
+ - LICENSE
63
+ - README.markdown
64
+ - Rakefile
65
+ - VERSION
66
+ - lib/transmission-client.rb
67
+ - lib/transmission-client/client.rb
68
+ - lib/transmission-client/em-connection.rb
69
+ - lib/transmission-client/session.rb
70
+ - lib/transmission-client/torrent.rb
71
+ - test/helper.rb
72
+ - test/test_transmission-rpc.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/jmoses/transmission-client
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ hash: 3
97
+ segments:
98
+ - 0
99
+ version: "0"
100
+ requirements: []
101
+
102
+ rubyforge_project:
103
+ rubygems_version: 1.3.7
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: A Transmission RPC Client
107
+ test_files:
108
+ - test/helper.rb
109
+ - test/test_transmission-rpc.rb