-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet.rb
126 lines (117 loc) · 3.75 KB
/
tweet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class Tweet
def initialize
@twitterrc = File.join(ENV['HOME'], '.twitter')
@config = YAML.load(File.read(@twitterrc))
tweets = File.dirname(__FILE__) + '/tweets'
@store = PStore.new(tweets)
@username = @config['username']
end
def post(text, reply_to_status_id = nil)
@twitter = Twitter.new(@username)
options = {:query => {}}
options[:query][:status] = text
options[:query][:in_reply_to_status_id] = reply_to_status_id if reply_to_status_id
@twitter.post(options)
end
def reply(reply_to_status_id)
@store.transaction do
colored_status = @store[reply_to_status_id.to_i].uncolored
status = colored_status.uncolored
screen_name = status.match(/[\s]?[\w]+:[\s]/)[0].gsub(':', '').strip
text = "@#{screen_name} "
message = ask("Reply to: #{colored_status}"){|q| q.echo = true}
text += message
if message.size > 0 && text.size <= 140
post(text, reply_to_status_id)
puts text
else
extra_chars = text.size - 140
puts "I can haz no tweet longer than 140 chars! Overshot by #{extra_chars}..."
end
end
rescue
puts "I can get no status!"
end
def retweet(reply_to_status_id)
@store.transaction do
status = @store[reply_to_status_id.to_i].uncolored
puts status
screen_name = status.match(/[\s]?[\w]+:[\s]/)[0].gsub(':', '').strip
message = status.match(/:[\s].[\w].+$/)[0].sub(': ', '')
text = "RT @#{screen_name} #{message}"
string_to_prepend = STDIN.gets
text = "#{string_to_prepend.chomp} #{text}"
if text.size <= 140
post(text)
puts text
else
extra_chars = text.size - 140
puts "I can haz no tweet longer than 140 chars! Overshot by #{extra_chars} chars! Try again!"
puts text
end
end
rescue
puts "I can get no status!"
end
def user(screen_name)
@twitter = Twitter.new(@username)
user = @twitter.users(:show, :query => {:screen_name => screen_name})
user.each do |key, value|
if key == 'status'
time = Time.parse(value['created_at']).strftime("%a %I:%M%P")
puts "#{key.cyan}: #{time.blue} #{value['text'].yellow}"
else
puts "#{key.cyan}: #{value.to_s.yellow}"
end
end
puts "\n"
end
def timeline_options(since_id_type)
@timeline_options = {}
@timeline_options[:since_id] = @config[since_id_type]
@timeline_options[:count] = 200
end
def friends
@twitter = Twitter.new(@username)
timeline_options('last_recent_id')
@timeline = @twitter.timeline(:friends, :query => @timeline_options)
store('last_recent_id')
end
def mentions
@twitter = Twitter.new(@username)
timeline_options('last_mention_id')
@timeline = @twitter.mentions(:query => @timeline_options)
store('last_mention_id')
end
def store(since_id_type)
n = 0
@timeline.each do |status|
user = status['user']
status_id = status['id']
created_at = Time.parse(status['created_at']).strftime("%a %I:%M%P")
@store.transaction do
string = "#{status_id.to_s.blue} #{created_at.to_s.blue} #{user['name'].cyan} #{user['screen_name'].red}: #{status['text'].yellow}"
@store[status_id] = string
puts string
end
@config[since_id_type] = status_id if n == 0
n += 1
end
File.open(@twitterrc, 'w'){|file| YAML.dump(@config, file) }
end
def history(no_of_tweets = 50, query = nil)
n = 1
@store.transaction do
@store.roots.sort{|a,b| b <=> a}.each do |index|
message = @store[index]
if query
puts message if message =~ /.+#{query}.+/
else
puts message
end
break if n == no_of_tweets.to_i && query == nil
n += 1
end
end
end
end