Class: Aniview::Interface::DelugeC

Inherits:
Bridge
  • Object
show all
Defined in:
lib/aniview/interface/deluge/delugec.rb

Instance Method Summary collapse

Methods inherited from Bridge

#make_hash

Constructor Details

#initialize(pref) ⇒ DelugeC

Returns a new instance of DelugeC.



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
# File 'lib/aniview/interface/deluge/delugec.rb', line 9

def initialize pref
  @pref = pref
  host     = @pref.get("deluge_config")["host"]
      = @pref.get("deluge_config")["login"]
  password = @pref.get("deluge_config")["password"]
    
  if ( == "" and password == "") and (host == "127.0.0.1" or host == "localhost")
    la = localAuth
     = la[0]
    password = la[1]
    @pref.set(["deluge_config", "login"], )
    @pref.set(["deluge_config", "password"], password)
  end
    
  @client = Deluge::Rpc::Client.new(
    host:     host,
    port:     Integer(@pref.get("deluge_config")["port"]),
    login:    @pref.get("deluge_config")["login"],
    password: @pref.get("deluge_config")["password"]
  )
    
  @status = "not connected"
  @connected = false
  connect
    
end

Instance Method Details

#addTorrent(mag, loc, verbose: false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/aniview/interface/deluge/delugec.rb', line 128

def addTorrent mag, loc, verbose: false
  connect unless @connected
  return false unless @connected
  puts "[delugec] connected!" if verbose
  begin
    tid = @client.core.add_torrent_magnet(mag, {"download_location": loc})
  rescue Deluge::Rpc::Connection::InvokeTimeoutError
    #puts "[delugec] Error: timeout error" if verbose
    tid = nil
  rescue Errno::EPIPE
    #puts "[delugec] Error: Errno::EPIPE" if verbose
    tid = nil
  end
  return true if tid.class == String and tid.length > 1
  addTorrentCli mag, loc
  return false
end

#addTorrentCli(mag, loc) ⇒ Object



146
147
148
149
150
# File 'lib/aniview/interface/deluge/delugec.rb', line 146

def addTorrentCli mag, loc
  cli_tool = @pref.get("deluge_config")["cli_executable"]
  command = "add #{mag} -p #{Shellwords.escape loc}"
  result = %x(#{cli_tool} '#{command}')
end

#connectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aniview/interface/deluge/delugec.rb', line 40

def connect
  @connected = false
  @status = "error connecting"
  begin
    @client.connect
  rescue Deluge::Rpc::Connection::RPCError
    @client.close
  rescue RuntimeError
    @client.close
  rescue Errno::ECONNREFUSED
    @client.close
  else
    @status = "connected"
    @connected = true
  end
    
  @client.close unless @connected
end

#getStatusObject



36
37
38
# File 'lib/aniview/interface/deluge/delugec.rb', line 36

def getStatus
  @status
end

#itemsObject



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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/aniview/interface/deluge/delugec.rb', line 65

def items
  connect unless @connected
  return {} unless @connected
    
  tkeys = [
    "name",
    "progress",
    "eta",
    "paused",
    "state",
    "hash",
    #"active_time",
    #"is_finished",
    #"max_connections",
    #"max_download_speed",
    #"max_upload_slots",
    #"max_upload_speed",
    #"message",
    #"next_announce",
    #"num_peers",
    #"num_seeds",
    #"remove_at_ratio",
    #"save_path",
    #"seeding_time",
    #"seeds_peers_ratio",
    #"seed_rank",
    #"stop_at_ratio",
    #"stop_ratio",
    #"time_added",
    #"upload_payload_rate",
    #"comment",
    #"file_priorities",
    #"file_progress",
    #"files",
    #"is_seed",
    #"num_files",
    #"num_pieces",
    #"peers",
    #"piece_length",
    #"private",
    #"queue",
    #"ratio",
    #"total_size",
    #"tracker_host"
  ]
    
  makeHash @client.core.get_torrents_status({}, tkeys)
end

#localAuthObject



59
60
61
62
63
# File 'lib/aniview/interface/deluge/delugec.rb', line 59

def localAuth
  authfile = Dir.home + "/.config/deluge/auth"
  return ["", ""] if not File.exist? authfile
  File.open(Dir.home + "/.config/deluge/auth") { |f| f.read }.split(":")
end

#makeHash(arr) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/aniview/interface/deluge/delugec.rb', line 152

def makeHash(arr)
  ret = {}
  arr.each{ |t|
    tinfo = t[1]
    torrent = TorrentItem.new(
      name:     tinfo["name"],
      progress: tinfo["progress"],
      eta:      tinfo["eta"],
      paused:   tinfo["paused"], 
      state:    tinfo["state"],
      id:       tinfo["hash"]
    )
    ret.merge!(torrent => torrent)
  }
  ret
end

#removeTorrent(torrentItem, withData) ⇒ Object



123
124
125
126
# File 'lib/aniview/interface/deluge/delugec.rb', line 123

def removeTorrent torrentItem, withData
  id = torrentItem.attributes["h"]
  @client.core.remove_torrent(id, withData)
end

#toggleTorrent(torrentItem) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/aniview/interface/deluge/delugec.rb', line 114

def toggleTorrent torrentItem
  id = torrentItem.attributes["h"]
  if torrentItem.attributes["u"]
    @client.core.resume_torrent([id])
  else
    @client.core.pause_torrent([id])
  end
end