-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwake_transmission.rb
More file actions
executable file
·89 lines (68 loc) · 1.77 KB
/
wake_transmission.rb
File metadata and controls
executable file
·89 lines (68 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env ruby
#encoding: utf-8
#frozen_string_literal: true
require 'json'
require 'net/http'
require 'uri'
class Controller
def initialize(ip_address)
@http = Net::HTTP.new(ip_address, 9091)
@session = nil
end
def get_torrents
body = { method: 'torrent-get', arguments: { fields: ['id', 'name'] } }
response = post(body)
json = JSON.parse(response.body)
if json['result'] == 'success'
json['arguments']['torrents']
else
nil
end
end
def start_torrents
body = { method: 'torrent-start-now', arguments: { } }
response = post(body)
json = JSON.parse(response.body)
if json['result'] == 'success'
true
else
false
end
end
def stop_torrents
body = { method: 'torrent-stop', arguments: { } }
response = post(body)
json = JSON.parse(response.body)
if json['result'] == 'success'
true
else
false
end
end
private
def post(body, first_request = true)
request = Net::HTTP::Post.new('/transmission/rpc')
request.content_type = 'application/json'
request.body = body.to_json
request['X-Transmission-Session-Id'] = @session unless @session.nil?
response = @http.request request
if response.code == '409'
if first_request
@session = response['X-Transmission-Session-Id']
return post(body, false)
else
raise 'Did not receive a session id'
end
elsif response.code != '200'
raise "Fatal error attempting to communicate: #{response.code}: #{response.body}"
else
return response
end
end
end
ip_address = ARGV[0] || '127.0.0.1'
controller = Controller.new ip_address
response = controller.get_torrents
puts response.inspect
response = controller.start_torrents
puts response.inspect