jaimeiniesta-hashtag_retweet_bot 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,8 @@
1
+ bin/hashtag_retweet_bot
2
+ create_db_table.rb
3
+ feed.rb
4
+ hashtag_retweet_bot.gemspec
5
+ lib/bot.rb
6
+ Manifest
7
+ Rakefile
8
+ README.markdown
data/README.markdown ADDED
@@ -0,0 +1,66 @@
1
+ # Hashtag retweet bot
2
+
3
+ A bot that retweets all tweets tagged by a certain hashtag. Ideal for conferences, meetup groups, communities, etc.
4
+
5
+ As an example, let's say you want to retweet every twitt found with the hashtag #icecream every 5 minutes (300 seconds). This ruby gem will let you do it as easily as running:
6
+
7
+ hashtag_retweet_bot icecream 300
8
+
9
+ ## Installation
10
+
11
+ gem install jaimeiniesta-hashtag_retweet_bot --source http://gems.github.com
12
+
13
+ Create the database:
14
+
15
+ mysqladmin create my_conference_db
16
+
17
+ Create a directory where you will run the bot from, and a config directory with 2 files in it: _database.yml_ for your database settings and _bot.yml_ for the login/password of the twitter account.
18
+
19
+ mkdir -p bot_for_my_conference/config
20
+ cd config/bot_for_my_conference
21
+ touch database.yml bot.yml
22
+
23
+ Put the credentials for the twitter account in _bot.yml_:
24
+
25
+ login: my_conference_bot
26
+ password: secret
27
+
28
+ Put the database connection attributes in _database.yml_:
29
+
30
+ adapter: mysql
31
+ username: root
32
+ host: localhost
33
+ password:
34
+ database: my_conference_db
35
+
36
+ And create the table on the database that will hold the tweets:
37
+
38
+ hashtag_retweet_bot create_db_table
39
+
40
+ ## Run it
41
+
42
+ Now the only thing left to do is running the bot, telling it what tag to follow, like that:
43
+
44
+ hashtag_retweet_bot tag_to_follow
45
+
46
+ This will launch the bot with a default pause of 180 seconds. If you want a different time schedule you can specify other value for the seconds parameter, but I've experienced Twitter connection problems when trying to update every 60 seconds or less:
47
+
48
+ hashtag_retweet_bot tag_to_follow seconds_between_updates
49
+
50
+ Example:
51
+
52
+ hashtag_retweet_bot icecream
53
+ hashtag_retweet_bot icecream 240
54
+
55
+ ## Live examples
56
+
57
+ Some twitterbots using hashtag_retweet_bot:
58
+
59
+ [@euruko_bot](http://twitter.com/euruko_bot)
60
+ [@confrorbot](http://twitter.com/confrorbot)
61
+
62
+ ## Credits
63
+
64
+ Original idea and script by [Mark Connell](http://github.com/mconnell) for Scotland on Rails 2009. Customization for Euruko '09, and some improvements by [Jaime Iniesta](http://github.com/jaimeiniesta). Some improvements and gemification by [Balint Erdi](http://github.com/balinterdi).
65
+
66
+ Please send feedback and bug reports to <jaimeiniesta@gmail.com>
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('hashtag_retweet_bot', '0.1.2') do |p|
6
+ p.description = "Script that listens to a tag and retweets messages with that tag. Originally made for Scotland on Rails '09 by Mark Connell (http://github.com/rubaidh)"
7
+ p.url = "http://github.com/jaimeiniesta/hashtag_retweet_bot"
8
+ p.author = "Jaime Iniesta"
9
+ p.email = "jaimeiniesta@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "config/*", "script/*", "*.txt", "pkg"]
11
+ p.development_dependencies = ["twibot >=0.1.7", "activerecord", "pauldix-feedzirra >=0.0.12"]
12
+ p.runtime_dependencies = ["twibot >=0.1.7", "activerecord", "pauldix-feedzirra >=0.0.12"]
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'bot')
4
+
5
+ if ARGV.length < 1
6
+ puts "Usage: hashtag_retweet_bot [tag_to_watch] [seconds_between_updates]"
7
+ puts "If you haven't created the database table, run:"
8
+ puts "hashtag_retweet_bot create_db_table"
9
+ exit
10
+ end
11
+
12
+ tag = ARGV[0]
13
+ seconds = ARGV[1].to_i || 180
14
+ if tag.downcase == 'create_db_table'
15
+ puts "Creating the database"
16
+ require File.join(File.dirname(__FILE__), '..', 'create_db_table')
17
+ else
18
+ puts "Launching the twitterbot to track \##{tag} every #{seconds} seconds"
19
+ bot = HashtagRetweetBot.new(tag, seconds)
20
+ bot.run
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'yaml'
4
+
5
+ adapter_attrs = YAML.load(File.open('config/database.yml') { |f| f.read })
6
+ ActiveRecord::Base.establish_connection(adapter_attrs)
7
+
8
+ ActiveRecord::Schema.define do
9
+ create_table(:tweets, :force => true) do |t|
10
+ t.string :twitter_id
11
+ t.datetime :published
12
+ t.string :link
13
+ t.string :title
14
+ t.string :content
15
+ t.string :author_name
16
+ t.string :author_uri
17
+ t.boolean :tweeted
18
+ end
19
+ end
data/feed.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'feedzirra' # feed helper
3
+
4
+ feed = Feedzirra::Feed.fetch_and_parse("http://search.twitter.com/search.atom?q=+%23euruko")
5
+
6
+ feed.entries.each do |entry|
7
+
8
+ puts "entry #{entry.inspect}"
9
+
10
+ end
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{hashtag_retweet_bot}
5
+ s.version = "0.1.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Balint Erdi", "Jaime Iniesta"]
9
+ s.date = %q{2009-08-16}
10
+ s.default_executable = %q{hashtag_retweet_bot}
11
+ s.description = %q{Script that listens to a tag and retweets messages with that tag. Originally made for Scotland on Rails '09 by Mark Connell (http://github.com/rubaidh)}
12
+ s.email = %q{jaimeiniesta@gmail.com}
13
+ s.executables = ["hashtag_retweet_bot"]
14
+ s.extra_rdoc_files = ["bin/hashtag_retweet_bot", "lib/bot.rb", "README.markdown"]
15
+ s.files = ["bin/hashtag_retweet_bot", "create_db_table.rb", "feed.rb", "hashtag_retweet_bot.gemspec", "lib/bot.rb", "Manifest", "Rakefile", "README.markdown"]
16
+ s.homepage = %q{http://github.com/jaimeiniesta/hashtag_retweet_bot}
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Hashtag_retweet_bot", "--main", "README.markdown"]
18
+ s.require_paths = ["lib"]
19
+ s.rubyforge_project = %q{hashtag_retweet_bot}
20
+ s.rubygems_version = %q{1.3.3}
21
+ s.summary = %q{Script that listens to a tag and retweets messages with that tag. Originally made for Scotland on Rails '09 by Mark Connell (http://github.com/rubaidh)}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<twibot>, [">= 0.1.7"])
29
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
30
+ s.add_runtime_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
31
+ s.add_development_dependency(%q<twibot>, [">= 0.1.7"])
32
+ s.add_development_dependency(%q<activerecord>, [">= 0"])
33
+ s.add_development_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
34
+ else
35
+ s.add_dependency(%q<twibot>, [">= 0.1.7"])
36
+ s.add_dependency(%q<activerecord>, [">= 0"])
37
+ s.add_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
38
+ s.add_dependency(%q<twibot>, [">= 0.1.7"])
39
+ s.add_dependency(%q<activerecord>, [">= 0"])
40
+ s.add_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
41
+ end
42
+ else
43
+ s.add_dependency(%q<twibot>, [">= 0.1.7"])
44
+ s.add_dependency(%q<activerecord>, [">= 0"])
45
+ s.add_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
46
+ s.add_dependency(%q<twibot>, [">= 0.1.7"])
47
+ s.add_dependency(%q<activerecord>, [">= 0"])
48
+ s.add_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
49
+ end
50
+ end
data/lib/bot.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'rubygems'
2
+ require 'twibot' # our bot helper
3
+ require 'active_record' # db
4
+ require 'feedzirra' # feed helper
5
+ require 'yaml'
6
+
7
+ class Tweets < ActiveRecord::Base
8
+ end
9
+
10
+ ###
11
+ # What we want the bot to do
12
+ ###
13
+ # 1. Listen to an rss feed and store that stuff
14
+ # 2. Work out which tweets need to be tweeted by the bot
15
+ # 3. send the tweets and mark them as 'tweeted'
16
+ # 4. repeat ad nauseam every x seconds (as indicated, 180 is recommended)
17
+ #
18
+
19
+ class HashtagRetweetBot
20
+ def initialize(tag, seconds=180)
21
+ @tag = tag
22
+ @seconds = seconds
23
+ end
24
+
25
+ def run
26
+
27
+ adapter_attrs = YAML.load(File.open('config/database.yml') { |f| f.read })
28
+ ActiveRecord::Base.establish_connection(adapter_attrs)
29
+
30
+ feed_thread = Thread.new do
31
+ while(true != false)
32
+ begin
33
+ # fetch the feed
34
+ feed = get_tweets_tagged_with(@tag)
35
+ feed.entries.reverse.each do |entry|
36
+ tweet = Tweets.find_or_create_by_twitter_id(
37
+ :twitter_id => entry.id,
38
+ :published => entry.published,
39
+ :title => entry.title,
40
+ :content => entry.content,
41
+ :link => entry.url
42
+ )
43
+
44
+ if tweet.tweeted.blank?
45
+ origin = tweet.link.gsub(/^http.*com\//,"").gsub(/\/statuses\/\d*/,"")
46
+ # strip the whole tag at the end of the tweet (since it is just for tagging)
47
+ message = tweet.title.gsub(%r{#(#{@tag})\s*$}i, '').rstrip
48
+ # strip only the # anywhere else (since it is part of the tweet)
49
+ message = message.gsub(%r{#(#{@tag})}i, '\1')
50
+ if origin.size + message.size <= 135
51
+ twitter.status(:post, "RT @#{origin}: #{message}")
52
+ else
53
+ twitter.status(:post, "RT @#{origin} tagged '#{@tag}': #{tweet.link}")
54
+ end
55
+ puts "#{Time.now.to_s(:long)}" # poor mans logging
56
+ tweet.update_attribute(:tweeted, true)
57
+ end
58
+ end
59
+ rescue
60
+ end
61
+ sleep(@seconds)
62
+ end
63
+ end
64
+
65
+ feed_thread.join
66
+ end
67
+
68
+ private
69
+ def get_tweets_tagged_with(tag)
70
+ Feedzirra::Feed.fetch_and_parse("http://search.twitter.com/search.atom?q=+%23#{tag}")
71
+ end
72
+
73
+ end # class
74
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jaimeiniesta-hashtag_retweet_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Balint Erdi
8
+ - Jaime Iniesta
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-08-16 00:00:00 -07:00
14
+ default_executable: hashtag_retweet_bot
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: twibot
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.1.7
25
+ version:
26
+ - !ruby/object:Gem::Dependency
27
+ name: activerecord
28
+ type: :runtime
29
+ version_requirement:
30
+ version_requirements: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ version:
36
+ - !ruby/object:Gem::Dependency
37
+ name: pauldix-feedzirra
38
+ type: :runtime
39
+ version_requirement:
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 0.0.12
45
+ version:
46
+ - !ruby/object:Gem::Dependency
47
+ name: twibot
48
+ type: :development
49
+ version_requirement:
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.7
55
+ version:
56
+ - !ruby/object:Gem::Dependency
57
+ name: activerecord
58
+ type: :development
59
+ version_requirement:
60
+ version_requirements: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ - !ruby/object:Gem::Dependency
67
+ name: pauldix-feedzirra
68
+ type: :development
69
+ version_requirement:
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 0.0.12
75
+ version:
76
+ description: Script that listens to a tag and retweets messages with that tag. Originally made for Scotland on Rails '09 by Mark Connell (http://github.com/rubaidh)
77
+ email: jaimeiniesta@gmail.com
78
+ executables:
79
+ - hashtag_retweet_bot
80
+ extensions: []
81
+
82
+ extra_rdoc_files:
83
+ - bin/hashtag_retweet_bot
84
+ - lib/bot.rb
85
+ - README.markdown
86
+ files:
87
+ - bin/hashtag_retweet_bot
88
+ - create_db_table.rb
89
+ - feed.rb
90
+ - hashtag_retweet_bot.gemspec
91
+ - lib/bot.rb
92
+ - Manifest
93
+ - Rakefile
94
+ - README.markdown
95
+ has_rdoc: false
96
+ homepage: http://github.com/jaimeiniesta/hashtag_retweet_bot
97
+ licenses:
98
+ post_install_message:
99
+ rdoc_options:
100
+ - --line-numbers
101
+ - --inline-source
102
+ - --title
103
+ - Hashtag_retweet_bot
104
+ - --main
105
+ - README.markdown
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: "0"
113
+ version:
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: "1.2"
119
+ version:
120
+ requirements: []
121
+
122
+ rubyforge_project: hashtag_retweet_bot
123
+ rubygems_version: 1.3.5
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Script that listens to a tag and retweets messages with that tag. Originally made for Scotland on Rails '09 by Mark Connell (http://github.com/rubaidh)
127
+ test_files: []
128
+