class Seotracker
Constants
- RESULTS
- USER_AGENT
Public Class Methods
new()
click to toggle source
# File lib/seotracker.rb, line 10 def initialize @agent = Mechanize.new @agent.user_agent_alias = Seotracker::USER_AGENT @agent.agent.http.verify_mode = OpenSSL::SSL::VERIFY_NONE @agent.agent.http.retry_change_requests = true @debug = true if @debug @log = Logger.new('log.txt') #@agent.log = @log если надо логировать mechnize end end
Public Instance Methods
debug(message)
click to toggle source
# File lib/seotracker.rb, line 52 def debug(message) @log.debug(message) if @debug end
get_position(site, word, region = Seotracker::Yandex::MOSCOW, pages = 200)
click to toggle source
получаем позиции получаем массив ссылок от парсера увеличиваем счетчик позиций, пока не найдем нужную ссылку 4 ссылки храним в массиве “последних”, чтобы не считать случайно выдранные парсером повторения
# File lib/seotracker.rb, line 28 def get_position(site, word, region = Seotracker::Yandex::MOSCOW, pages = 200) pos, found, start, hrefs = 0, false, 0, [] while (start < pages) && !found links = parse(word, start, region) start += RESULTS break if links == 'error' links.each do |l| href = get_link(l) next if href == '' || hrefs.include?(href) # храним 4 последние полученные ссылки hrefs = hrefs.pop(3) hrefs << href pos += 1 if href.rindex(site) && same_level(href, site) found = true break end end end found ? pos : 0 end
Protected Instance Methods
get_link(link)
click to toggle source
# File lib/seotracker.rb, line 67 def get_link(link) href = link.attribute('href').value.downcase if link.attribute('href') href ||= link.value.downcase href.match(/[a-zA-Z0-9\-\.]+\.\w*/).to_s end
same_level(href, site)
click to toggle source
убеждаемся, что сайт, для которого определяем позицию (site) и найденная ссылка (href) на одном и том же уровне например, если искали позицию ya.ru а сначала нашлась позиция maps.ya.ru то ее не считаем
# File lib/seotracker.rb, line 62 def same_level(href, site) return href.count('.') - 1 == site.count('.') if href.index('www') href.count('.') == site.count('.') end