-
Notifications
You must be signed in to change notification settings - Fork 1
/
sun_sign_astrology.rb
65 lines (54 loc) · 1.42 KB
/
sun_sign_astrology.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
require 'nkf'
require 'rubygems'
require "open-uri"
require 'hpricot'
class SunSignAstrology < SkypeBot::Plugin
def initialize(*args)
super
@suffix = @config['suffix'] || 'の運勢教えて'
end
def on_privmsg(prefix, channel, message)
case message
when /^(.+)#{@suffix}$/
result = search_sign($1)
if result.empty?
sendMessage(channel, 'わかんないっ')
else
sendMessage(channel, result.to_s)
end
end
end
private
def search_sign(sign)
ranking = get_ranking 'http://fortune.yahoo.co.jp/12astro/ranking.html'
result = ''
ranking.each do |rank|
if rank[:name] == sign
result = "#{rank[:name]} #{rank[:rank]} #{rank[:desc]}!と出てるよ (#{rank[:link]})"
end
end
result
end
def get_ranking(uri)
begin
doc = Hpricot(open(uri))
names = doc/'td/p/img'
ranks = doc/'td/img'
descs = doc/'p.ft01/a'
ranking = []
names.each_with_index do |name, desc_counter|
rank_counter = desc_counter + 1
ranking << {
:name => name.attributes['alt'].to_u8,
:rank => ranks[rank_counter].attributes['alt'].to_u8,
:desc => descs[desc_counter].inner_text.to_u8,
:link => URI.short_uri(descs[desc_counter].attributes['href'])
}
end
ranking
rescue
'ひょえーこわれたー'
end
end
end