neufelry-twitter-sms 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +32 -0
  2. data/bin/twitter-sms +157 -0
  3. data/example.conf +13 -0
  4. metadata +54 -0
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ twitter-sms: Twitter SMS for the rest of us.
2
+ =======================================
3
+
4
+ Twitter-sms bridges the gap left by Twitter.com when it removed SMS capability for many countries. Twitter-sms runs in the background and periodically checks Twitter for new tweets; When new tweets are found they are forwarded via a gmail account to your mobile phone's email address.
5
+
6
+ All it requires is:
7
+ * A running computer with internet access and Ruby installed. (*nix systems only for the moment.),
8
+ * A google email address (to forward messages from) and,
9
+ * An SMS capable phone with an email-to-sms email address. (i.e. 12345551234@text.provider.net that automatically forwards messages to your phone as SMS)
10
+
11
+ DISCLAIMER: Please bear in mind that it MAY cost you to receive text message on your email address; I am not responsable for any costs incurred to you using this program.
12
+
13
+ Setup
14
+ -----
15
+ To use twitter-sms you will need only to create the config file "~/.twitter-sms.conf" and fill it with the necessary YAML mappings.
16
+
17
+ Here is an example conf:
18
+
19
+ bot:
20
+ email: "twitter.bot@gmail.com"
21
+ password: p0n33zR0oL
22
+ user:
23
+ name: rkneufeld
24
+ password: p0n33zR0oL
25
+ phone: "12045555555@text.provider.net"
26
+ config:
27
+ per_hour: 12
28
+ keep_alive: true
29
+
30
+ This config will cause the script to run forever; sending you the last 5 minutes tweets every 5 minutes. (60 / 5 == 12)
31
+
32
+ One can also run the script as a cron job; instructions to follow soon...
data/bin/twitter-sms ADDED
@@ -0,0 +1,157 @@
1
+ #!/opt/local/bin/ruby
2
+ require 'rubygems'
3
+ require 'twitter'
4
+ require 'tlsmail'
5
+ require 'optparse'
6
+ require 'cgi'
7
+
8
+ SEC_PER_HOUR = 3600 # Seconds per hour
9
+
10
+ # Put Debug (prepend time)
11
+ def putd (message)
12
+ puts "#{Time.now.strftime("(%b %d - %H:%M:%S)")} #{message}"
13
+ end
14
+
15
+ class TwitterSms
16
+
17
+ def initialize(config_file="#{ENV['HOME'] + '/.twitter-sms.conf'}", args=ARGV)
18
+ load_config(config_file)
19
+ load_opts(args)
20
+
21
+ # Required for gmail smtp (and not a part of standalone Net::SMTP)
22
+ Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
23
+ end
24
+
25
+ # Run the twitter-sms bot once or repeatedly, depending on config
26
+ def run
27
+ begin # do-while
28
+ load_config if config_stale?
29
+
30
+ tweets = update
31
+ send tweets unless tweets.nil?
32
+
33
+ if @config['keep_alive']
34
+ putd "Waiting for ~#{@config['wait']/60} minutes..."
35
+ Kernel.sleep @config['wait']
36
+ end
37
+ end while @config['keep_alive']
38
+ end
39
+
40
+ # Collect a list of recent tweets on a user's timeline (that are not their own)
41
+ def update
42
+ twitter = Twitter::Base.new(@user['name'],@user['password'])
43
+
44
+ begin
45
+ if twitter.rate_limit_status.remaining_hits > 0
46
+ now = Time.now
47
+ tweets = twitter.timeline(:friends, :since => @last_check)
48
+ @last_check = now
49
+
50
+ # Block own tweets if specified via settings
51
+ tweets.reject! {|t| t.user.screen_name == @user['name']} unless @config['own_tweets']
52
+ # Don't send messages from users under no_follow
53
+ tweets.reject! {|t| @config['no_follow'].member?(t.user.screen_name) }
54
+ tweets.reverse! # reverse-chronological
55
+ else
56
+ putd "Your account has run out of API calls; call not made."
57
+ end
58
+ rescue
59
+ putd "Error occured retreiving timeline. Perhaps Internet is down?"
60
+ end
61
+ end
62
+
63
+ # Email via Gmail SMTP any tweets to the desired cell phone
64
+ def send(tweets)
65
+ putd "Sending received tweets..."
66
+ begin
67
+ Net::SMTP.start('smtp.gmail.com', 587, 'gmail.com', @bot['email'], "tweeterbot", :login) do |smtp|
68
+ tweets.each do |tweet|
69
+ smtp.send_message(content(tweet),@bot['email'],@user['phone']) rescue putd "Error occured sending message:"
70
+ putd "\tSent: #{tweet.user.screen_name}: #{tweet.text[0..20]}..."
71
+ end
72
+ end
73
+ putd "Messages sent."
74
+ rescue
75
+ putd "Error occured starting smtp. Perhaps account info is incorrect or Internet is down?"
76
+ end
77
+ end
78
+
79
+ # Parse and store a config file (either as an initial load or as an update)
80
+ def load_config(config_file=@config_file['name'])
81
+ # Load config file -- Add try block here
82
+ config = YAML.load_file(config_file)
83
+ @config_file = { 'name' => config_file,
84
+ 'modified_at' => File.mtime(config_file) }
85
+
86
+
87
+ # Seperate config hashes into easier to use parts
88
+ @user = config['user']
89
+ @bot = config['bot']
90
+ # Merge specified config onto defaults
91
+ default = { 'own_tweets' => false,
92
+ 'keep_alive' => true,
93
+ 'per_hour' => 60}
94
+ @config = config['config'].merge(default)
95
+
96
+ set_wait
97
+ @last_check = Time.now - @config['wait']
98
+ end
99
+
100
+ private
101
+
102
+ # Set wait time based on times per hour
103
+ def set_wait
104
+ @config['wait'] = SEC_PER_HOUR / @config['per_hour']
105
+ end
106
+
107
+ def load_opts(args)
108
+ opts = OptionParser.new do |opts|
109
+ opts.banner = "Twitter-sms bot help menu:\n"
110
+ opts.banner += "==========================\n"
111
+ opts.banner += "Usage #$0 [options]"
112
+
113
+ opts.on('-t', '--times-per-hour [TIMES]',
114
+ 'Indicate the amount of times per hour to check (0 < TIMES <= 100)') do |times|
115
+ times = 100 if times > 100
116
+ times = 1 if times < 1 # once per hour minimum?
117
+ @config['per_hour'] = times
118
+ end
119
+
120
+ opts.on('-o', '--own-tweets',
121
+ "Makes your own tweets send in addition to followed account\'s tweets") do
122
+ @config['own_tweets'] = true
123
+ end
124
+
125
+ opts.on('-s', '--single-check',
126
+ 'Forces the program to only check once, instead of continually') do
127
+ @config['keep_alive'] = false
128
+ end
129
+
130
+ opts.on_tail('-h', '--help', 'display this help and exit') do
131
+ puts opts
132
+ exit
133
+ end
134
+ end
135
+
136
+ opts.parse!(args)
137
+ end
138
+
139
+ def config_stale?
140
+ return File.mtime(@config_file['name']) > @config_file['modified_at']
141
+ end
142
+
143
+ def content(tweet)
144
+ "From: #{@bot['email']}\n"+
145
+ "To: #{@user['phone']}\n"+
146
+ "Date: #{Time.parse(tweet.created_at).rfc2822}\n\n"+
147
+ "#{tweet.user.screen_name}: #{CGI.escapeHTML(tweet.text)}"
148
+ end
149
+
150
+ end
151
+
152
+ # Run as program only if library is the running program
153
+ if $0.split('/')[-1] == __FILE__.split('/')[-1] # Can anyone fix this idiom for me; I just know its wrong
154
+ # Add some logic for command line options
155
+ program = TwitterSms.new
156
+ program.run
157
+ end
data/example.conf ADDED
@@ -0,0 +1,13 @@
1
+ bot:
2
+ email: "twitter.bot@gmail.com"
3
+ password: p0n33zR0oL
4
+ user:
5
+ name: rkneufeld
6
+ password: p0n33zR0oL
7
+ phone: "12045555555@text.provider.net"
8
+ config:
9
+ per_hour: 12
10
+ keep_alive: true
11
+ no_follow:
12
+ - annoying_guy
13
+ - annoying_girl
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neufelry-twitter-sms
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.2"
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Neufeld
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Twitter-SMS provides a persistent command line tool to send SMS updates to your mobile phone via a gmail account.
17
+ email: neufelry@gmail.com
18
+ executables:
19
+ - - twitter-sms
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.md
26
+ - example.conf
27
+ has_rdoc: false
28
+ homepage: http://www.github.com/neufelry/twitter-sms
29
+ post_install_message:
30
+ rdoc_options: []
31
+
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: "0"
39
+ version:
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ requirements: []
47
+
48
+ rubyforge_project:
49
+ rubygems_version: 1.2.0
50
+ signing_key:
51
+ specification_version: 2
52
+ summary: Twitter-SMS lets you send SMS updates to your phone.
53
+ test_files: []
54
+