class NyaaAnime

Constants

ALREADY_DOWNLOADED_COLOR
CL_WIDTH
MAX_PAGES
NEW_COLOR
NYAA_DL_DIR
NYAA_ENTRIES_PER_PAGE
NYAA_SEARCH_BACKUP
NYAA_SEARCH_DEFAULT
NYAA_URI
THREAD_COUNT
VERSION

Public Class Methods

categorize_entries(entries) click to toggle source
# File lib/nyaa_anime.rb, line 81
def self.categorize_entries(entries)
  result = { downloaded: [], new: [], version: [], other: [] }
  dls = NyaaAnime.downloaded_entries
  downloaded_titles = dls.map { |e| e.title }
  downloaded_distro_metas = dls.map { |e| e.distro_meta }
  downloaded_distro_metas.uniq!
  downloaded_distro_episodes = dls.map { |e| e.distro_episode }
  downloaded_distro_episodes.uniq!
  entries.each do |e|
    if downloaded_titles.include? "#{e.title}.torrent"
      result[:downloaded].push e
    elsif downloaded_distro_episodes.include? e.distro_episode
      result[:version].push e
    elsif downloaded_distro_metas.include? e.distro_meta
      result[:new].push e
    else
      result[:other].push e
    end
  end
  result
end
colorized_entries(entries) click to toggle source
# File lib/nyaa_anime.rb, line 74
def self.colorized_entries(entries)
  c = self.categorize_entries entries
  c[:downloaded] = c[:downloaded].map { |e| Entry.new e.title.colorize(ALREADY_DOWNLOADED_COLOR), e.link }
  c[:new] = c[:new].map { |e| Entry.new e.title.colorize(NEW_COLOR), e.link }
  c
end
download(entries) click to toggle source
# File lib/nyaa_anime.rb, line 128
def self.download(entries)
  if entries.is_a? Array
    Parallel.map(entries) { |e| self.download_single e }.sort!
  else
    self.download_single entries
  end
end
download_single(entry) click to toggle source
# File lib/nyaa_anime.rb, line 117
def self.download_single(entry)
  torrent = open entry.link
  if redir = torrent.meta["refresh"]
    torrent = open redir[/url=(.+)\z/, 1]
  end
  filename = "#{entry.title.uncolorize}.torrent"
  #filename = torrent.meta["content-disposition"][/filename="(.+\.torrent)"/, 1]
  open("#{NYAA_DL_DIR}#{filename}", "w") { |f| f.write torrent.read }
  "#{NYAA_DL_DIR}\"#{filename}\""
end
downloaded_entries() click to toggle source
# File lib/nyaa_anime.rb, line 103
def self.downloaded_entries
  Dir["#{NYAA_DL_DIR}*"].select { |f| File.file? f }.map { |f| Entry.new File.basename(f) }
end
downloaded_entries_by_distro() click to toggle source
# File lib/nyaa_anime.rb, line 113
def self.downloaded_entries_by_distro
  NyaaAnime.entries_by_distro NyaaAnime.downloaded_entries
end
entries_by_distro(entries) click to toggle source
# File lib/nyaa_anime.rb, line 107
def self.entries_by_distro(entries)
  dls = {}
  entries.each { |e| (dls[e.distro_meta] ||= []) << e }
  dls
end
find_new_titles(quiet=false) click to toggle source
# File lib/nyaa_anime.rb, line 47
def self.find_new_titles(quiet=false)
  search_terms = NyaaAnime.downloaded_entries.map { |e| e.distro_meta }
  search_terms.uniq!
  count_s_size = "[/] ".size + search_terms.count.to_s.size * 2
  line_end_size = "...none found.".size
  max_entry_size = [search_terms.map { |t| t.size }.max + count_s_size +
                    line_end_size, CL_WIDTH].min - line_end_size
  index = 1
  lock = Mutex.new
  all_results = []
  Parallel.each(search_terms, in_threads: THREAD_COUNT) do |t|
    results = NyaaAnime.categorize_entries(NyaaAnime.search t)[:new]
    lock.synchronize do
      if !quiet
        msg = "/#{search_terms.count}] #{t[0...max_entry_size-count_s_size]}..." <<
              ("." * [max_entry_size - (count_s_size + t.size), 0].max) <<
              (results.any? ? "#{results.count} found!".colorize(NEW_COLOR) : "none found.")
        puts "[#{index.to_s.rjust(search_terms.count.to_s.size, "0")}#{msg}"
        index += 1
      end
    end
    all_results.concat results
  end
  all_results.uniq!
  all_results.sort!
end