Class: QuartzTorrent::Metainfo
- Inherits:
-
Object
- Object
- QuartzTorrent::Metainfo
- Defined in:
- lib/quartz_torrent/metainfo.rb
Overview
Torrent metainfo structure. This is what’s usually found in .torrent files. This class generally follows the structure of the metadata format.
Defined Under Namespace
Instance Attribute Summary collapse
-
#announce ⇒ Object
Announce URL of the tracker.
-
#announceList ⇒ Object
Returns the value of attribute announceList.
-
#comment ⇒ Object
Comment.
-
#createdBy ⇒ Object
Created By.
-
#creationDate ⇒ Object
Creation date as a ruby Time object.
-
#encoding ⇒ Object
The string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile.
-
#info ⇒ Object
A Metainfo::Info object.
-
#infoHash ⇒ Object
A 20-byte SHA1 hash of the value of the info key from the metainfo.
Class Method Summary collapse
-
.createFromFile(path) ⇒ Object
Create a Metainfo object from the named file.
-
.createFromIO(io) ⇒ Object
Create a Metainfo object from the passed IO.
-
.createFromString(data) ⇒ Object
Create a Metainfo object from the passed bencoded string.
-
.valueOrException(v, msg) ⇒ Object
If ‘v’ is null, throw an exception.
Instance Method Summary collapse
-
#initialize ⇒ Metainfo
constructor
A new instance of Metainfo.
Constructor Details
#initialize ⇒ Metainfo
Returns a new instance of Metainfo.
151 152 153 154 155 156 157 158 159 |
# File 'lib/quartz_torrent/metainfo.rb', line 151 def initialize @info = nil @announce = nil @announceList = nil @creationDate = nil @comment = nil @createdBy = nil @encoding = nil end |
Instance Attribute Details
#announce ⇒ Object
Announce URL of the tracker
169 170 171 |
# File 'lib/quartz_torrent/metainfo.rb', line 169 def announce @announce end |
#announceList ⇒ Object
Returns the value of attribute announceList.
170 171 172 |
# File 'lib/quartz_torrent/metainfo.rb', line 170 def announceList @announceList end |
#comment ⇒ Object
Comment
175 176 177 |
# File 'lib/quartz_torrent/metainfo.rb', line 175 def comment @comment end |
#createdBy ⇒ Object
Created By
177 178 179 |
# File 'lib/quartz_torrent/metainfo.rb', line 177 def createdBy @createdBy end |
#creationDate ⇒ Object
Creation date as a ruby Time object
173 174 175 |
# File 'lib/quartz_torrent/metainfo.rb', line 173 def creationDate @creationDate end |
#encoding ⇒ Object
The string encoding format used to generate the pieces part of the info dictionary in the .torrent metafile
179 180 181 |
# File 'lib/quartz_torrent/metainfo.rb', line 179 def encoding @encoding end |
#info ⇒ Object
A Metainfo::Info object
162 163 164 |
# File 'lib/quartz_torrent/metainfo.rb', line 162 def info @info end |
#infoHash ⇒ Object
A 20-byte SHA1 hash of the value of the info key from the metainfo. This is neede when connecting to the tracker or to a peer.
166 167 168 |
# File 'lib/quartz_torrent/metainfo.rb', line 166 def infoHash @infoHash end |
Class Method Details
.createFromFile(path) ⇒ Object
Create a Metainfo object from the named file.
218 219 220 221 222 223 224 |
# File 'lib/quartz_torrent/metainfo.rb', line 218 def self.createFromFile(path) result = File.open(path,"rb") do |io| result = self.createFromIO(io) end result end |
.createFromIO(io) ⇒ Object
Create a Metainfo object from the passed IO.
213 214 215 |
# File 'lib/quartz_torrent/metainfo.rb', line 213 def self.createFromIO(io) self.createFromString(io.read) end |
.createFromString(data) ⇒ Object
Create a Metainfo object from the passed bencoded string.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/quartz_torrent/metainfo.rb', line 182 def self.createFromString(data) logger = LogManager.getLogger("metainfo") decoded = BEncode.load(data, {:ignore_trailing_junk => 1}) logger.debug "Decoded torrent metainfo: #{decoded.inspect}" result = Metainfo.new result.createdBy = decoded['created by'] result.comment = decoded['comment'] result.creationDate = decoded['creation date'] if result.creationDate if !result.creationDate.is_a?(Integer) if result.creationDate =~ /^\d+$/ result.creationDate = result.creationDate.to_i else logger.warn "Torrent metainfo contained invalid date: '#{result.creationDate.class}'" result.creationDate = nil end end result.creationDate = Time.at(result.creationDate) if result.creationDate end result.encoding = decoded['encoding'] result.announce = decoded['announce'].strip result.announceList = decoded['announce-list'] result.info = Info.createFromBdecode(decoded['info']) result.infoHash = Digest::SHA1.digest( decoded['info'].bencode ) result end |
.valueOrException(v, msg) ⇒ Object
If ‘v’ is null, throw an exception. Otherwise return ‘v’.
12 13 14 15 16 17 18 |
# File 'lib/quartz_torrent/metainfo.rb', line 12 def self.valueOrException(v, msg) if ! v LogManager.getLogger("metainfo").error msg raise "Invalid torrent metainfo" end v end |