seppaleinen/torrentify

View on GitHub
lib/manager/sites/extratorrent_parser.rb

Summary

Maintainability
A
55 mins
Test Coverage
#!/usr/bin/ruby

require_relative '../../model/torrent_model'

# Class responsible for parsing
# the page response from kickass
class ExtratorrentParser
  # Takes mechanize-page
  def initialize(page)
    @page = page
  end

  # Parse values from html
  module Parser
    BASEURL = 'http://extratorrent.cc'

    # Extracts amount of seeders on torrent
    def self.seeders(div)
      links = div.search(".//td[@class='sy']")
      value = ''
      links.each do |link|
        value = link.content
      end
      value
    end

    # Extracts amount of leechers on torrent
    def self.leechers(div)
      links = div.search(".//td[@class='ly']")
      value = ''
      links.each do |link|
        value = link.content
      end
      value
    end

    # Extracts size on torrent
    def self.size(div)
      links = div.search('.//td')
      links[3].content
    end

    # Extracts torrent url
    def self.torrent_url(div)
      links = div.search('.//a')
      BASEURL + links[0].attributes['href']
    end

    # Extracts torrent name
    def self.torrent_name(div)
      links = div.search('.//a')
      # Get link title and strip Download och torrent from result
      title = links[0].attributes['title'].text
      title.gsub!('Download ', '').gsub!(' torrent', '')
    end
  end

  # Main method of class
  # Makes a list of all the torrent on a page
  # and loops through them, extracting information from Parser.
  # Returns a list of torrent-objects
  def main_divs
    divs = @page.search(".//tr[@class='tlr' or @class='tlz']")
    torrents = []
    divs.each do |div|
      torrent_file = Parser.torrent_url(div)
      name = Parser.torrent_name(div)
      size = Parser.size(div)
      seeders = Parser.seeders(div)
      leechers = Parser.leechers(div)
      torrent = Torrent.new(torrent_file, name, size, seeders, leechers)
      torrents.push(torrent)
    end
    torrents
  end
end