class QuartzTorrent::MagnetURI
Attributes
raw[R]
Public Class Methods
encodeFromMetainfo(metainfo)
click to toggle source
Create a magnet URI string given the metainfo from a torrent file.
# File lib/quartz_torrent/magnet.rb, line 84 def self.encodeFromMetainfo(metainfo) s = "magnet:?xt=urn:btih:" s << metainfo.infoHash.unpack("H*").first s << "&tr=" s << metainfo.announce end
magnetURI?(str)
click to toggle source
Returns true if the passed string is a magnet URI, false otherwise.
# File lib/quartz_torrent/magnet.rb, line 24 def self.magnetURI?(str) str =~ @@regex end
new(str)
click to toggle source
Create a new MagnetURI
object given a magnet URI string.
# File lib/quartz_torrent/magnet.rb, line 10 def initialize(str) @params = {} @raw = str if str =~ @@regex parseQuery $1 else raise "Not a magnet URI" end end
Public Instance Methods
[](key)
click to toggle source
Return the value of the specified key from the magnet URI.
# File lib/quartz_torrent/magnet.rb, line 29 def [](key) @params[key] end
btInfoHash()
click to toggle source
Return the first Bittorrent info hash found in the magnet URI. The returned info hash is in binary format.
# File lib/quartz_torrent/magnet.rb, line 35 def btInfoHash result = nil @params['xt'].each do |topic| if topic =~ /urn:btih:(.*)/ hash = $1 if hash.length == 40 # Hex-encoded info hash. Convert to binary. result = [hash].pack "H*" else # Base32 encoded result = Base32.decode hash end break end end result end
displayName()
click to toggle source
Return the first display name found in the magnet link. Returns nil if the magnet has no display name.
# File lib/quartz_torrent/magnet.rb, line 74 def displayName dn = @params['dn'] if dn dn.first else nil end end
tracker()
click to toggle source
Return the first tracker URL found in the magnet link. Returns nil if the magnet has no tracker info.
# File lib/quartz_torrent/magnet.rb, line 54 def tracker tr = @params['tr'] if tr tr.first else nil end end
trackers()
click to toggle source
Return an array of all tracker URLS in the magnet link.
# File lib/quartz_torrent/magnet.rb, line 64 def trackers tr = @params['tr'] if tr tr else [] end end
Private Instance Methods
parseQuery(query)
click to toggle source
# File lib/quartz_torrent/magnet.rb, line 92 def parseQuery(query) query.split('&').each do |part| if part =~ /(.*)=(.*)/ name = $1 val = $2 name = $1 if name =~ /(.*).\d+$/ @params.pushToList name, URI.unescape(val).tr('+',' ') end end end