Class: Trans::Api::Torrent

Inherits:
Object
  • Object
show all
Defined in:
lib/trans-api/torrent.rb

Overview

Torrent class

Constant Summary collapse

STATUS =

torrent status value

[ :stopped, :checkQueue, :checkFiles, :downloadQueue, :downloading, :seedQueue, :seeding, :isolated ]
ACCESSOR_FIELDS =

torrent get fields

[
:activityDate, :addedDate, :bandwidthPriority, :comment, :corruptEver, :creator, :dateCreated, :desiredAvailable,
:doneDate, :downloadDir, :downloadedEver, :downloadLimit, :downloadLimited, :error, :errorString, :eta, :files,
:fileStats, :hashString, :haveUnchecked, :haveValid, :honorsSessionLimits, :id, :isFinished, :isPrivate, :isStalled,
:leftUntilDone, :magnetLink, :manualAnnounceTime, :maxConnectedPeers, :metadataPercentComplete, :name, :peer_limit,
:peers, :peersConnected, :peersFrom, :peersGettingFromUs, :peersSendingToUs, :percentDone, :pieces, :pieceCount,
:pieceSize, :priorities, :queuePosition, :rateDownload, :rateUpload, :recheckProgress, :secondsDownloading,
:secondsSeeding, :seedIdleLimit, :seedIdleMode, :seedRatioLimit, :seedRatioMode, :sizeWhenDone, :startDate, :status,
:trackers, :trackerStats, :totalSize, :torrentFile, :uploadedEver, :uploadLimit, :uploadLimited, :uploadRatio,
:wanted, :webseeds, :webseedsSendingToUs ]
MUTATOR_FIELDS =

torrent set fields

[ :bandwidthPriority, :downloadLimit, :downloadLimited, :files_wanted, :files_unwanted, :honorsSessionLimits,
:ids, :location, :peer_limit, :priority_high, :priority_low, :priority_normal, :queuePosition, :seedIdleLimit,
:seedIdleMode, :seedRatioLimit, :seedRatioMode, :trackerAdd, :trackerRemove, :trackerReplace, :uploadLimit, :uploadLimited ]
CONTROL =
[:start!, :start_now!, :stop!, :verify!, :reannounce!, :set_location!, :delete!, :queue_top!, :queue_bottom!,
:queue_up!, :queue_down!]
ADD =

torrent add fields

