class RutrackerApi::Client
Constants
- LOGIN_PAGE
- ORDER_OPTIONS
- SEARCH_PAGE
- SORT_OPTIONS
Attributes
agent[RW]
Public Class Methods
new(username, pass)
click to toggle source
# File lib/rutracker_api/client.rb, line 18 def initialize(username, pass) @agent = Mechanize.new @agent.user_agent_alias = 'Mac Safari' login(username, pass) end
Public Instance Methods
search(options = {})
click to toggle source
Advance search throw rutracker
@param options [Hash] the format type, `:category`, `:term`, `:sort`, `:order_by` @return [Hash] the options keys with search result
# File lib/rutracker_api/client.rb, line 28 def search(options = {}) query = prepare_query_string options @agent.get query parse_search end
Private Instance Methods
login(username, pass)
click to toggle source
# File lib/rutracker_api/client.rb, line 36 def login(username, pass) @agent.post(LOGIN_PAGE, login_username: username, login_password: pass, login: 'Вход') end
parse_search()
click to toggle source
# File lib/rutracker_api/client.rb, line 54 def parse_search @agent.page.search("//table[@class='forumline tablesorter']/tbody/tr").map do |row| return { error: 'Not found' } if row.css('.row1').text =~ /Не найдено/ description = row.at('td[4]').text.strip torrent_id = row.at('td[4]//a').attributes['data-topic_id'].text.to_i size = row.at('td[6]/a').text.chomp(' ↓') seeders = row.at('td[7]').text.to_i leechers = row.at('td[8]').text.to_i downloads = row.at('td[9]').text.to_i date = Time.strptime(row.at('td[10]/u').text, '%s') author = row.css('.u-name/a').text user_id = row.css('.u-name/a')[0]['href'].scan(/\d+/)[0].to_i { description: description, torrent_id: torrent_id, size: size, seeders: seeders, leechers: leechers, downloads: downloads, date: date, author: author, user_id: user_id } end end
prepare_query_string(options)
click to toggle source
# File lib/rutracker_api/client.rb, line 41 def prepare_query_string(options) prepared = { f: options[:category], nm: options[:term], s: SORT_OPTIONS[options[:sort]], o: ORDER_OPTIONS[options[:order_by]] } StringIO.new.tap do |q| q << "#{SEARCH_PAGE}?" q << "nm=#{prepared[:nm]}" if prepared[:nm] q << "&f=#{prepared[:f]}" if prepared[:f] q << "&o=#{prepared[:o]}" if prepared[:o] q << "&s=#{prepared[:s]}" if prepared[:s] end.string end