balinterdi-hashtag_retweet_bot 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,7 @@
1
+ bin/hashtag_retweet_bot
2
+ create_db_table.rb
3
+ feed.rb
4
+ lib/bot.rb
5
+ Manifest
6
+ Rakefile
7
+ README.markdown
data/README.markdown ADDED
@@ -0,0 +1,42 @@
1
+ # Hashtag retweet bot
2
+
3
+ A bot that retweets all tweets tagged by a certain tag. Ideal for conferences, meetup groups, communities, etc.
4
+
5
+ ## Installation
6
+
7
+ gem install balinterdi-hashtag_retweet_bot --source http://gems.github.com
8
+
9
+ Create the database that will hold the tweets:
10
+
11
+ mysqladmin create my_conference
12
+
13
+ Create a directory where you will run the bot from:
14
+
15
+ mkdir bot_for_my_conference
16
+ cd bot_for_my_conference
17
+ touch database.yml bot.yml
18
+
19
+ Put the credentials for the twitter account in _bot.yml_:
20
+
21
+ login: my_conference_bot
22
+ password: secret
23
+
24
+ Put the database connection attributes in _database.yml_:
25
+
26
+ adapter: mysql
27
+ username: root
28
+ host: localhost
29
+ password:
30
+ database: my_conference
31
+
32
+ ## Run it
33
+
34
+ Now the only thing left to do is to run the bot:
35
+
36
+ hashtag_retweet_bot my_conference
37
+
38
+ ## Credits
39
+
40
+ Original idea and script by [Mark Connell](http://github.com/mconnell) for Scotland on Rails 2009. Customization for Euruko '09 by [Jaime Iniesta](http://github.com/jaimeiniesta). Some improvements and gemification by [Balint Erdi](http://github.com/balinterdi).
41
+
42
+ Please send feedback and bug reports to <balint@bucionrails.com>
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('hashtag_retweet_bot', '0.1') 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/balinterdi/hashtag_retweet_bot"
8
+ p.author = "Balint Erdi"
9
+ p.email = "balint@bucionrails.com"
10
+ p.ignore_pattern = ["tmp/*", "config/*", "script/*", "*.txt", "pkg"]
11
+ p.development_dependencies = ["twibot >=0.1.7", "active_record", "pauldix-feedzirra >=0.0.12"]
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,16 @@
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]"
7
+ exit
8
+ end
9
+
10
+ tag = ARGV.first
11
+ if tag.downcase == 'create_db_table'
12
+ require File.join(File.dirname(__FILE__), '..', 'create_db_table')
13
+ else
14
+ bot = HashtagRetweetBot.new(tag)
15
+ bot.run
16
+ 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,41 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{hashtag_retweet_bot}
5
+ s.version = "0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Balint Erdi"]
9
+ s.date = %q{2009-06-28}
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{balint@bucionrails.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", "lib/bot.rb", "Manifest", "Rakefile", "README.markdown", "hashtag_retweet_bot.gemspec"]
16
+ s.homepage = %q{http://github.com/balinterdi/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_development_dependency(%q<twibot>, [">= 0.1.7"])
29
+ s.add_development_dependency(%q<active_record>, [">= 0"])
30
+ s.add_development_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
31
+ else
32
+ s.add_dependency(%q<twibot>, [">= 0.1.7"])
33
+ s.add_dependency(%q<active_record>, [">= 0"])
34
+ s.add_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<twibot>, [">= 0.1.7"])
38
+ s.add_dependency(%q<active_record>, [">= 0"])
39
+ s.add_dependency(%q<pauldix-feedzirra>, [">= 0.0.12"])
40
+ end
41
+ end
data/lib/bot.rb ADDED
@@ -0,0 +1,73 @@
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
+ #
17
+
18
+ class HashtagRetweetBot
19
+ def initialize(tag)
20
+ @tag = tag
21
+ end
22
+
23
+ def run
24
+
25
+ adapter_attrs = YAML.load(File.open('config/database.yml') { |f| f.read })
26
+ ActiveRecord::Base.establish_connection(adapter_attrs)
27
+
28
+ feed_thread = Thread.new do
29
+ while(true != false)
30
+ begin
31
+ # fetch the feed
32
+ feed = get_tweets_tagged_with(@tag)
33
+ puts "XXX Feed entries length: #{feed.entries.length}"
34
+ feed.entries.reverse.each do |entry|
35
+ tweet = Tweets.find_or_create_by_twitter_id(
36
+ :twitter_id => entry.id,
37
+ :published => entry.published,
38
+ :title => entry.title,
39
+ :content => entry.content,
40
+ :link => entry.url
41
+ )
42
+
43
+ if tweet.tweeted.blank?
44
+ origin = tweet.link.gsub(/^http.*com\//,"").gsub(/\/statuses\/\d*/,"")
45
+ # strip the whole tag at the end of the tweet (since it is just for tagging)
46
+ message = tweet.title.gsub(%r{#(#{@tag})\s*$}i, '').rstrip
47
+ # strip only the # anywhere else (since it is part of the tweet)
48
+ message = message.gsub(%r{#(#{@tag})}i, '\1')
49
+ if origin.size + message.size <= 135
50
+ twitter.status(:post, "RT @#{origin}: #{message}")
51
+ else
52
+ twitter.status(:post, "RT @#{origin} tagged '#{@tag}': #{tweet.link}")
53
+ end
54
+ puts "#{Time.now.to_s(:long)}" # poor mans logging
55
+ tweet.update_attribute(:tweeted, true)
56
+ end
57
+ end
58
+ rescue
59
+ end
60
+ sleep(60)
61
+ end
62
+ end
63
+
64
+ feed_thread.join
65
+ end
66
+
67
+ private
68
+ def get_tweets_tagged_with(tag)
69
+ Feedzirra::Feed.fetch_and_parse("http://search.twitter.com/search.atom?q=+%23#{tag}")
70
+ end
71
+
72
+ end # class
73
+
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: balinterdi-hashtag_retweet_bot
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Balint Erdi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-28 00:00:00 -07:00
13
+ default_executable: hashtag_retweet_bot
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: twibot
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.1.7
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: active_record
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: pauldix-feedzirra
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.0.12
44
+ version:
45
+ 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)
46
+ email: balint@bucionrails.com
47
+ executables:
48
+ - hashtag_retweet_bot
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - bin/hashtag_retweet_bot
53
+ - lib/bot.rb
54
+ - README.markdown
55
+ files:
56
+ - bin/hashtag_retweet_bot
57
+ - create_db_table.rb
58
+ - feed.rb
59
+ - lib/bot.rb
60
+ - Manifest
61
+ - Rakefile
62
+ - README.markdown
63
+ - hashtag_retweet_bot.gemspec
64
+ has_rdoc: false
65
+ homepage: http://github.com/balinterdi/hashtag_retweet_bot
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --line-numbers
69
+ - --inline-source
70
+ - --title
71
+ - Hashtag_retweet_bot
72
+ - --main
73
+ - README.markdown
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "1.2"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project: hashtag_retweet_bot
91
+ rubygems_version: 1.2.0
92
+ signing_key:
93
+ specification_version: 3
94
+ 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)
95
+ test_files: []
96
+