Skip to content

Commit 1c20087

Browse files
committed
#130 Create Plugin Filter::GoogleNews
1 parent b1f4152 commit 1c20087

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

plugins/filter/google_news.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# Name:: Automatic::Plugin::Filter::GoogleNews
3+
# Author:: 774 <http://id774.net>
4+
# Created:: Oct 12, 2014
5+
# Updated:: Oct 12, 2014
6+
# Copyright:: Copyright (c) 2014 Automatic Ruby Developers.
7+
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
8+
9+
module Automatic::Plugin
10+
class FilterGoogleNews
11+
require 'uri'
12+
require 'nkf'
13+
14+
def initialize(config, pipeline=[])
15+
@config = config
16+
@pipeline = pipeline
17+
end
18+
19+
def run
20+
@return_feeds = []
21+
@pipeline.each {|feeds|
22+
new_feeds = []
23+
unless feeds.nil?
24+
feeds.items.each {|feed|
25+
new_feeds << rewrite_link(feed) unless feed.link.nil?
26+
}
27+
end
28+
@return_feeds << Automatic::FeedMaker.create_pipeline(new_feeds)
29+
}
30+
@return_feeds
31+
end
32+
33+
private
34+
35+
def rewrite_link(feed)
36+
if feed.link.index("http://news.google.com")
37+
matched = feed.link.match(/(&url=)/)
38+
unless matched.nil?
39+
new_link = matched.post_match
40+
feed.link = new_link unless new_link.nil?
41+
end
42+
end
43+
44+
feed
45+
end
46+
end
47+
end
48+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
# Name:: Automatic::Plugin::Filter::GoogleNews
3+
# Author:: 774 <http://id774.net>
4+
# Created:: Oct 12, 2014
5+
# Updated:: Oct 12, 2014
6+
# Copyright:: Copyright (c) 2014 Automatic Ruby Developers.
7+
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
8+
9+
require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')
10+
11+
require 'filter/google_news'
12+
13+
describe Automatic::Plugin::FilterGoogleNews do
14+
15+
context "It should be not rewrite link other urls" do
16+
17+
subject {
18+
Automatic::Plugin::FilterGoogleNews.new(
19+
{},
20+
AutomaticSpec.generate_pipeline {
21+
feed {
22+
item "http://test1.id774.net",
23+
"dummy title",
24+
"aaa bbb ccc http://test2.id774.net ddd eee",
25+
"Mon, 07 Mar 2011 15:54:11 +0900"
26+
}
27+
}
28+
)
29+
}
30+
31+
describe "#run" do
32+
its(:run) { should have(1).feeds }
33+
34+
specify {
35+
subject.run
36+
subject.instance_variable_get(:@pipeline)[0].items[0].link.
37+
should == "http://test1.id774.net"
38+
}
39+
end
40+
end
41+
42+
context "It should be rewrite link for google news urls" do
43+
44+
subject {
45+
Automatic::Plugin::FilterGoogleNews.new(
46+
{},
47+
AutomaticSpec.generate_pipeline {
48+
feed {
49+
item "http://news.google.com/news/url?sa=t&fd=R&ct2=us&usg=AFQjCNGhIFo1illQ6jFyVGPZtfkttFaJYQ&clid=c3a7d30bb8a4878e06b80cf16b898331&cid=52779138313507&ei=Pts3VLDAD4X7kgWO9oGIBg&url=http://www.yomiuri.co.jp/world/20141010-OYT1T50090.html",
50+
"dummy title",
51+
"aaa bbb ccc http://test2.id774.net ddd eee",
52+
"Mon, 07 Mar 2011 15:54:11 +0900"
53+
}
54+
}
55+
)
56+
}
57+
58+
describe "#run" do
59+
its(:run) { should have(1).feeds }
60+
61+
specify {
62+
subject.run
63+
subject.instance_variable_get(:@pipeline)[0].items[0].link.
64+
should == "http://www.yomiuri.co.jp/world/20141010-OYT1T50090.html"
65+
}
66+
end
67+
end
68+
69+
end

0 commit comments

Comments
 (0)