Class: Nyaa::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/nyaa/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, cat = nil, fil = nil) ⇒ Search



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/nyaa/search.rb', line 8

def initialize(query, cat = nil, fil = nil)
  self.query    = URI.escape(query)
  self.category = cat ? CATS[cat.to_sym][:id] : '0_0'
  self.filter   = fil ? FILS[fil.to_sym][:id] : '0'
  self.offset   = 0
  self.results  = []
  self.count    = 1.0/0.0

  self.runid    = Time.new.to_i
  self.cachedir = File.expand_path('~/.nyaa/cache')
  FileUtils.mkdir_p(cachedir) unless File.directory?(cachedir)
end

Instance Attribute Details

#cachedirObject

Returns the value of attribute cachedir.



6
7
8
# File 'lib/nyaa/search.rb', line 6

def cachedir
  @cachedir
end

#categoryObject

Returns the value of attribute category.



4
5
6
# File 'lib/nyaa/search.rb', line 4

def category
  @category
end

#countObject

Returns the value of attribute count.



5
6
7
# File 'lib/nyaa/search.rb', line 5

def count
  @count
end

#filterObject

Returns the value of attribute filter.



4
5
6
# File 'lib/nyaa/search.rb', line 4

def filter
  @filter
end

#offsetObject

Returns the value of attribute offset.



5
6
7
# File 'lib/nyaa/search.rb', line 5

def offset
  @offset
end

#queryObject

Returns the value of attribute query.



4
5
6
# File 'lib/nyaa/search.rb', line 4

def query
  @query
end

#resultsObject

Returns the value of attribute results.



5
6
7
# File 'lib/nyaa/search.rb', line 5

def results
  @results
end

#runidObject

Returns the value of attribute runid.



6
7
8
# File 'lib/nyaa/search.rb', line 6

def runid
  @runid
end

Instance Method Details

#cached(page) ⇒ Object

TODO: Deprecated function



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nyaa/search.rb', line 42

def cached(page)
  cachefile = "#{self.cachedir}/cache_#{self.runid}_p#{page}"
  p cachefile
  return nil unless File.exists?(cachefile)

  File.open(cachefile, 'rb') do |file|
    begin
      results = Marshal.load(file)
    rescue => e
      puts "ERROR: Failed to load #{cachefile}"
      puts "#{e.backtrace}: #{e.message} (#{e.class})"
    end
  end
  results
end

#get_resultsObject

Returns current batch (page) results



22
23
24
25
26
27
28
29
30
31
# File 'lib/nyaa/search.rb', line 22

def get_results
  if self.offset.zero?
    batch = []
  elsif self.offset == 1
    batch = self.results[0, 100]
  else # self.offset > 1
    batch = self.results[(self.offset - 1) * 100, 100]
  end
  batch
end

#moreObject



33
34
35
36
37
38
39
# File 'lib/nyaa/search.rb', line 33

def more
  self.offset += 1
  if self.results.length < self.count
    extract(self.offset)
  end
    self
end

#purgeObject



58
59
60
# File 'lib/nyaa/search.rb', line 58

def purge
  FileUtils.rm_rf Dir.glob("#{self.cachedir}/*")
end