Class: Aniview::Interface::DelugeC

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

Instance Method Summary collapse

Constructor Details

#initialize(pref) ⇒ DelugeC

Returns a new instance of DelugeC.



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

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



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

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
  p tid
  return true if tid.class == String and tid.length > 1
  return false
end

#connectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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
  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

#getTorrentsObject



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

def getTorrents
  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"
  ]
    
  return @client.core.get_torrents_status({}, tkeys)
end

#localAuthObject



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

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

#makeHash(arr) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/aniview/interface/deluge/delugec.rb', line 144

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)
  }
  return ret
end

#removeTorrent(torrentItem, withData) ⇒ Object



121
122
123
124
# File 'lib/aniview/interface/deluge/delugec.rb', line 121

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

#toggleTorrent(torrentItem) ⇒ Object



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

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