Class: RHACK::Yandex

Inherits:
Service
  • Object
show all
Defined in:
lib/rhack/services/examples.rb

Constant Summary collapse

URI =
{
    :speller => "http://speller.yandex.net/services/spellservice.json/checkText",
    :search => "http://www.yandex.ru/yandsearch?lr=213&%s",
    :weather => "http://pogoda.yandex.ru/%d/details/"
}
IGNORE_UPPERCASE =
1
IGNORE_DIGITS =
2
IGNORE_URLS =
4
FIND_REPEAT_WORDS =
8
IGNORE_LATIN =
16
NO_SUGGEST =
32
FLAG_LATIN =
128

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service = :search, frame = nil) ⇒ Yandex

Returns a new instance of Yandex.



25
26
27
28
29
30
31
32
# File 'lib/rhack/services/examples.rb', line 25

def initialize(service=:search, frame=nil)
  ua = RHACK.useragents.rand
  ua << " YB/4.2.0" if !ua["YB"]
  super service, frame, nil, ua, :ck => {
      "yandexuid"=>"3644005621268702222",
      "t"=>"p"
  }, :eval => false
end

Class Method Details

.search(*args, &block) ⇒ Object



82
# File 'lib/rhack/services/examples.rb', line 82

def self.search(*args, &block) new.go *args, &block end

.weather(*args, &block) ⇒ Object



81
# File 'lib/rhack/services/examples.rb', line 81

def self.weather(*args, &block) new(:weather).go *args, &block end

Instance Method Details

#fix_content(doc, opts = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rhack/services/examples.rb', line 52

def fix_content(doc, opts={})
  nodes = doc.root.text_nodes
  speller(nodes*". ", opts) {|json|
    fix = {}
    json.each {|h| fix[h.word] = h.s[0] if h.s[0]}
    nodes.each {|n|
      fixed = false
      text = n.text
      fix.each {|k, v| fixed = true if text.gsub!(/\b#{k}\b/, v)}
      n.text(text) if fixed
    }
  }
  Curl.wait
end

#process(page) ⇒ Object



39
40
41
# File 'lib/rhack/services/examples.rb', line 39

def process page
  page.find('.p1/.cr').map {|n| [n.at('.cs').href, n.at('.cs').text.strip, (n.at('.kk') || n.at('.k7/div')).text.strip]} if page.html.b
end

#search(text, opts = {}, &block) ⇒ Object



34
35
36
37
# File 'lib/rhack/services/examples.rb', line 34

def search(text, opts={}, &block)
  uri = URI.search % urlencode(opts.merge(:text=>text))
  @f.run(uri, :proc_result => block) {|page| process page}
end

#speller(text, opts = 23) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rhack/services/examples.rb', line 43

def speller(text, opts=23)
  text = text.split_to_lines(10000)
  i = 0
  @f.run({"text" => text[i], "options" => opts}, URI.speller, :json => true) {|pg| 
    yield pg.hash
    text[i+=1] && @f.get({"text" => text[i], "options" => opts}, URI.speller, :json => true)
  }
end

#weather(city = 27612, day = nil, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rhack/services/examples.rb', line 67

def weather city=27612, day=nil, &block
  if city.is String
    city = CitiesCodes[city] if defined? CitiesCodes
    raise ServiceError, "can't get weather info for #{city.inspect}:#{city.class}" if !city.is(Fixnum)
  end
  @f.get(URI.weather%city, :proc_result => block) {|pg|
    ary = pg.find('//.b-forecast-details/tbody/tr{_["class"] =~ /t\d/}').map {|e|
      "#{e.at('.date') ? e.at('.date').text+":\n" : ''} - #{e.at('.t').text} - #{e.at('.data').text} - #{e.at('.wind/img').alt} #{e.at('.wind').text} м/с"
    }
    ary = ary[0..11].div(4) + ary[12..-1].div(2)
    day ? ary[day] : ary
  }#.res
end