camptweet 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,3 @@
1
+ v0.8.1. Use standard logger. Bot class restructured. Better error handling. Better Campfire spam protection.
2
+
3
+ v0.8. First Version
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2008 Brian Dainton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,9 @@
1
+ bin/camptweet
2
+ bin/camptweetd_base
3
+ CHANGELOG
4
+ init.rb
5
+ lib/camptweet/bot.rb
6
+ lib/camptweet.rb
7
+ LICENSE
8
+ README.rdoc
9
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ = CampTweet
2
+
3
+ == Synopsis
4
+
5
+ A simple daemon that polls for updated Twitter statuses and posts them to a Campfire room.
6
+
7
+ == Installation
8
+
9
+ Install the gem and its dependencies:
10
+
11
+ sudo gem install camptweet
12
+
13
+ From the commandline, run:
14
+
15
+ camptweet
16
+
17
+ This creates a 'camptweetd' file.
18
+
19
+ Edit this file to specify the Twitter user screen names for which you'd like to grab status information, the desired Campfire subdomain and room name, and the Campfire email/password with which this daemon should use to connect.
20
+
21
+ Then, run:
22
+
23
+ ./camptweetd
24
+
25
+ The latest Twitter statuses for the users you have specified will be broadcast into your Campfire room. Nohup and background this script excecution to keep it going (nohup ./camptweetd &).
26
+
27
+ == License
28
+
29
+ The MIT License
30
+
31
+ Copyright (c) 2008 Brian Dainton
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining a copy
34
+ of this software and associated documentation files (the "Software"), to deal
35
+ in the Software without restriction, including without limitation the rights
36
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37
+ copies of the Software, and to permit persons to whom the Software is
38
+ furnished to do so, subject to the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be included in
41
+ all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
44
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
45
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
46
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
47
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
48
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
49
+ THE SOFTWARE.
data/bin/camptweet ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'fileutils'
4
+
5
+ destination = ARGV[0] || Dir.pwd
6
+ unless File.directory?(destination)
7
+ abort "CampTweet: #{destination} does not exist; cannot create CampTweet daemon script"
8
+ end
9
+
10
+ if File.exists?(File.join(destination, 'camptweetd'))
11
+ abort "CampTweet: CampTweet daemon script already exists at #{destination}. Exiting."
12
+ end
13
+
14
+ origin = File.join(File.dirname(__FILE__), "camptweetd_base")
15
+ FileUtils.mkdir_p destination
16
+ FileUtils.cp origin, File.join(destination, 'camptweetd')
17
+
18
+ STDERR.puts "CampTweet daemon script created at #{File.join(destination, 'camptweetd')}."
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'camptweet'
4
+
5
+ Camptweet::Bot.new do |camptweet|
6
+ camptweet.twitter_users = ['bdainton']
7
+ camptweet.campfire_subdomain = 'mycompany'
8
+ camptweet.campfire_use_ssl = true
9
+ camptweet.campfire_room = 'Room Name'
10
+ camptweet.campfire_email = 'foo@mycompany.com'
11
+ camptweet.campfire_password = 'foo_password'
12
+ camptweet.verbose = false
13
+ camptweet.logfile = 'camptweet.log'
14
+ end.run
data/camptweet.gemspec ADDED
@@ -0,0 +1,57 @@
1
+
2
+ # Gem::Specification for Camptweet-0.8.1
3
+ # Originally generated by Echoe
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = %q{camptweet}
7
+ s.version = "0.8.1"
8
+
9
+ s.specification_version = 2 if s.respond_to? :specification_version=
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Brian Dainton"]
13
+ s.date = %q{2008-06-08}
14
+ s.description = %q{A simple daemon that polls for updated Twitter statuses and posts them to a Campfire room.}
15
+ s.email = %q{}
16
+ s.executables = ["camptweet", "camptweetd_base"]
17
+ s.extra_rdoc_files = ["bin/camptweet", "bin/camptweetd_base", "CHANGELOG", "lib/camptweet/bot.rb", "lib/camptweet.rb", "LICENSE", "README.rdoc"]
18
+ s.files = ["bin/camptweet", "bin/camptweetd_base", "CHANGELOG", "init.rb", "lib/camptweet/bot.rb", "lib/camptweet.rb", "LICENSE", "README.rdoc", "Manifest", "camptweet.gemspec"]
19
+ s.has_rdoc = true
20
+ s.homepage = %q{http://github.com/bdainton/camptweet}
21
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Camptweet", "--main", "README.rdoc"]
22
+ s.require_paths = ["lib"]
23
+ s.rubyforge_project = %q{camptweet}
24
+ s.rubygems_version = %q{1.1.1}
25
+ s.summary = %q{A simple daemon that polls for updated Twitter statuses and posts them to a Campfire room.}
26
+
27
+ s.add_dependency(%q<twitter4r>, [">= 0.3.0"])
28
+ s.add_dependency(%q<tinder>, [">= 0.1.6"])
29
+ end
30
+
31
+
32
+ # # Original Rakefile source (requires the Echoe gem):
33
+ #
34
+ # # -*- ruby -*-
35
+ #
36
+ # require 'rubygems'
37
+ # require 'rake/testtask'
38
+ # require 'echoe'
39
+ #
40
+ # Echoe.new('camptweet') do |p|
41
+ # p.author = "Brian Dainton"
42
+ # p.summary = "A simple daemon that polls for updated Twitter statuses and posts them to a Campfire room."
43
+ # p.url = "http://github.com/bdainton/camptweet"
44
+ # p.dependencies = ["twitter4r >=0.3.0", "tinder >=0.1.6"]
45
+ # end
46
+ #
47
+ #
48
+ # task :default => :test
49
+ #
50
+ # desc 'Run all tests'
51
+ # Rake::TestTask.new('test') do |t|
52
+ # t.libs << 'test'
53
+ # t.pattern = 'test/*_test.rb'
54
+ # t.verbose = true
55
+ # end
56
+ #
57
+ # # vim: ft=ruby sw=2 ts=2 ai
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'camptweet'
@@ -0,0 +1,139 @@
1
+ module Camptweet
2
+ class Bot
3
+
4
+ attr_accessor :twitter_users
5
+ attr_accessor :campfire_subdomain
6
+ attr_accessor :campfire_use_ssl
7
+ attr_accessor :campfire_room
8
+ attr_accessor :campfire_email
9
+ attr_accessor :campfire_password
10
+ attr_accessor :verbose
11
+ attr_accessor :logfile
12
+ attr_reader :twitter, :campfire, :room, :log
13
+
14
+ def initialize(&block)
15
+ yield self if block_given?
16
+ init_log
17
+ connect_to_twitter
18
+ connect_to_campfire
19
+ login_to_campfire
20
+ connect_to_campfire_room
21
+ end
22
+
23
+ def run
24
+ last_statuses = initial_statuses
25
+
26
+ loop do
27
+ begin
28
+ new_statuses = []
29
+ checking_twitter_timelines do |user, status|
30
+ if last_statuses[user].nil?
31
+ # Only broadcast this tweet if we have an initial status against
32
+ # which we can compare it.
33
+ last_statuses[user] = status
34
+ elsif status.created_at > last_statuses[user].created_at
35
+ # Only consider the most recent tweet.
36
+ new_statuses << status
37
+ last_statuses[user] = status
38
+ end
39
+ end
40
+
41
+ new_statuses.sort_by(&:created_at).each do |status|
42
+ begin
43
+ message = "[#{status.user.name}] #{status.text}"
44
+ log.info message
45
+ room.speak message
46
+ log.debug "(Campfire updated)"
47
+ rescue Timeout::Error => e
48
+ log.info "Campfire timeout: (#{e.message})"
49
+ ensure
50
+ sleep 2
51
+ end
52
+ end
53
+ rescue => e
54
+ log.error e.message
55
+ log.error e.backtrace
56
+ end
57
+ log.debug "Sleeping (10s)"
58
+ sleep 10
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def connect_to_twitter
65
+ @twitter = Twitter::Client.new
66
+ unless twitter
67
+ log.info "Unable to establish connection to Twitter. Exiting."
68
+ exit
69
+ end
70
+ log.info "Established connection to Twitter."
71
+ end
72
+
73
+ def connect_to_campfire
74
+ @campfire = Tinder::Campfire.new(campfire_subdomain, :ssl => campfire_use_ssl)
75
+ unless campfire
76
+ log.info "Unable to establish connection to Campfire (#{campfire_subdomain}). Exiting."
77
+ exit
78
+ end
79
+ log.info "Established connection to Campfire (#{campfire_subdomain})."
80
+ end
81
+
82
+ def login_to_campfire
83
+ unless campfire.login(campfire_email, campfire_password)
84
+ log.info "Unable to log in to Campfire (#{campfire_subdomain}). Exiting."
85
+ exit
86
+ end
87
+ log.info "Logged in to Campfire (#{campfire_subdomain})."
88
+ log.debug "Available rooms: #{campfire.rooms.map(&:name).inspect}"
89
+ end
90
+
91
+ def connect_to_campfire_room
92
+ @room = campfire.find_room_by_name(campfire_room)
93
+ if room
94
+ log.info "Entered Campfire room '#{room.name}'."
95
+ else
96
+ log.info "No room '#{campfire_room}' found. Exiting."
97
+ exit
98
+ end
99
+ end
100
+
101
+ def init_log
102
+ @log = Logger.new(logfile || 'camptweet.log')
103
+ log.level = verbose? ? Logger::DEBUG : Logger::INFO
104
+ end
105
+
106
+ def verbose?
107
+ verbose
108
+ end
109
+
110
+ def initial_statuses
111
+ returning statuses = {} do
112
+ checking_twitter_timelines do |user, status|
113
+ statuses[user] = status
114
+ end
115
+ end
116
+ end
117
+
118
+ def checking_twitter_timelines
119
+ twitter_users.each do |user|
120
+ begin
121
+ log.debug "Checking '#{user}' timeline..."
122
+ twitter.timeline_for(:user, :id => user, :count => 1) do |status|
123
+ yield user, status
124
+ end
125
+ rescue Timeout::Error => e
126
+ log.error "Twitter timeout: (#{e.message})"
127
+ rescue Twitter::RESTError => e
128
+ log.error "Twitter REST Error: (#{e.message})"
129
+ rescue => e
130
+ log.error "Twitter error: (#{e.message})"
131
+ ensure
132
+ log.debug " ...done."
133
+ sleep 2
134
+ end
135
+ end
136
+ end
137
+
138
+ end
139
+ end
data/lib/camptweet.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'time'
4
+ require 'twitter'
5
+ require 'tinder'
6
+
7
+ Dir[File.join(File.dirname(__FILE__), 'camptweet/**/*.rb')].sort.each { |lib| require lib }
8
+
9
+ module Camptweet
10
+ class Error < StandardError; end
11
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camptweet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian Dainton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-08 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: twitter4r
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.3.0
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: tinder
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: 0.1.6
32
+ version:
33
+ description: A simple daemon that polls for updated Twitter statuses and posts them to a Campfire room.
34
+ email: ""
35
+ executables:
36
+ - camptweet
37
+ - camptweetd_base
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - bin/camptweet
42
+ - bin/camptweetd_base
43
+ - CHANGELOG
44
+ - lib/camptweet/bot.rb
45
+ - lib/camptweet.rb
46
+ - LICENSE
47
+ - README.rdoc
48
+ files:
49
+ - bin/camptweet
50
+ - bin/camptweetd_base
51
+ - CHANGELOG
52
+ - init.rb
53
+ - lib/camptweet/bot.rb
54
+ - lib/camptweet.rb
55
+ - LICENSE
56
+ - README.rdoc
57
+ - Manifest
58
+ - camptweet.gemspec
59
+ has_rdoc: true
60
+ homepage: http://github.com/bdainton/camptweet
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --line-numbers
64
+ - --inline-source
65
+ - --title
66
+ - Camptweet
67
+ - --main
68
+ - README.rdoc
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project: camptweet
86
+ rubygems_version: 1.1.1
87
+ signing_key:
88
+ specification_version: 2
89
+ summary: A simple daemon that polls for updated Twitter statuses and posts them to a Campfire room.
90
+ test_files: []
91
+