Class: Saber::Tracker::CHD

Inherits:
Base
  • Object
show all
Defined in:
lib/saber/tracker/chd.rb

Constant Summary collapse

BASE_URL =
"https://chdbits.org"
LOGIN_CHECK_PATH =
"/messages.php"
CACHE_FILE =
"#{ENV['HOME']}/.saber/chd.cache"

Constants inherited from Base

Base::DELEGATE_METHODS, Base::POPULATE_TYPES

Instance Attribute Summary collapse

Attributes inherited from Base

#agent, #name

Instance Method Summary collapse

Methods inherited from Base

can_populate?, inherited, #login, #populate, #upload

Constructor Details

#initializeCHD

Returns a new instance of CHD.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/saber/tracker/chd.rb', line 13

def initialize
  super

  if Pa.exists?(CACHE_FILE)
    @cache = Marshal.load(File.read(CACHE_FILE))
  else
    @cache = {}
  end

  at_exit { 
    Saber.ui.debug "Exit."
    File.write(CACHE_FILE, Marshal.dump(cache))
  }

  agent.pluggable_parser["application/x-bittorrent"] = Mechanize::DirectorySaver.save_to(Rc.chd.dir)
end

Instance Attribute Details

#cacheObject

cache is_download



11
12
13
# File 'lib/saber/tracker/chd.rb', line 11

def cache
  @cache
end

Instance Method Details

#add_torrentsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/saber/tracker/chd.rb', line 30

def add_torrents
  update_cache

  cache.each {|k,v|
    next if v 

    file = agent.get(build_download_link(k))
    Saber.ui.debug "Download #{Time.now.strftime("%H:%M:%S")} #{file.filename}"
    cache[k] = true
  }

  # limit to 60 counts
  self.cache = Hash[cache.to_a[-60..-1]] if cache.length > 60 
end

#update_cache(loaded = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/saber/tracker/chd.rb', line 45

def update_cache(loaded=false)
  agent.get("/torrents.php") {|p|
    links = p.search("//*[@id='torrenttbale']//img[@class='pro_free']/preceding-sibling::a")
    #links = p.search("//*[@id='torrenttbale']//img[@class='pro_free' or @class='pro_50pctdown']/preceding-sibling::a")
    links.each {|link|
      id = link["href"].match(/id=(\d+)/)[1].to_i

      cache[id] = loaded unless cache.has_key?(id)
    }
  }
end