[ :cookies, :download_dir, :filename, :metainfo, :paused, :peer_limit, :bandwidthPriority, :files_wanted,
:files_unwanted, :priority_high, :priority_low, :priority_normal ]
DELETE =
[ :ids, :delete_local_data ]
LOCATION =
[ :ids, :move, :location ]
@@default_fields =
[ :id, :name, :status ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Torrent

constructor



48
49
50
51
52
53
54
# File 'lib/trans-api/torrent.rb', line 48

def initialize(options={})
  @client = Client.new
  @fields = options[:torrent] if options.include? :torrent
  @old_fields = @fields.clone
  @last_error = {error: "", message: ""}
  self.attach_methods!
end

Class Method Details

.add_file(filename, options = {}) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/trans-api/torrent.rb', line 214

def add_file(filename, options={})
  raise "file not found: #{filename}" unless ::File.exists? filename
  # filter duplicates
  find = Trans::Api::Torrent.find_by_field_value(:name, ::File.basename(filename, ".*"))
  return find unless find.nil?
  client = Client.new
  options[:filename] = filename
  torrent = client.connect.torrent_add options
  torrent = client.connect.torrent_get( @@default_fields, [torrent[:id]]).first
  Torrent.new torrent: torrent
end

.add_magnet(uri, options = {}) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/trans-api/torrent.rb', line 226

def add_magnet(uri, options={})
  #TODO: urlencode & dn= in name not checked
  uri_match = uri.match(/dn=([\S]+)[&]/)
  raise "no name found" if uri_match.size != 2
  filename = uri_match[1]
  # filter duplicates
  find = Trans::Api::Torrent.find_by_field_value(:name, ::File.basename(filename, ".*"))
  return find unless find.nil?
  client = Client.new
  options[:filename] = uri
  torrent = client.connect.torrent_add options
  torrent = client.connect.torrent_get( @@default_fields, [torrent[:id]]).first
  Torrent.new torrent: torrent
end

.add_metainfo(metainfo, name, options = {}) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/trans-api/torrent.rb', line 241

def add_metainfo(metainfo, name, options={})
  raise "name empty" if name.nil? || name == ""
  options[:metainfo] = metainfo
  # filter duplicates
  find = Trans::Api::Torrent.find_by_field_value(:name, name)
  return find unless find.nil?
  client = Client.new
  torrent = client.connect.torrent_add options
  torrent = client.connect.torrent_get( @@default_fields, [torrent[:id]]).first
  Torrent.new torrent: torrent
end

.allObject



168
169
170
171
# File 'lib/trans-api/torrent.rb', line 168

def all
  torrents = Client.new.connect.torrent_get( @@default_fields )
  torrents.map{|t| Torrent.new torrent: t}
end

.default_fields=(list = []) ⇒ Object



253
254
255
256
# File 'lib/trans-api/torrent.rb', line 253

def default_fields=(list=[])
  @@default_fields << :id unless list.include? :id
  @@default_fields |= list
end

.delete_all(torrents, options = {}) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/trans-api/torrent.rb', line 205

def delete_all(torrents, options={})
  raise "no ids assigned" if torrents.empty? || !torrents.kind_of?(Array)
  raise "expected type: Torrent" if torrents.size > 0 && !torrents.first.kind_of?(Torrent)
  ids = torrents.map{|torrent| torrent.id}
  client = Client.new
  options[:delete_local_data] = false unless options.include? :delete_local_data # optional
  client.connect.torrent_remove options
end

.find(id) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/trans-api/torrent.rb', line 173

def find(id)
  torrents = Client.new.connect.torrent_get( @@default_fields , [id])
  remap = torrents.map{|t| Torrent.new torrent: t }
  return remap.first if torrents.size == 1
  return nil if torrents.empty?
  remap
end

.find_by_field_value(field, value) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
# File 'lib/trans-api/torrent.rb', line 181

def find_by_field_value(field, value)
  # set a reduced search field (with some default values)
  fields = [field, :id, :name, :status]
  torrents = Client.new.connect.torrent_get( fields )
  torrents.reject!{|t| t[field] != value}
  # TODO: perhaps add original default values??
  remap = torrents.map{|t| Torrent.new torrent: t }
  return remap.first if torrents.size == 1
  return nil if torrents.empty?
  remap
end

.start_allObject



193
194
195
196
197
# File 'lib/trans-api/torrent.rb', line 193

def start_all
  torrents = Client.new.connect.torrent_get [:id, :status]
  remap = torrents.map{ |t| Torrent.new torrent: t }
  Client.new.connect.torrent_start remap.map{|t| t.id}
end

.stop_allObject



199
200
201
202
203
# File 'lib/trans-api/torrent.rb', line 199

def stop_all
  torrents = Client.new.connect.torrent_get [:id, :status]
  remap = torrents.map{ |t| Torrent.new torrent: t }
  Client.new.connect.torrent_stop remap.map{|t| t.id}
end

Instance Method Details

#delete!(options = {}) ⇒ Object



139
140
141
142
# File 'lib/trans-api/torrent.rb', line 139

def delete!(options={})
  options[:delete_local_data] = false unless options.include? :delete_local_data # optional
  @client.connect.torrent_remove options, [self.id]
end

#fieldsObject

get registered fields



86
87
88
# File 'lib/trans-api/torrent.rb', line 86

def fields
  @fields
end

#files_objectsObject

files wrapped by an File object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/trans-api/torrent.rb', line 91

def files_objects
  ret = []
  torrent = @client.connect.torrent_get([:files, :fileStats], [self.id]).first
  @fields[:files] = torrent[:files]
  @fields[:fileStatus] = torrent[:fileStats]
  @fields[:files].each_with_index do |f,i| 
    ret << Trans::Api::File.new( torrent: self, fields: @fields,
      file: f.merge(id: i).merge(fileStat: torrent[:fileStats][i]))
  end
  ret
end

#last_errorObject



56
57
58
# File 'lib/trans-api/torrent.rb', line 56

def last_error
  @last_error
end

#metaclassObject

placeholder for fields



61
62
63
# File 'lib/trans-api/torrent.rb', line 61

def metaclass
  class << self; self; end
end

#queue_bottom!Object



148
149
150
# File 'lib/trans-api/torrent.rb', line 148

def queue_bottom!
  @client.connect.queue_move_bottom [self.id]
end

#queue_down!Object



156
157
158
# File 'lib/trans-api/torrent.rb', line 156

def queue_down!
  @client.connect.queue_move_down [self.id]
end

#queue_top!Object



144
145
146
# File 'lib/trans-api/torrent.rb', line 144

def queue_top!
  @client.connect.queue_move_top [self.id]
end

#queue_up!Object



152
153
154
# File 'lib/trans-api/torrent.rb', line 152

def queue_up!
  @client.connect.queue_move_up [self.id]
end

#reannounce!Object



131
132
133
# File 'lib/trans-api/torrent.rb', line 131

def reannounce!
  @client.connect.torrent_reannounce [self.id]
end

#reset!Object

reload current object



104
105
106
107
108
109
110
111
112
113
# File 'lib/trans-api/torrent.rb', line 104

def reset!
  begin
    @fields = @client.connect.torrent_get( @fields.map{|k,v| k}, [self.id]).first
    @old_fields = @fields.clone unless @fields.nil?
    @last_error = {error: "", message: ""}
  rescue Exception => e
    @last_error = {error: "reset_exception", message: "cannot reset torrent (probably removed)"}
  end
  nil
end

#save!Object

store changed field



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/trans-api/torrent.rb', line 70

def save!
  # reject unchanged fields
  changed = @fields.reject{|k,v| @old_fields[k] == v}
  # reject inmutable fields
  changed.reject!{|k,v| !MUTATOR_FIELDS.include?(k) }
  # reject empty arrays
  changed.reject!{|k,v| v.class == Array && v.empty? }
  if changed.size > 0
    # call api, and store changed fields
    @client.connect.torrent_set changed, [self.id]
    @old_fields = @fields.clone
  end
  nil
end

#set_location!(file, move = false) ⇒ Object



135
136
137
# File 'lib/trans-api/torrent.rb', line 135

def set_location!(file, move = false)
  @client.connect.torrent_set_location({location: file, move: move}, [self.id])
end

#start!Object



115
116
117
# File 'lib/trans-api/torrent.rb', line 115

def start!
  @client.connect.torrent_start [self.id]
end

#start_now!Object



119
120
121
# File 'lib/trans-api/torrent.rb', line 119

def start_now!
  @client.connect.torrent_start_now [self.id]
end

#status_nameObject



65
66
67
# File 'lib/trans-api/torrent.rb', line 65

def status_name
  STATUS[self.status]
end

#stop!Object



123
124
125
# File 'lib/trans-api/torrent.rb', line 123

def stop!
  @client.connect.torrent_stop [self.id]
end

#verify!Object



127
128
129
# File 'lib/trans-api/torrent.rb', line 127

def verify!
  @client.connect.torrent_verify [self.id]
end

#waitfor(lamb, place = :after) ⇒ Object

wait for a specific torrent lambda after call Ex: lamb = instance.waitfor_after(lambda{|torrent| torrent.status_name != :stopped}, :after).start!



162
163
164
# File 'lib/trans-api/torrent.rb', line 162

def waitfor(lamb, place = :after)
  TorrentDummy.new lamb, self, place
end