Class: Nanny::Torrent
- Inherits:
-
Object
- Object
- Nanny::Torrent
- Includes:
- AttrInitializer
- Defined in:
- lib/nanny/torrent.rb
Defined Under Namespace
Classes: URLNotFound
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#peers ⇒ Object
readonly
Returns the value of attribute peers.
-
#seeds ⇒ Object
readonly
Returns the value of attribute seeds.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #human_size ⇒ Object
- #page_doc ⇒ Object
- #page_html ⇒ Object
- #torcache_url(progress = Progress.new) ⇒ Object
- #torrent_url(progress = Progress.new) ⇒ Object
- #trackers ⇒ Object
- #trackers_torcache_url(progress = Progress.new) ⇒ Object
- #trackers_torrent_url(progress = Progress.new) ⇒ Object
Methods included from AttrInitializer
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
12 13 14 |
# File 'lib/nanny/torrent.rb', line 12 def hash @hash end |
#peers ⇒ Object (readonly)
Returns the value of attribute peers.
11 12 13 |
# File 'lib/nanny/torrent.rb', line 11 def peers @peers end |
#seeds ⇒ Object (readonly)
Returns the value of attribute seeds.
10 11 12 |
# File 'lib/nanny/torrent.rb', line 10 def seeds @seeds end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
9 10 11 |
# File 'lib/nanny/torrent.rb', line 9 def size @size end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
7 8 9 |
# File 'lib/nanny/torrent.rb', line 7 def title @title end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
8 9 10 |
# File 'lib/nanny/torrent.rb', line 8 def url @url end |
Instance Method Details
#human_size ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/nanny/torrent.rb', line 16 def human_size return "0B" if size.zero? units = %w{B KB MB GB TB} e = (Math.log(size)/Math.log(1024)).floor s = "%.1f" % (size.to_f / 1024**e) s.sub(/\.?0*$/, units[e]) end |
#page_doc ⇒ Object
77 78 79 |
# File 'lib/nanny/torrent.rb', line 77 def page_doc Nokogiri::HTML(page_html) end |
#page_html ⇒ Object
81 82 83 |
# File 'lib/nanny/torrent.rb', line 81 def page_html @page_html ||= HTTPClient.get(url) end |
#torcache_url(progress = Progress.new) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/nanny/torrent.rb', line 32 def torcache_url(progress = Progress.new) progress.step { Torcache.url_for(hash) } rescue Torcache::HashNotFound trackers_torcache_url(progress.child) ensure progress.complete! end |
#torrent_url(progress = Progress.new) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/nanny/torrent.rb', line 24 def torrent_url(progress = Progress.new) torcache_url(progress.child) rescue URLNotFound trackers_torrent_url(progress.child) ensure progress.complete! end |
#trackers ⇒ Object
71 72 73 74 75 |
# File 'lib/nanny/torrent.rb', line 71 def trackers page_doc.css(".download dl dt a[href^=http]").map do |link| Tracker.new(link['href']) end end |
#trackers_torcache_url(progress = Progress.new) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/nanny/torrent.rb', line 40 def trackers_torcache_url(progress = Progress.new) progress.step do trackers.each do |tracker| begin return progress.step do Torcache.url_for tracker.magnet_uri(progress.child).hash end rescue Tracker::MagnetNotFound rescue Torcache::HashNotFound end end end raise URLNotFound ensure progress.complete! end |
#trackers_torrent_url(progress = Progress.new) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/nanny/torrent.rb', line 57 def trackers_torrent_url(progress = Progress.new) progress.step do trackers.each do |tracker| begin return tracker.torrent_url(progress.child) rescue Tracker::TorrentNotFound end end end raise URLNotFound ensure progress.complete! end |