transmission-rpc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in transmission-rpc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jarred Sumner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Transmission::RPC
2
+
3
+ A simplistic Transmission RPC client for Ruby. It implements part of [RPC 1.7.x](https://trac.transmissionbt.com/browser/branches/1.7x/doc/rpc-spec.txt).
4
+
5
+ You can:
6
+
7
+ * Add a torrent by URL or file path
8
+ * Start a torrent
9
+ * Stop a torrent
10
+ * Get all torrents
11
+ * Delete torrents from Transmission and from disk
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'transmission-rpc'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install transmission-rpc
28
+
29
+ It depends on ```transmission-daemon```. Install it from homebrew:
30
+
31
+ $ brew install transmission
32
+
33
+ ```transmission-daemon``` must be running, run it like so:
34
+
35
+ $ transmission-daemon
36
+
37
+ ## Usage
38
+
39
+ To add a torrent by URL or file path:
40
+
41
+ ```ruby
42
+ Transmission::RPC::Torrent + "http://example.com/url/to/torrent.torrent"
43
+ ```
44
+
45
+ To start all torrents:
46
+
47
+ ```ruby
48
+ Transmission::RPC::Torrent.start!
49
+ ```
50
+
51
+ To stop all torrents:
52
+
53
+ ```ruby
54
+ Transmission::RPC::Torrent.stop!
55
+ ```
56
+
57
+ To get all torrents:
58
+
59
+ ```ruby
60
+ Transmission.torrents
61
+ ```
62
+
63
+ To start a specific torrent:
64
+
65
+ ```ruby
66
+ Transmission.torrents.first.start!
67
+ ```
68
+
69
+ To stop a specific torrent:
70
+
71
+ ```ruby
72
+ Transmission.torrents.first.stop!
73
+ ```
74
+
75
+ To delete a torrent:
76
+
77
+ ```ruby
78
+ Transmission.torrents.first.delete!
79
+ ```
80
+
81
+ To delete a torrent and delete the contents from disk:
82
+
83
+ ```ruby
84
+ Transmission.torrents.first.delete!(true)
85
+ ```
86
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,58 @@
1
+ module Transmission
2
+ module RPC
3
+ # This communicates with Transmission's RPC server. Transmission's RPC is just an HTTP server with a JSON API
4
+ # It implements part of the [RPC 1.7.X spec](https://trac.transmissionbt.com/browser/branches/1.7x/doc/rpc-spec.txt)
5
+ module Client
6
+ API_VERSION = '1.7'
7
+
8
+ # Checks if we're connected to the Trasmission Daemon.
9
+ # If you're having problems, make sure you have the Transmission Daemon installed and then try running transmission-daemon -f in your shell
10
+ def self.connected?
11
+ begin
12
+ request("session-get")
13
+ true
14
+ rescue
15
+ false
16
+ end
17
+ end
18
+
19
+ # Sends out a request to Transmission's RPC server
20
+ # If you're curious about the formatting, [read this](https://trac.transmissionbt.com/browser/branches/1.7x/doc/rpc-spec.txt)
21
+ def self.request(method, arguments = {}, ids = [])
22
+ arguments = self.add_ids(arguments, ids) if ids.present?
23
+ begin
24
+ @response = RestClient.post(self.url, { :method => method, :arguments => arguments }.to_json, :x_transmission_session_id => self.session_id)
25
+ JSON.parse(@response.body)
26
+ rescue
27
+ puts "Couldn't connect to Transmission. Is Transmission running at http://#{Transmission.configuration.ip}:#{Transmission.configuration.port}?"
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ # Transmission's RPC requires a session ID. When you do a GET on /transmission/rpc then it gives you a 409 and a header that contains the session ID.
34
+ # This gets that session ID
35
+ def self.session_id
36
+ return @session_id unless @session_id.blank?
37
+ begin
38
+ RestClient.get(self.url)
39
+ rescue => e
40
+ return @session_id = e.response.headers[:x_transmission_session_id]
41
+ end
42
+ end
43
+
44
+ # Utility method for adding IDs to the arguments hash, even if it doesn't exist
45
+ def self.add_ids(arguments, ids)
46
+ arguments = {} if arguments.nil?
47
+ arguments[:ids] = ids
48
+ arguments
49
+ end
50
+
51
+ # URL to current Transmission Daemon
52
+ def self.url
53
+ "http://#{Transmission.configuration.ip}:#{Transmission.configuration.port}/#{Transmission.configuration.path}"
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,98 @@
1
+ module Transmission
2
+ module RPC
3
+ # A nice wrapper around Transmission's RPC
4
+ class Torrent
5
+ attr_accessor :id, :seeders, :leechers, :name, :download_directory, :eta, :percent_done, :files, :total_size, :date_added, :bytes_left, :comment, :description, :torrent_file, :hash, :status
6
+ include Transmission::RPC
7
+
8
+ def initialize(options = {})
9
+ self.id = options['id']
10
+ self.date_added = options['addedDate']
11
+ self.comment = options['comment']
12
+ self.eta = options['eta']
13
+ self.bytes_left = options['leftUntilDone']
14
+ self.name = options['name']
15
+ self.percent_done = options['percentDone']
16
+ self.torrent_file = options['torrentFile']
17
+ self.total_size = options['totalSize']
18
+ self.hash = options['hashString']
19
+ self.status = options['status']
20
+ end
21
+
22
+ # Starts downloading the current torrent
23
+ def start!
24
+ Client.request("torrent-start", nil, [self.id])
25
+ end
26
+
27
+ # Stops downloading the current torrent
28
+ def stop!
29
+ Client.request("torrent-stop", nil, [self.id])
30
+ end
31
+
32
+ # Deletes the current torrent, and, optionally, the data for that torrent
33
+ def delete!(delete_data = false)
34
+ Client.request("torrent-remove", { :delete_local_data => delete_data }, [self.id])
35
+ end
36
+
37
+ # Checks if torrent is currently downloading
38
+ def downloading?
39
+ self.status == 4
40
+ end
41
+
42
+ # Checks if torrent is paused
43
+ def paused?
44
+ self.status == 0
45
+ end
46
+
47
+ # Adds a torrent by URL or file path
48
+ def self.+(url)
49
+ self.add(:url => url)
50
+ end
51
+
52
+ # Gets all the torrents
53
+ def self.all
54
+ @unprocessed_torrents = Client.request("torrent-get", { :fields => self.fields })['arguments']['torrents']
55
+ @unprocessed_torrents.collect { |torrent| self.new(torrent) }
56
+ end
57
+
58
+ # Finds a torrent by ID
59
+ def self.find(id)
60
+ @unprocessed_response = Client.request("torrent-get", { :fields => self.fields }, [id])
61
+ @torrents = @unprocessed_response['arguments']['torrents']
62
+ if @torrents.count > 0
63
+ return self.new(@torrents.first)
64
+ else
65
+ return nil
66
+ end
67
+ end
68
+
69
+ # Adds a torrent by file path or URL (.torrent file's only right now)
70
+ def self.add(options = {})
71
+ @response = Client.request("torrent-add", :filename => options[:url])
72
+ if @response['result'] == 'success'
73
+ self.find(@response['arguments']['torrent-added']['id'])
74
+ else
75
+ nil
76
+ end
77
+ end
78
+
79
+ # Starts all torrents
80
+ def self.start!
81
+ Client.request "torrent-start"
82
+ end
83
+
84
+ # Stops all torrents
85
+ def self.stop!
86
+ Client.request "torrent-stop"
87
+ end
88
+
89
+ private
90
+
91
+ # The accessors for a torrent, the way that Transmission's RPC likes them.
92
+ def self.fields
93
+ @fields ||= %w(addedDate comment eta id leechers name seeders percentDone totalSize torrentFile status leftUntilDone hashString)
94
+ end
95
+
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,5 @@
1
+ module Transmission
2
+ module RPC
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ require 'rest-client'
2
+ require 'active_support/core_ext'
3
+ require 'transmission-rpc/torrent'
4
+ require 'transmission-rpc/client'
5
+
6
+ module Transmission
7
+ mattr_writer :configuration
8
+
9
+ def self.configuration
10
+ @configuration = Transmission::Configuration.new if @configuration.nil?
11
+ @configuration
12
+ end
13
+
14
+ def self.configure
15
+ self.configuration ||= Configuration.new
16
+ yield(configuration)
17
+ end
18
+
19
+ def self.connected?
20
+ Transmission::RPC::Client.connected?
21
+ end
22
+
23
+ # Convenience method for getting all the torrents
24
+ def self.torrents
25
+ Transmission::RPC::Torrent.all
26
+ end
27
+
28
+ class Configuration
29
+ attr_accessor :ip, :port, :path
30
+
31
+ def initialize
32
+ self.ip = "127.0.0.1"
33
+ self.port = 9091
34
+ self.path = "transmission/rpc"
35
+
36
+ self
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "transmission-rpc"
7
+ gem.version = '0.1.0'
8
+ gem.authors = ["Jarred Sumner"]
9
+ gem.email = ["jarred@jarredsumner.com"]
10
+ gem.description = %q{A simple Transmission RPC client for Ruby.}
11
+ gem.summary = %q{A simple Transmission RPC client for Ruby. It can add torrents, start/stop torrents, remove them to/from Transmission.}
12
+ gem.homepage = "https://github.com/jarred-sumner/transmission-rpc"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_dependency "rest-client"
20
+ gem.add_dependency "json"
21
+ gem.add_dependency "activesupport"
22
+ end
@@ -0,0 +1,8 @@
1
+ {
2
+ "folders":
3
+ [
4
+ {
5
+ "path": "/Users/jarred/Code/Content/transmission-rpc"
6
+ }
7
+ ]
8
+ }