crankshaft 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +0 -0
- data/README.md +21 -0
- data/lib/crankshaft/file.rb +17 -0
- data/lib/crankshaft/session.rb +75 -0
- data/lib/crankshaft/torrent.rb +74 -0
- data/lib/crankshaft/version.rb +6 -0
- data/lib/crankshaft.rb +9 -0
- metadata +87 -0
data/LICENSE
ADDED
File without changes
|
data/README.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
Crankshaft
|
3
|
+
----------
|
4
|
+
|
5
|
+
Ruby bindings for the Transmission BitTorrent client.
|
6
|
+
|
7
|
+
|
8
|
+
Example
|
9
|
+
-------
|
10
|
+
|
11
|
+
session = Crankshaft::Session.new('http://localhost:9091/transmission/rpc')
|
12
|
+
session.torrents do |torrent|
|
13
|
+
p torrent['name']
|
14
|
+
torrent.verify
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
License
|
19
|
+
-------
|
20
|
+
|
21
|
+
Copyright (c) 2010 by Tomas "wereHamster" Carnecky (tomas.carnecky@gmail.com)
|
@@ -0,0 +1,75 @@
|
|
1
|
+
|
2
|
+
require 'base64'
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'uri'
|
6
|
+
|
7
|
+
module Crankshaft
|
8
|
+
|
9
|
+
class Session
|
10
|
+
|
11
|
+
def initialize(url, timeout = 2)
|
12
|
+
@uri = URI.parse(url)
|
13
|
+
@timeout = timeout
|
14
|
+
end
|
15
|
+
|
16
|
+
def execute(method, arguments = {})
|
17
|
+
begin
|
18
|
+
request = { :method => method, :arguments => arguments }
|
19
|
+
response = connection.post(@uri.path, request.to_json, @headers)
|
20
|
+
if response.class == Net::HTTPConflict
|
21
|
+
@headers = { 'X-Transmission-Session-Id' => response['X-Transmission-Session-Id'] }
|
22
|
+
return execute(method, arguments)
|
23
|
+
end
|
24
|
+
|
25
|
+
return JSON.parse(response.body)
|
26
|
+
rescue Exception => e
|
27
|
+
@connection = nil
|
28
|
+
throw "Failed to execute request: #{e.message}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def add(file, options = {})
|
33
|
+
arguments = { :metainfo => Base64.encode64(file) }.merge(options)
|
34
|
+
response = execute('torrent-add', arguments)
|
35
|
+
if response['result'] == 'success'
|
36
|
+
attrs = response['arguments']['torrent-added']
|
37
|
+
if attrs
|
38
|
+
return Crankshaft::Torrent.new(self, attrs)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def torrents(*attrs)
|
44
|
+
arguments = { :fields => Crankshaft::Torrent::ATTRS }
|
45
|
+
response = execute('torrent-get', arguments)
|
46
|
+
|
47
|
+
return response['arguments']['torrents'].map do |attrs|
|
48
|
+
Crankshaft::Torrent.new(self, attrs)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def torrent(hash, *attrs)
|
53
|
+
arguments = { :ids => [ hash ], :fields => Crankshaft::Torrent::ATTRS + attrs }
|
54
|
+
response = execute('torrent-get', arguments)
|
55
|
+
if response['result'] == 'success'
|
56
|
+
attrs = response['arguments']['torrents'][0]
|
57
|
+
if attrs
|
58
|
+
return Crankshaft::Torrent.new(self, attrs)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def connection
|
66
|
+
@connection ||= begin
|
67
|
+
http = Net::HTTP.new(@uri.host, @uri.port) do |http|
|
68
|
+
http.read_timeout = @timeout
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
|
2
|
+
module Crankshaft
|
3
|
+
|
4
|
+
class Torrent
|
5
|
+
|
6
|
+
# Default attributes fetched with each torrent
|
7
|
+
ATTRS = [
|
8
|
+
:addedDate, :comment, :creator, :dateCreated, :files, :hashString, :id, :isPrivate,
|
9
|
+
:magnetLink, :name, :pieceCount, :pieceSize, :startDate, :trackers, :totalSize
|
10
|
+
]
|
11
|
+
|
12
|
+
def initialize(session, attrs = {})
|
13
|
+
@session = session
|
14
|
+
@attrs = attrs
|
15
|
+
end
|
16
|
+
|
17
|
+
# Actions
|
18
|
+
# -------
|
19
|
+
|
20
|
+
def start
|
21
|
+
arguments = { :ids => [ self['id'] ] }
|
22
|
+
@session.execute('torrent-start', arguments)
|
23
|
+
end
|
24
|
+
|
25
|
+
def stop
|
26
|
+
arguments = { :ids => [ self['id'] ] }
|
27
|
+
@session.execute('torrent-stop', arguments)
|
28
|
+
end
|
29
|
+
|
30
|
+
def verify
|
31
|
+
arguments = { :ids => [ self['id'] ] }
|
32
|
+
@session.execute('torrent-verify', arguments)
|
33
|
+
end
|
34
|
+
|
35
|
+
def reannounce
|
36
|
+
arguments = { :ids => [ self['id'] ] }
|
37
|
+
@session.execute('torrent-reannounce')
|
38
|
+
end
|
39
|
+
|
40
|
+
def remove(delete = false)
|
41
|
+
arguments = { :ids => [ self['id'] ], 'delete-local-data' => delete }
|
42
|
+
@session.execute('torrent-delete', arguments)
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
# Attributes
|
47
|
+
# ----------
|
48
|
+
|
49
|
+
def [](attribute)
|
50
|
+
return @attrs[attribute] if @attrs.include?(attribute)
|
51
|
+
|
52
|
+
arguments = { :ids => [ self['id'] ], :fields => [ attribute ] }
|
53
|
+
response = @session.execute('torrent-get', arguments)
|
54
|
+
if response['result'] == 'success'
|
55
|
+
attrs = response['arguments']['torrents'][0]
|
56
|
+
return attrs[attribute]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
# Files
|
62
|
+
# -----
|
63
|
+
|
64
|
+
def files
|
65
|
+
@files ||= begin
|
66
|
+
self['files'].map do |file|
|
67
|
+
Crankshaft::File.new(self, file)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/lib/crankshaft.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crankshaft
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tomas Carnecky
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-13 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: json
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
description:
|
36
|
+
email:
|
37
|
+
- tomas.carnecky@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- lib/crankshaft/file.rb
|
46
|
+
- lib/crankshaft/session.rb
|
47
|
+
- lib/crankshaft/torrent.rb
|
48
|
+
- lib/crankshaft/version.rb
|
49
|
+
- lib/crankshaft.rb
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/wereHamster/crankshaft
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
version: "0"
|
79
|
+
requirements: []
|
80
|
+
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.3.7
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Ruby bindings for the Transmission BitTorrent client
|
86
|
+
test_files: []
|
87
|
+
|