streambot 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/retweet.rb +6 -1
- data/lib/streambot.rb +14 -8
- metadata +3 -3
data/lib/retweet.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'open-uri'
|
3
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
|
4
6
|
class Retweet
|
5
7
|
|
6
8
|
# intitialize method aka constructor
|
@@ -13,12 +15,15 @@ class Retweet
|
|
13
15
|
def retweet(id)
|
14
16
|
url = URI.parse("http://api.twitter.com/1/statuses/retweet/#{id}.json")
|
15
17
|
req = Net::HTTP::Post.new(url.path)
|
18
|
+
# set credentials
|
16
19
|
req.basic_auth @auth[:username],@auth[:password]
|
20
|
+
# connect
|
17
21
|
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
|
18
22
|
case res
|
19
|
-
when Net::HTTPSuccess, Net::HTTPRedirection
|
20
23
|
#OK
|
24
|
+
when Net::HTTPSuccess, Net::HTTPRedirection
|
21
25
|
else
|
26
|
+
# when connection wasn't successful print error message
|
22
27
|
res.error!
|
23
28
|
end
|
24
29
|
end
|
data/lib/streambot.rb
CHANGED
@@ -2,27 +2,33 @@ require 'rubygems'
|
|
2
2
|
require 'tweetstream'
|
3
3
|
require 'retweet'
|
4
4
|
|
5
|
+
# The StreamBot class that provides a start and a stop method
|
5
6
|
class StreamBot
|
6
7
|
|
7
|
-
#
|
8
|
-
def initialize(auth, blacklist
|
8
|
+
# the initialization method aka the constructor
|
9
|
+
def initialize(auth, blacklist, *keywords)
|
9
10
|
@stream = TweetStream::Client.new(auth[:username],auth[:password])
|
10
|
-
|
11
|
+
# initializing retweet
|
12
|
+
@retweet = Retweet.new(auth) #
|
13
|
+
# get string with keywords comma separated keywords
|
11
14
|
@keywords=keywords.join(',')
|
12
|
-
|
15
|
+
# set blacklist array if not nil
|
16
|
+
@blacklist=blacklist
|
13
17
|
end
|
14
18
|
|
15
|
-
#
|
19
|
+
# start the bot
|
16
20
|
def start
|
21
|
+
# starting to track the keywords via tweetstream
|
17
22
|
@stream.track(@keywords) do |status|
|
18
|
-
if
|
19
|
-
|
23
|
+
# if status.user is NOT in blacklist then retweet it
|
24
|
+
if !@blacklist.include?(status.user)
|
25
|
+
#puts "#{status.text}"
|
20
26
|
@retweet.retweet(status.id)
|
21
27
|
end
|
22
28
|
end
|
23
29
|
end
|
24
30
|
|
25
|
-
# stop bot
|
31
|
+
# stop the bot
|
26
32
|
def stop
|
27
33
|
@stream.stop
|
28
34
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 3
|
9
|
+
version: 0.2.3
|
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-04-
|
17
|
+
date: 2010-04-15 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|