Class: Trans::Api::Torrent
- Inherits:
-
Object
- Object
- Trans::Api::Torrent
- 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
- .add_file(filename, options = {}) ⇒ Object
- .add_magnet(uri, options = {}) ⇒ Object
- .add_metainfo(metainfo, name, options = {}) ⇒ Object
- .all ⇒ Object
- .default_fields=(list = []) ⇒ Object
- .delete_all(torrents, options = {}) ⇒ Object
- .find(id) ⇒ Object
- .find_by_field_value(field, value) ⇒ Object
- .start_all ⇒ Object
- .stop_all ⇒ Object
Instance Method Summary collapse
- #delete!(options = {}) ⇒ Object
-
#fields ⇒ Object
get registered fields.
-
#files_objects ⇒ Object
files wrapped by an File object.
-
#initialize(options = {}) ⇒ Torrent
constructor
constructor.
- #last_error ⇒ Object
-
#metaclass ⇒ Object
placeholder for fields.
- #queue_bottom! ⇒ Object
- #queue_down! ⇒ Object
- #queue_top! ⇒ Object
- #queue_up! ⇒ Object
- #reannounce! ⇒ Object
-
#reset! ⇒ Object
reload current object.
-
#save! ⇒ Object
store changed field.
- #set_location!(file, move = false) ⇒ Object
- #start! ⇒ Object
- #start_now! ⇒ Object
- #status_name ⇒ Object
- #stop! ⇒ Object
- #verify! ⇒ Object
-
#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!.
Constructor Details
#initialize(options = {}) ⇒ Torrent
constructor
48 49 50 51 52 53 54 |
# File 'lib/trans-api/torrent.rb', line 48 def initialize(={}) @client = Client.new @fields = [:torrent] if .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, ={}) 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 [:filename] = filename torrent = client.connect.torrent_add 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, ={}) #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 [:filename] = uri torrent = client.connect.torrent_add 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 (, name, ={}) raise "name empty" if name.nil? || name == "" [: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 torrent = client.connect.torrent_get( @@default_fields, [torrent[:id]]).first Torrent.new torrent: torrent end |
.all ⇒ Object
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, ={}) 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 [:delete_local_data] = false unless .include? :delete_local_data # optional client.connect.torrent_remove 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_all ⇒ Object
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_all ⇒ Object
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!(={}) [:delete_local_data] = false unless .include? :delete_local_data # optional @client.connect.torrent_remove , [self.id] end |
#fields ⇒ Object
get registered fields
86 87 88 |
# File 'lib/trans-api/torrent.rb', line 86 def fields @fields end |
#files_objects ⇒ Object
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_error ⇒ Object
56 57 58 |
# File 'lib/trans-api/torrent.rb', line 56 def last_error @last_error end |
#metaclass ⇒ Object
placeholder for fields
61 62 63 |
# File 'lib/trans-api/torrent.rb', line 61 def 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_name ⇒ Object
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 |