hornairs-transmission-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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,49 @@
1
+ # transmission-client: A Transmission RPC Client
2
+
3
+ The goal is to support all requests described in the Transmission [RPC Specifications](http://trac.transmissionbt.com/browser/trunk/doc/rpc-spec.txt).
4
+
5
+ ## Installing
6
+ You need to have http://gemcutter.org in you gem sources. To add it you can execute either
7
+
8
+ sudo gem install gemcutter
9
+ sudo gem tumble
10
+
11
+ or
12
+
13
+ sudo gem source -a http://gemcutter.org
14
+
15
+ To install transmission-client:
16
+
17
+ sudo gem install transmission-client
18
+
19
+ If you want to use EventMachine (optional) you need to install the eventmachine gem and igrigorik's em-http-request:
20
+
21
+ sudo gem install eventmachine
22
+ sudo gem install em-http-request
23
+
24
+ ## Usage
25
+ Get a list of torrents and print its file names:
26
+
27
+ require 'transmission-client'
28
+ t = Transmission::Client.new('127.0.0.1', 9091)
29
+ t.torrents.each do |torrent|
30
+ puts torrent.name
31
+ end
32
+
33
+ To use the EventMachine driven interface:
34
+
35
+ require 'eventmachine'
36
+ require 'transmission-client'
37
+
38
+ EventMachine.run do
39
+ t = Transmission::Client.new
40
+ EM.add_periodic_timer(1) do
41
+ t.torrents do |torrents|
42
+ torrents.each do |tor|
43
+ puts tor.percentDone
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ RDoc is still to be written, at the meantime have a look at the code to find out which methods are supported.
data/README.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ # transmission-client: A Transmission RPC Client
2
+
3
+ The goal is to support all requests described in the Transmission [RPC Specifications](http://trac.transmissionbt.com/browser/trunk/doc/rpc-spec.txt).
4
+
5
+ ## Installing
6
+ You need to have http://gemcutter.org in you gem sources. To add it you can execute either
7
+
8
+ sudo gem install gemcutter
9
+ sudo gem tumble
10
+
11
+ or
12
+
13
+ sudo gem source -a http://gemcutter.org
14
+
15
+ To install transmission-client:
16
+
17
+ sudo gem install transmission-client
18
+
19
+ If you want to use EventMachine (optional) you need to install the eventmachine gem and igrigorik's em-http-request:
20
+
21
+ sudo gem install eventmachine
22
+ sudo gem install em-http-request
23
+
24
+ ## Usage
25
+ Get a list of torrents and print its file names:
26
+
27
+ require 'transmission-client'
28
+ t = Transmission::Client.new('127.0.0.1', 9091)
29
+ t.torrents.each do |torrent|
30
+ puts torrent.name
31
+ end
32
+
33
+ To use the EventMachine driven interface:
34
+
35
+ require 'eventmachine'
36
+ require 'transmission-client'
37
+
38
+ EventMachine.run do
39
+ t = Transmission::Client.new
40
+ EM.add_periodic_timer(1) do
41
+ t.torrents do |torrents|
42
+ torrents.each do |tor|
43
+ puts tor.percentDone
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ 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,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "hornairs-transmission-client"
8
+ gem.summary = %Q{A Transmission RPC Client}
9
+ #gem.description = %Q{}
10
+ gem.email = "git@dsander.de"
11
+ gem.homepage = "http://github.com/hornairs/transmission-client"
12
+ gem.authors = ["Dominik Sander"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.files += Dir['lib/**/*.rb','README.markdown']
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "transmission-rpc #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,5 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'transmission-client'))
2
+ require 'pp'
3
+ t = Transmission::Client.new('scurvvy.info', 9091, 'transmission', 'orturren33')
4
+ puts t.torrents[0].name
5
+ pp t.torrents[0].move("/var/downloads/movies")
@@ -0,0 +1,22 @@
1
+ require 'net/http'
2
+ require 'singleton'
3
+ require 'rubygems'
4
+ require 'json'
5
+
6
+ $:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
7
+
8
+ require 'transmission-client/client'
9
+
10
+ if defined? EM
11
+ begin
12
+ require 'em-http'
13
+ require 'transmission-client/em-connection'
14
+ rescue LoadError
15
+ require 'transmission-client/connection'
16
+ end
17
+ else
18
+ require 'transmission-client/connection'
19
+ end
20
+ require 'transmission-client/torrent'
21
+ require 'transmission-client/session'
22
+
@@ -0,0 +1,72 @@
1
+ module Transmission
2
+ class Client
3
+ def initialize(host='localhost',port=9091, username = "", password = "")
4
+ Connection.instance.init(host, port, username, password)
5
+ end
6
+
7
+ def start_all &cb
8
+ Connection.instance.send('torrent-start', &cb)
9
+ end
10
+
11
+ def start(id, &cb)
12
+ Connection.instance.send('torrent-start', {'ids' => id.class == Array ? id : [id]}, &cb)
13
+ end
14
+
15
+ def stop(id, &cb)
16
+ Connection.instance.send('torrent-stop', {'ids' => id.class == Array ? id : [id]}, &cb)
17
+ end
18
+
19
+ def stop_all &cb
20
+ Connection.instance.send('torrent-stop', &cb)
21
+ end
22
+
23
+ def remove(id, delete_data = false, &cb)
24
+ Connection.instance.send('torrent-remove', {'ids' => id.class == Array ? id : [id], 'delete-local-data' => delete_data }, &cb)
25
+ end
26
+
27
+ def remove_all(delete_data = false, &cb)
28
+ Connection.instance.send('torrent-remove', {'delete-local-data' => delete_data }, &cb)
29
+ end
30
+
31
+ def add_torrent(a, &cb)
32
+ if a['filename'].nil? && a['metainfo'].nil?
33
+ raise "You need to provide either a 'filename' or 'metainfo'."
34
+ end
35
+ Connection.instance.send('torrent-add', a, &cb)
36
+ end
37
+
38
+ def add_torrent_by_file(filename, &cb)
39
+ add_torrent({'filename' => filename}, &cb)
40
+ end
41
+
42
+ def add_torrent_by_data(data, &cb)
43
+ add_torrent({'metainfo' => data}, &cb)
44
+ end
45
+
46
+ def session &cb
47
+ if cb
48
+ Connection.instance.request('session-get') { |resp| cb.call Session.new resp }
49
+ else
50
+ Session.new Connection.instance.request('session-get')
51
+ end
52
+ end
53
+
54
+ def torrents(fields = nil, &cb)
55
+ torrs = []
56
+ if cb
57
+ Connection.instance.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES}) { |resp|
58
+ resp['torrents'].each do |t|
59
+ torrs << Torrent.new(t)
60
+ end
61
+ cb.call torrs
62
+ }
63
+ else
64
+ data = Connection.instance.request('torrent-get', {'fields' => fields ? fields : Transmission::Torrent::ATTRIBUTES})
65
+ data['torrents'].each do |t|
66
+ torrs << Torrent.new(t)
67
+ end
68
+ torrs
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,72 @@
1
+ module Transmission
2
+ class Connection
3
+ include Singleton
4
+
5
+ def init(host, port, username = "", password = "")
6
+ @host = host
7
+ @port = port
8
+ @username = username unless username == ""
9
+ @password = password unless password == ""
10
+ @header = {}
11
+ uri = URI.parse("http://#{@host}:#{@port}")
12
+ Net::HTTP.start(uri.host, uri.port) do |http|
13
+ @conn = http;
14
+ resp = http.request(build_request(), build_json('session_get'))
15
+ if resp.class == Net::HTTPUnauthorized
16
+ raise SecurityError, 'The client was not able to authenticate, is your username or password wrong?'
17
+ elsif resp.class == Net::HTTPConflict && @header['x-transmission-session-id'].nil?
18
+ @header['x-transmission-session-id'] = resp['x-transmission-session-id']
19
+ elsif resp.class == Net::HTTPOK
20
+ true
21
+ else
22
+ false
23
+ end
24
+ end
25
+ end
26
+
27
+ def build_request()
28
+ req = Net::HTTP::Post.new('/transmission/rpc')
29
+ if @username != ""
30
+ req.basic_auth @username, @password
31
+ end
32
+ if ! @header['x-transmission-session-id'].nil?
33
+ req.add_field 'x-transmission-session-id', @header['x-transmission-session-id']
34
+ end
35
+ req
36
+ end
37
+
38
+ def request(method, attributes={})
39
+ res = @conn.request(build_request, build_json(method, attributes))
40
+ if res.class == Net::HTTPConflict && @header['x-transmission-session-id'].nil?
41
+ @header['x-transmission-session-id'] = res['x-transmission-session-id']
42
+ request(method,attributes)
43
+ elsif res.class == Net::HTTPOK
44
+ resp = JSON.parse(res.body)
45
+ if resp["result"] == 'success'
46
+ #pp resp
47
+ resp['arguments']
48
+ else
49
+ resp
50
+ end
51
+ else
52
+ pp res
53
+ raise RuntimeError, 'Reponse received from the daemon was something transmission-client cant deal with! Panic!'
54
+ end
55
+ end
56
+
57
+ def send(method, attributes={})
58
+ data = request(method, attributes)['result']
59
+ pp data
60
+ data.nil?
61
+ end
62
+
63
+ def build_json(method,attributes = {})
64
+ if attributes.length == 0
65
+ {'method' => method}.to_json
66
+ else
67
+ {'method' => method, 'arguments' => attributes }.to_json
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,51 @@
1
+ module Transmission
2
+ class Connection
3
+ include Singleton
4
+ #include EM::Deferrable
5
+
6
+ def init(host, port)
7
+ @host = host
8
+ @port = port
9
+ uri = URI.parse("http://#{@host}:#{@port}/transmission/rpc")
10
+ #@conn = Net::HTTP.start(uri.host, uri.port)
11
+ @conn = EventMachine::HttpRequest.new(uri)
12
+ @header = {} #{"Accept-Encoding" => "deflate"} deflate is broken somewhere
13
+ end
14
+
15
+ def request(method, attributes={}, &cb)
16
+ req = @conn.post(:body => build_json(method,attributes), :head => @header )
17
+ req.callback {
18
+ if req.response_header.status == 409 #&& @header['x-transmission-session-id'].nil?
19
+ @header['x-transmission-session-id'] = req.response_header['X_TRANSMISSION_SESSION_ID']
20
+ request(method,attributes, &cb)
21
+ elsif req.response_header.status == 200
22
+ resp = JSON.parse(req.response)
23
+ if resp["result"] == 'success'
24
+ cb.call resp['arguments'] if cb
25
+ else
26
+ cb.call resp if cb
27
+ end
28
+ end
29
+ }
30
+ req.errback {
31
+ puts 'errback'
32
+ pp req
33
+ }
34
+ end
35
+
36
+ def send(method, attributes={}, &cb)
37
+ request(method, attributes) do |resp|
38
+ cb.call resp if cb
39
+ end
40
+ end
41
+
42
+ def build_json(method,attributes = {})
43
+ if attributes.length == 0
44
+ {'method' => method}.to_json
45
+ else
46
+ {'method' => method, 'arguments' => attributes }.to_json
47
+ end
48
+ end
49
+
50
+ end
51
+ 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,51 @@
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']
4
+ ADV_ATTRIBUTES = ['files', '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
+
7
+ def initialize(attributes)
8
+ @attributes = attributes
9
+ end
10
+
11
+ def start
12
+ Connection.instance.send('torrent-start', {'ids' => @attributes['id']})
13
+ end
14
+
15
+ def stop
16
+ Connection.instance.send('torrent-stop', {'ids' => @attributes['id']})
17
+ end
18
+
19
+ def verify
20
+ Connection.instance.send('torrent-verify', {'ids' => @attributes['id']})
21
+ end
22
+
23
+ def reannounce
24
+ Connection.instance.send('torrent-reannounce', {'ids' => @attributes['id']})
25
+ end
26
+
27
+ def remove(delete_data = false)
28
+ Connection.instance.send('torrent-remove', {'ids' => @attributes['id'], 'delete-local-data' => delete_data })
29
+ end
30
+
31
+ def move(location, move = true)
32
+ Connection.instance.send('torrent-set-location', {'ids' => @attributes['id'], 'location' => location, 'move' => move})
33
+ end
34
+
35
+ def method_missing(m, *args, &block)
36
+ if ATTRIBUTES.include? m.to_s
37
+ return @attributes[m.to_s]
38
+ elsif ADV_ATTRIBUTES.include? m.to_s
39
+ raise "Attribute not yet supported."
40
+ elsif m[-1..-1] == '='
41
+ if SETABLE_ATTRIBUTES.include? m[0..-2]
42
+ Connection.instance.send('torrent-set', {'ids' => [@attributes['id']], m[0..-2] => args.first})
43
+ else
44
+ raise "Invalid Attribute."
45
+ end
46
+ else
47
+ raise "Invalid Attribute."
48
+ end
49
+ end
50
+ end # end class
51
+ 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,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hornairs-transmission-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dominik Sander
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-25 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description:
26
+ email: git@dsander.de
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ - README.rdoc
35
+ files:
36
+ - .document
37
+ - .gitignore
38
+ - LICENSE
39
+ - README.markdown
40
+ - README.rdoc
41
+ - Rakefile
42
+ - VERSION
43
+ - lib/transmission-client.rb
44
+ - lib/transmission-client/client.rb
45
+ - lib/transmission-client/connection.rb
46
+ - lib/transmission-client/em-connection.rb
47
+ - lib/transmission-client/session.rb
48
+ - lib/transmission-client/torrent.rb
49
+ - test/helper.rb
50
+ - test/test_transmission-rpc.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/hornairs/transmission-client
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.3.5
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A Transmission RPC Client
79
+ test_files:
80
+ - test/helper.rb
81
+ - test/test_transmission-rpc.rb
82
+ - examples/connection.rb