-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitcurses.rb
146 lines (115 loc) · 2.88 KB
/
twitcurses.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env ruby
# TODO
# - Add optparse
# - different colors for individuals
# - show current length of the tweet/character count
# - mentions page
# - all the mentions
# - color. if a reply has been made then normal
# if a reply has not been made then yellow or red
# - search
# - followers
# - list followers
# - add followers
# - remove followers
# - direct message
require 'date'
require 'rubygems'
require 'yaml'
require 'curses'
require 'open-uri'
require 'twitter'
ConfigFile = "#{ENV['HOME']}/.twitcurses"
def header
Curses::addstr("Twitcurses by abhiyerra\n\n")
end
def create_refresher_thread
@refresher = Thread.new do
loop do
update_timeline
Curses.refresh
sleep @config["refresh-rate"]
end
end
end
def destroy_refresher_thread
Thread.kill @refresher
end
def restart_refresher_thread
destroy_refresher_thread
create_refresher_thread
end
def update_timeline
Curses.clear
header
Curses.addstr "Last updated: #{Time.now}\n"
@twitter.friends_timeline.each do |tweet|
tweet_text = "#{tweet.user.screen_name}: #{tweet.text}\n"
Curses.addstr tweet_text
if @updated_time <= DateTime.parse(tweet.created_at)
unless @mute_say
`say "#{tweet_text}"`
end
end
end
@updated_time = DateTime.parse(Time.now.to_s)
end
def show_user_tweets
destroy_refresher_thread
Curses.clear
Curses.addstr "\n"
Curses.addstr "Type the screen name for user or press enter a regex to find user? "
screen_name = Curses.getstr
@twitter.user_timeline(:screen_name => screen_name).each do |tweet|
tweet_text = "#{tweet.user.screen_name}: #{tweet.text}\n"
Curses.addstr tweet_text
end
Curses.getch
create_refresher_thread
end
def new_tweet
destroy_refresher_thread
Curses.deleteln
Curses.addstr "\n"
Curses.addstr "Type a tweet or an empty line to cancel: "
new_tweet = Curses.getstr
urls = Regexp.compile('(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]', Regexp::IGNORECASE|Regexp::EXTENDED).match(new_tweet)
unless urls.nil?
shorturl = open("http://bit.ly/api?url=#{urls[0]}").read
new_tweet = new_tweet.gsub(urls[0], shorturl)
end
unless new_tweet.eql? ''
@twitter.update(new_tweet)
end
create_refresher_thread
end
def mute_say
@mute_say = !@mute_say
end
begin
@config = YAML::load_file(ConfigFile)
@twitter = Twitter::Base.new(Twitter::HTTPAuth.new(@config["username"], @config["password"]))
@updated_time = DateTime.parse(Time.now.to_s)
@mute_say = false
Curses.init_screen
Curses.start_color
Curses.setpos(0, 0)
create_refresher_thread
loop do
c = Curses.getch
case c
when ?\e
restart_refresher_thread
when ?U
show_user_tweets
when ?T
new_tweet
when ?M
mute_say
when ?Q
Curses.clear
exit
end
end
Curses.clear
end