Class: Seotracker::Yandex::Direct

Inherits:
Seotracker::Yandex show all
Defined in:
lib/seotracker/yandex/direct.rb

Defined Under Namespace

Classes: Exception

Constant Summary

Constants inherited from Seotracker::Yandex

MOSCOW, SEARCH_URL, WORDSTAT_URL

Constants inherited from Seotracker

RESULTS, USER_AGENT

Instance Method Summary collapse

Methods inherited from Seotracker::Yandex

#get_wordstat

Methods inherited from Seotracker

#debug, #get_position, #initialize

Constructor Details

This class inherits a constructor from Seotracker

Instance Method Details

#special(word, region = MOSCOW) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/seotracker/yandex/direct.rb', line 4

def special(word, region = MOSCOW)
  get_cookie
  url = SEARCH_URL + "text=#{word}&p=0&lr=#{region}"
  page = @agent.get(url, [], nil, {'cookie' => @cookie})
  begin
    texts = page.root.xpath('/html/body/div[3]/div/div/div/div/div[2]/div/h2/a')
    hrefs = page.root.xpath('/html/body/div[3]/div/div/div/div/div/div/div/div/span')

    # pre_result - массив вида ['text','text','text','url','url','url']
    pre_result = []
    texts.map { |t| pre_result << t.content.strip.squeeze("\n") }
    hrefs.map { |h| pre_result << h.content.scan(/\w+\.\w+/).first unless h.content.scan(/\w+\.\w+/)== [] }
    raise Seotracker::Yandex::Direct::Exception if pre_result.count != 6

    # приводим pre_result к виду [{ad: text, url: url}}]
    result = []
    (0...3).to_a.each { |i| result << { ad: pre_result[i], url: pre_result[i + 3] } }

    result
  rescue Exception => e
    debug "can't parse yandex direct: #{e.message}"
    'error'
  end
end