remotransmission 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env ruby
2
+ # RemoTransmission command line interface to control
3
+ # a remote transmission client.
4
+ #
5
+ # Examples:
6
+ # $ remotransmission -p PaSsWord --add 'magnet://...'
7
+ # success
8
+ # $ remotransmission -p PaSsWord --server 214.512.12.20 --port 9092 --list
9
+ # 100% - ubuntu-10.10-desktop-i386.iso
10
+ # 100% - ubuntu-10.10-server-i386.iso
11
+
12
+
13
+ require 'remotransmission'
14
+
15
+ Choice.options do
16
+ header ""
17
+ header 'Commands:'
18
+ option :add do
19
+ short '-a'
20
+ long '--add=URL'
21
+ desc 'Add a torrent by its URL'
22
+ end
23
+
24
+ option :list do
25
+ short '-l'
26
+ long '--list'
27
+ desc "List current torrents"
28
+ end
29
+
30
+ option :help do
31
+ #short '-h'
32
+ long '--help'
33
+ desc 'Show this message'
34
+ end
35
+
36
+ option :version do
37
+ long '--version'
38
+ desc 'Show version'
39
+ action do
40
+ puts "RemoTransmission #{RemoTransmission::Version::STRING}"
41
+ exit
42
+ end
43
+ end
44
+
45
+ separator ""
46
+ separator 'Common options:'
47
+ option :host do
48
+ short '-s'
49
+ long '--server=IP'
50
+ desc 'The hostname or ip of the host to bind to (default 192.168.0.254)'
51
+ default '192.168.0.254'
52
+ end
53
+
54
+ option :port do
55
+ long '--port=PORT'
56
+ desc 'The port to bind to (default 9091)'
57
+ cast Integer
58
+ default 9091
59
+ end
60
+
61
+ option :user do
62
+ short '-u'
63
+ long '--user=USER'
64
+ desc 'User to authenticate (default freebox)'
65
+ default 'freebox'
66
+ end
67
+
68
+ option :password do
69
+ short '-p'
70
+ long '--password=PASSWORD'
71
+ desc 'Password to authenticate'
72
+ end
73
+
74
+ option :debug do
75
+ short '-d'
76
+ long '--debug'
77
+ desc 'Enable debug mode'
78
+ end
79
+
80
+ separator ""
81
+ separator "Examples:"
82
+ separator " $ #{File.basename($0)} -p PaSsWord --add 'magnet://...'"
83
+ separator " success"
84
+ separator " $ #{File.basename($0)} -p PaSsWord --server 214.512.12.20 --port 9092 --list"
85
+ separator " 100% - ubuntu-10.10-desktop-i386.iso"
86
+ separator " 100% - ubuntu-10.10-server-i386.iso"
87
+ end
88
+
89
+
90
+ client = RemoTransmission::Client.new(
91
+ Choice[:host],
92
+ Choice[:port],
93
+ Choice[:user],
94
+ Choice[:password],
95
+ Choice[:debug]
96
+ )
97
+
98
+ if Choice[:add]
99
+ client.add(Choice[:add])
100
+ elsif Choice[:list]
101
+ client.list
102
+ else
103
+ Choice.help
104
+ end
@@ -0,0 +1,110 @@
1
+ # The main Remote Transmission driver
2
+ class RemoTransmission::Client
3
+ # Initialize a remote transmission
4
+ #
5
+ # Arguments:
6
+ # host: host to connect to (String)
7
+ # port: port to connect to (Integer)
8
+ # user: username to authenticate to (String)
9
+ # password: password to authenticate to (String)
10
+ # debug: flag to turn on debugging (Integer)
11
+ def initialize(host = "localhost", port = 9091, user = "", password = "", debug = false)
12
+ @host = host
13
+ @port = port
14
+ @user = user
15
+ @password = password
16
+ @debug = debug
17
+ end
18
+
19
+ # Add a torrent to the transmission client
20
+ # and prints output.
21
+ #
22
+ # Arguments:
23
+ # url: magnet URL or URL of torrent file
24
+ #
25
+ # Example:
26
+ # >> transmission.add("magnet://..."")
27
+ # success
28
+ def add(url)
29
+ add = rpc(
30
+ method: "torrent-add",
31
+ tag: 8,
32
+ arguments: {
33
+ filename: url,
34
+ }
35
+ )
36
+ result = add["result"]
37
+ puts result
38
+ end
39
+
40
+ # Prints all active torrents
41
+ #
42
+ # Example:
43
+ # >> transmission.list
44
+ # 100% - ubuntu-10.10-desktop-i386.iso
45
+ # 100% - ubuntu-10.10-server-i386.iso
46
+ def list
47
+ list = rpc(
48
+ method: "torrent-get",
49
+ tag: 4,
50
+ arguments: {
51
+ #fields: ["error","errorString","eta","id","isFinished","leftUntilDone","name","peersGettingFromUs","peersSendingToUs","rateDownload","rateUpload","sizeWhenDone","status","uploadRatio"],
52
+ fields: ["isFinished","leftUntilDone","sizeWhenDone", "name"],
53
+ }
54
+ )
55
+
56
+ args = list["arguments"]
57
+ torrents = args["torrents"]
58
+ torrents.each do |torrent|
59
+ left = torrent["leftUntilDone"].to_f
60
+ size = torrent["sizeWhenDone"].to_f
61
+ pourcent = size != 0 ? ((size-left)*100/size).floor : "?"
62
+ puts "#{pourcent}% - #{torrent["name"]}"
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def command(*cmd)
69
+ debug *cmd
70
+ exit_status = nil
71
+ err = nil
72
+ out = nil
73
+
74
+ Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thread|
75
+ err = stderr.gets(nil)
76
+ out = stdout.gets(nil)
77
+ [stdin, stdout, stderr].each { |stream|
78
+ stream.send('close')
79
+ }
80
+ exit_status = wait_thread.value
81
+ end
82
+
83
+ if exit_status.to_i > 0
84
+ err = err.chomp if err
85
+ raise ShellError, err
86
+ elsif out
87
+ out.chomp
88
+ else
89
+ true
90
+ end
91
+ end
92
+
93
+ def rpc(options = {})
94
+ default_options = { arguments: { fields: ["error","errorString","eta","id","isFinished","leftUntilDone","name","peersGettingFromUs","peersSendingToUs","rateDownload","rateUpload","sizeWhenDone","status","uploadRatio"] }, tag: 4 }
95
+ options = default_options.merge(options).to_json
96
+
97
+ curl = command("curl -fsS -u #{@user}:#{@password} -d '#{options}' http://#{@host}:#{@port}/transmission/rpc")
98
+ json = JSON.parse(curl)
99
+ debug(json)
100
+ json
101
+ rescue ShellError => e
102
+ puts e.message
103
+ exit 1
104
+ end
105
+
106
+ def debug(*obj)
107
+ puts *obj if @debug
108
+ end
109
+ end
110
+
@@ -0,0 +1,8 @@
1
+ module RemoTransmission
2
+ module Version #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+ STRING = [MAJOR, MINOR, TINY] * '.'
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ require 'choice'
2
+ require 'json'
3
+ require 'open3'
4
+
5
+ module RemoTransmission
6
+ # :nodoc:
7
+ class ShellError < StandardError; end
8
+ end
9
+
10
+ require 'remotransmission/client'
11
+ require 'remotransmission/version'
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remotransmission
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sunny Ripert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-24 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: choice
17
+ requirement: &77320450 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: '0.1'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *77320450
26
+ - !ruby/object:Gem::Dependency
27
+ name: json
28
+ requirement: &77320180 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *77320180
37
+ description: A gem that can talk to remote Transmission demons
38
+ email: sunny@sunfox.org
39
+ executables:
40
+ - remotransmission
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - lib/remotransmission.rb
45
+ - lib/remotransmission/client.rb
46
+ - lib/remotransmission/version.rb
47
+ - bin/remotransmission
48
+ has_rdoc: true
49
+ homepage: http://github.com/sunny/remotransmission
50
+ licenses:
51
+ - MIT
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements:
69
+ - Curl command line
70
+ rubyforge_project:
71
+ rubygems_version: 1.6.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Remote Transmission
75
+ test_files: []