streambot 0.2.5 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +19 -10
- data/Rakefile +2 -0
- data/lib/streambot.rb +12 -33
- data/lib/streambot/http.rb +28 -0
- data/lib/streambot/retweet.rb +24 -0
- data/lib/streambot/tracker.rb +35 -0
- data/test/streambot/base_test.rb +34 -0
- data/test/streambot/test_http.rb +27 -0
- data/test/streambot/test_retweet.rb +30 -0
- metadata +22 -9
- data/lib/retweet.rb +0 -29
- data/test/base_test.rb +0 -35
- data/test/test_retweet.rb +0 -32
- data/test/test_streambot.rb +0 -10
data/README.rdoc
CHANGED
@@ -1,28 +1,37 @@
|
|
1
1
|
= streambot
|
2
2
|
|
3
|
-
streambot is using tweetstream
|
3
|
+
streambot is using tweetstream[http://github.com/intridea/tweetstream] for simple access to twitter streaming api
|
4
4
|
|
5
5
|
== Installation
|
6
6
|
|
7
|
-
the streambot gem is available on rubygems.org
|
8
|
-
|
9
|
-
gem install streambot
|
7
|
+
the streambot gem is available on rubygems.org[http://rubygems.org/gems/streambot]
|
8
|
+
to get streambot installed, you simply need to run
|
10
9
|
|
10
|
+
gem install streambot
|
11
|
+
|
11
12
|
== Usage
|
12
13
|
|
13
14
|
require 'streambot'
|
14
15
|
|
15
16
|
@auth = {:username=>'username',:password=>'password'}
|
16
17
|
@blacklist = ['mac_rt','apple_rt']
|
17
|
-
bot = StreamBot.new(@auth, @blacklist, 'apple','ipad','iphone os 4','steve jobs')
|
18
|
+
bot = StreamBot::Tracker.new(@auth, @blacklist, 'apple','ipad','iphone os 4','steve jobs')
|
18
19
|
bot.start
|
19
20
|
|
20
|
-
|
21
|
+
|
22
|
+
== Contribution
|
23
|
+
=== Testing
|
24
|
+
|
25
|
+
All Tests should inherit from StreamBot::BaseTest
|
21
26
|
|
22
|
-
|
27
|
+
=== Feature Requests / Issues
|
28
|
+
|
29
|
+
If you ran into some errors? Then don't be shy and file an issue in the issue-tracker[http://github.com/gr4y/streambot/issues]
|
30
|
+
Maybe you have a feature request then file an issue too.
|
23
31
|
|
24
|
-
|
25
|
-
|
32
|
+
=== Note on Patches/Pull Requests
|
33
|
+
|
34
|
+
You want to add a feature or you want to patch streambot?
|
26
35
|
* Fork the project.
|
27
36
|
* Make your feature addition or bug fix.
|
28
37
|
* Add tests for it. This is important so I don't break it in a
|
@@ -33,4 +42,4 @@ All todos are listed in issue-tracker and labeled with todo (http://github.com/g
|
|
33
42
|
|
34
43
|
== Copyright
|
35
44
|
|
36
|
-
Copyright (c) 2010 Sascha Wessel. See LICENSE for details.
|
45
|
+
Copyright (c) 2010 Sascha Wessel. See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -10,6 +10,7 @@ begin
|
|
10
10
|
gem.email = "swessel@gr4yweb.de"
|
11
11
|
gem.homepage = "http://github.com/gr4y/streambot"
|
12
12
|
gem.authors = ["Sascha Wessel"]
|
13
|
+
gem.post_install_message = File.exist?('USAGE.rdoc') ? File.read('USAGE.rdoc') : ""
|
13
14
|
gem.require_path = 'lib'
|
14
15
|
gem.files = %w(Rakefile) + Dir.glob("{lib}/**/*")
|
15
16
|
|
@@ -17,6 +18,7 @@ begin
|
|
17
18
|
gem.add_dependency "tweetstream",">= 1.0.4"
|
18
19
|
gem.add_development_dependency "webmock",">= 1.0.0"
|
19
20
|
gem.add_development_dependency "rcov",">= 0.9.8"
|
21
|
+
|
20
22
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
21
23
|
end
|
22
24
|
rescue LoadError
|
data/lib/streambot.rb
CHANGED
@@ -1,36 +1,15 @@
|
|
1
1
|
require 'rubygems'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'net/http'
|
2
4
|
require 'tweetstream'
|
3
|
-
require '
|
5
|
+
require 'logger'
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
@keywords=keywords.join(',')
|
15
|
-
# set blacklist array if not nil
|
16
|
-
@blacklist=blacklist
|
17
|
-
end
|
18
|
-
|
19
|
-
# start the bot
|
20
|
-
def start
|
21
|
-
# starting to track the keywords via tweetstream
|
22
|
-
@stream.track(@keywords) do |status|
|
23
|
-
# if status.user is NOT in blacklist then retweet it
|
24
|
-
if !@blacklist.include?(status.user.screen_name)
|
25
|
-
#puts "#{status.text} ##{status.id}"
|
26
|
-
@retweet.retweet(status.id)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
# stop the bot
|
32
|
-
def stop
|
33
|
-
@stream.stop
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
7
|
+
require 'streambot/tracker'
|
8
|
+
require 'streambot/retweet'
|
9
|
+
require 'streambot/http'
|
10
|
+
|
11
|
+
module StreamBot
|
12
|
+
LOG = Logger.new(STDOUT)
|
13
|
+
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
14
|
+
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module StreamBot
|
2
|
+
# http communication wrapper
|
3
|
+
class HTTP
|
4
|
+
# initialize request with basic authentication and send
|
5
|
+
def post_with_auth(auth, url)
|
6
|
+
uri = URI.parse(url)
|
7
|
+
request = init_request(uri)
|
8
|
+
request.basic_auth auth[:username], auth[:password]
|
9
|
+
post_request(request, uri)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
# send request
|
14
|
+
def post_request(request, url)
|
15
|
+
Net::HTTP.new(url.host, url.port).start do |http|
|
16
|
+
LOG.debug("#{http.inspect}")
|
17
|
+
http.request(request)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# initialize an Net::HTTP::Post Request
|
22
|
+
def init_request(url)
|
23
|
+
LOG.debug("#{self.class}#init_request(#{url})")
|
24
|
+
puts url.inspect
|
25
|
+
Net::HTTP::Post.new(url.path)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module StreamBot
|
2
|
+
# wrapper class for dealing with twitters native retweet api
|
3
|
+
class Retweet
|
4
|
+
# api base url
|
5
|
+
$BASE_URL = "http://api.twitter.com/1/"
|
6
|
+
|
7
|
+
# intitialize method aka constructor
|
8
|
+
def initialize(auth)
|
9
|
+
@auth = auth
|
10
|
+
@http = StreamBot::HTTP.new
|
11
|
+
end
|
12
|
+
|
13
|
+
# retweets the status with given id
|
14
|
+
def retweet(id)
|
15
|
+
LOG.debug("#{self.class}#retweet")
|
16
|
+
res = @http.post_with_auth(@auth, "#{$BASE_URL}statuses/retweet/#{id}.json")
|
17
|
+
case res
|
18
|
+
when Net::HTTPSuccess then return res
|
19
|
+
else
|
20
|
+
res.error!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module StreamBot
|
2
|
+
# The Tracker class that provides a start and a stop method
|
3
|
+
class Tracker
|
4
|
+
# the initialization method aka the constructor
|
5
|
+
def initialize(auth, blacklist, *keywords)
|
6
|
+
LOG.debug("#{self.class}#initialize")
|
7
|
+
@stream = TweetStream::Client.new(auth[:username],auth[:password])
|
8
|
+
# initializing retweet
|
9
|
+
@retweet = StreamBot::Retweet.new(auth) #
|
10
|
+
# get string with keywords comma separated keywords
|
11
|
+
@keywords=keywords.join(',')
|
12
|
+
# set blacklist array if not nil
|
13
|
+
@blacklist=blacklist
|
14
|
+
end
|
15
|
+
|
16
|
+
# start the bot
|
17
|
+
def start
|
18
|
+
LOG.debug("#{self.class}#start")
|
19
|
+
# starting to track the keywords via tweetstream
|
20
|
+
@stream.track(@keywords) do |status|
|
21
|
+
username = status.user.screen_name
|
22
|
+
# if status.user is NOT in blacklist then retweet it
|
23
|
+
if !@blacklist.include?(username)
|
24
|
+
LOG.debug("#{self.class}#start - retweet ##{status.id} from @#{username}")
|
25
|
+
@retweet.retweet(status.id)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# stop the bot
|
31
|
+
def stop
|
32
|
+
@stream.stop
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'webmock'
|
4
|
+
require 'yaml'
|
5
|
+
include WebMock
|
6
|
+
|
7
|
+
require 'streambot'
|
8
|
+
|
9
|
+
module StreamBot
|
10
|
+
# The base of all tests in this projet
|
11
|
+
#
|
12
|
+
# class TestName < StreamBot::BaseTest
|
13
|
+
#
|
14
|
+
# def setup
|
15
|
+
# super
|
16
|
+
# # implementation goes here
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
class BaseTest < Test::Unit::TestCase
|
20
|
+
|
21
|
+
# reading config.yml and assign all keys to instance
|
22
|
+
def read_config
|
23
|
+
yml_config = YAML.load_file(File.dirname(__FILE__) + "/config.yml")
|
24
|
+
yml_config.each do |key, value|
|
25
|
+
instance_variable_set("@#{key}", value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# :nodoc:
|
30
|
+
def setup
|
31
|
+
self.read_config
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'streambot/base_test'
|
2
|
+
|
3
|
+
module StreamBot
|
4
|
+
# test for http wrapper class
|
5
|
+
class TestHttp < StreamBot::BaseTest
|
6
|
+
# setup
|
7
|
+
def setup
|
8
|
+
@http = StreamBot::HTTP.new
|
9
|
+
# override auth
|
10
|
+
@auth = {:username=>'test', :password=>'test'}
|
11
|
+
stub_request(:post, "http://test:test@testing.com/path/to/data").to_return(:body => @auth)
|
12
|
+
end
|
13
|
+
|
14
|
+
# nil all variables
|
15
|
+
def teardown
|
16
|
+
@http = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# test a post with basic authentication
|
20
|
+
def test_post
|
21
|
+
WebMock.disable_net_connect!
|
22
|
+
res = @http.post_with_auth(@auth, 'http://testing.com/path/to/data')
|
23
|
+
assert_not_nil(res)
|
24
|
+
assert_equal(@auth, res.body)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'streambot/base_test'
|
2
|
+
|
3
|
+
module StreamBot
|
4
|
+
# the test for testing retweet functionality
|
5
|
+
class TestRetweet < StreamBot::BaseTest
|
6
|
+
# setup the test
|
7
|
+
def setup
|
8
|
+
super
|
9
|
+
@retweet = StreamBot::Retweet.new(@auth)
|
10
|
+
stub_request(:post, "http://#{@auth[:username]}:#{@auth[:password]}@api.twitter.com/1/statuses/retweet/#{@id}.json").to_return(:body => 'content')
|
11
|
+
end
|
12
|
+
|
13
|
+
# teardown the test
|
14
|
+
# nil all instance variables
|
15
|
+
def teardown
|
16
|
+
@id, @retweet = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# retweet
|
20
|
+
def test_retweet
|
21
|
+
# no permission to access web!
|
22
|
+
WebMock.disable_net_connect!
|
23
|
+
# retweet the given id
|
24
|
+
response = @retweet.retweet(@id)
|
25
|
+
# assertion
|
26
|
+
assert_not_nil(response)
|
27
|
+
assert_equal('content', response.body)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Sascha Wessel
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-13 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -70,15 +70,28 @@ extra_rdoc_files:
|
|
70
70
|
- README.rdoc
|
71
71
|
files:
|
72
72
|
- Rakefile
|
73
|
-
- lib/retweet.rb
|
74
73
|
- lib/streambot.rb
|
74
|
+
- lib/streambot/http.rb
|
75
|
+
- lib/streambot/retweet.rb
|
76
|
+
- lib/streambot/tracker.rb
|
75
77
|
- LICENSE
|
76
78
|
- README.rdoc
|
77
79
|
has_rdoc: true
|
78
80
|
homepage: http://github.com/gr4y/streambot
|
79
81
|
licenses: []
|
80
82
|
|
81
|
-
post_install_message:
|
83
|
+
post_install_message: "\n\
|
84
|
+
=== You are upgrading from an earlier version?\n\n\
|
85
|
+
Maybe you need to change your code a little bit\n\
|
86
|
+
\t\n\
|
87
|
+
=== A simple example how to use streambot\n\n\
|
88
|
+
\trequire 'streambot'\n\n\
|
89
|
+
\t@auth = {:username=>'username',:password=>'password'}\n\
|
90
|
+
\t@blacklist = ['mac_rt','apple_rt']\n\
|
91
|
+
\tbot = StreamBot::Tracker.new(@auth, @blacklist, 'apple','ipad','iphone os 4','steve jobs')\n\
|
92
|
+
\tbot.start\n\n\
|
93
|
+
=== streambot is an open source project\n\n\
|
94
|
+
the code is available on github[http://github.com/gr4y/streambot] "
|
82
95
|
rdoc_options:
|
83
96
|
- --charset=UTF-8
|
84
97
|
require_paths:
|
@@ -105,6 +118,6 @@ signing_key:
|
|
105
118
|
specification_version: 3
|
106
119
|
summary: retweeting tweets with specified keywords on twitter
|
107
120
|
test_files:
|
108
|
-
- test/base_test.rb
|
109
|
-
- test/
|
110
|
-
- test/
|
121
|
+
- test/streambot/base_test.rb
|
122
|
+
- test/streambot/test_retweet.rb
|
123
|
+
- test/streambot/test_http.rb
|
data/lib/retweet.rb
DELETED
@@ -1,29 +0,0 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require 'open-uri'
|
3
|
-
|
4
|
-
# wrapper class for dealing with twitters native retweet api
|
5
|
-
# it simply connects to twitter api via http basic auth with given credentials
|
6
|
-
class Retweet
|
7
|
-
|
8
|
-
# intitialize method aka constructor
|
9
|
-
def initialize(auth)
|
10
|
-
return if auth.nil?
|
11
|
-
@auth = auth
|
12
|
-
end
|
13
|
-
|
14
|
-
# retweets the status with given id
|
15
|
-
def retweet(id)
|
16
|
-
url = URI.parse("http://api.twitter.com/1/statuses/retweet/#{id}.json")
|
17
|
-
req = Net::HTTP::Post.new(url.path)
|
18
|
-
# set credentials
|
19
|
-
req.basic_auth @auth[:username],@auth[:password]
|
20
|
-
# connect
|
21
|
-
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
|
22
|
-
case res
|
23
|
-
when Net::HTTPSuccess then return res
|
24
|
-
else
|
25
|
-
res.error!
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
data/test/base_test.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'yaml'
|
3
|
-
require 'test/unit'
|
4
|
-
require 'webmock'
|
5
|
-
|
6
|
-
include WebMock
|
7
|
-
|
8
|
-
# The base of all tests in this projet
|
9
|
-
# New Testcases need to look like this:
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# class TestName < BaseTest
|
13
|
-
#
|
14
|
-
# def setup
|
15
|
-
# super
|
16
|
-
# # implementation goes here
|
17
|
-
# end
|
18
|
-
#
|
19
|
-
# end
|
20
|
-
class BaseTest < Test::Unit::TestCase
|
21
|
-
|
22
|
-
# reading config.yml and assign all keys to instance
|
23
|
-
def read_config
|
24
|
-
yml_config = YAML.load_file(File.dirname(__FILE__) + "/config.yml")
|
25
|
-
yml_config.each do |key, value|
|
26
|
-
instance_variable_set("@#{key}", value)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# :nodoc:
|
31
|
-
def setup
|
32
|
-
self.read_config
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
data/test/test_retweet.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'retweet'
|
2
|
-
require 'base_test'
|
3
|
-
|
4
|
-
# the test for testing retweet functionality
|
5
|
-
class TestRetweet < BaseTest
|
6
|
-
|
7
|
-
# setup the test
|
8
|
-
def setup
|
9
|
-
super
|
10
|
-
@id = 12355936428
|
11
|
-
@retweet = Retweet.new(@auth)
|
12
|
-
stub_request(:post, "http://#{@auth[:username]}:#{@auth[:password]}@api.twitter.com/1/statuses/retweet/#{@id}.json").to_return(:body => 'content')
|
13
|
-
end
|
14
|
-
|
15
|
-
# teardown the test
|
16
|
-
# nil all instance variables
|
17
|
-
def teardown
|
18
|
-
@id, @retweet = nil
|
19
|
-
end
|
20
|
-
|
21
|
-
# retweet
|
22
|
-
def test_retweet
|
23
|
-
# no permission to access web!
|
24
|
-
WebMock.disable_net_connect!
|
25
|
-
# retweet the given id
|
26
|
-
response = @retweet.retweet(@id)
|
27
|
-
# assertion
|
28
|
-
assert_not_nil(response)
|
29
|
-
assert_equal('content',response.body)
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|