-
Notifications
You must be signed in to change notification settings - Fork 2
/
google_news.rb
66 lines (59 loc) · 1.74 KB
/
google_news.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
66
require 'net/http'
require 'rubygems'
require 'hpricot'
require 'uri'
class GoogleNews < Citrus::Plugin
def description
<<-DESCRIPTION.gsub(/^\s+/, '')
このプラグインは Google の API を利用しておらず、真っ黒です。
絶対に利用しないでください。
<http://www.google.com/accounts/TOS>
DESCRIPTION
end
def initialize(*args)
super
@prefix = @config['prefix'] || '(の)?ニュース教えて'
@keyword
@limit = 3
end
def on_privmsg(prefix, channel, message)
return if !@config["channels"].nil? && !@config["channels"].include?(channel)
case message
when /^(ヘッドライン|最新|)#{@prefix}/
getHeadline().each do |m|
notice(channel, m)
end
when /^[\s]{0,}(.+)#{@prefix}/i
searchNews(Regexp.last_match[1]).each do |m|
notice(channel, m)
end
end
end
private
def searchNews(query)
# 検索
query = URI.encode(query)
search = "http://news.google.com/news?hl=ja&ned=us&ie=UTF-8&oe=UTF-8&output=rss&q=#{query}"
messages = getNews(search)
return messages
end
def getHeadline()
# 最新ニュース
headline = "http://news.google.com/news?hl=ja&ned=us&ie=UTF-8&oe=UTF-8&output=rss&topic=h"
messages = getNews(headline)
return messages
end
def getNews(target)
messages = Array.new()
rss = open(target){ |file| RSS::Parser.parse(file.read) }
rss.items[0...@limit].each do |item|
title = item.title
#url = open("http://tinyurl.com/api-create.php?url=#{item.link}").read
url = URI.short_uri(item.link,"tiny")
date = item.date.strftime("%m月%d日 %H時")
message = "#{date} #{title} #{url}"
messages << message
end
return messages
end
end