Class: Nanny::Torrent

Inherits:
Object
  • Object
show all
Includes:
AttrInitializer
Defined in:
lib/nanny/torrent.rb

Defined Under Namespace

Classes: URLNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttrInitializer

#initialize

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



12
13
14
# File 'lib/nanny/torrent.rb', line 12

def hash
  @hash
end

#peersObject (readonly)

Returns the value of attribute peers.



11
12
13
# File 'lib/nanny/torrent.rb', line 11

def peers
  @peers
end

#seedsObject (readonly)

Returns the value of attribute seeds.



10
11
12
# File 'lib/nanny/torrent.rb', line 10

def seeds
  @seeds
end

#sizeObject (readonly)

Returns the value of attribute size.



9
10
11
# File 'lib/nanny/torrent.rb', line 9

def size
  @size
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/nanny/torrent.rb', line 7

def title
  @title
end

#urlObject (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_sizeObject



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_docObject



77
78
79
# File 'lib/nanny/torrent.rb', line 77

def page_doc
  Nokogiri::HTML(page_html)
end

#page_htmlObject



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

#trackersObject



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