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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
| #!/usr/bin/env python
# encoding: utf-8
# -*- coding: utf-8 -*-
############################################################################
# --- cmd-twitt 2009.08.16 --- #
# #
# Copyright (C) 2009 by Andreu Correa Casablanca (Original Author) #
# #
# Email: castarco@gmail.com (Andreu Correa Casablanca) #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################
import ConfigParser
import getpass
import gettext
import locale
import optparse
import os
import sys
import twitter
import urllib2
# An alias that allows us to imitate the touch command
touch = lambda file: open(file, "w").close()
cmd_usage = 'usage: %prog [option] [num_of_twits_per_timeline]'
cmd_version = '2009.08.16'
conf_dir = os.environ["HOME"] + "/.cmdtwitt/"
conf_file = conf_dir + "user.conf"
encoding = locale.getpreferredencoding().lower()
user = ''
pswd = ''
color = {}
color['green'] = '\33[1;32m'
color['nocolor'] = '\33[1;0m'
color['red'] = '\33[1;31m'
color['white'] = '\33[1;37m'
color['yellow'] = '\33[1;33m'
# Translate some global vars
def init_vars():
global cmd_usage
cmd_usage = _(cmd_usage)
# Load the account settings
def load_config():
global user, pswd
cfg = ConfigParser.ConfigParser()
try:
cfg.readfp(file(conf_file))
except:
raise
user = cfg.get ('TWIT_ACCOUNT', 'User')
pswd = cfg.get ('TWIT_ACCOUNT', 'Pswd')
#Set the account settings
def set_config():
global conf_dir, conf_file
print _("Settings:")
cfg = ConfigParser.ConfigParser()
cfg.add_section('TWIT_ACCOUNT')
cfg.set('TWIT_ACCOUNT', 'User', raw_input(_('tUser: ')) )
pswd_not_set = True
while pswd_not_set:
pswd1 = getpass.getpass(_('tPassword : '))
pswd2 = getpass.getpass(_('tRepeat password: '))
if pswd1 == pswd2:
pswd_not_set = False
else:
print _('The passwords don't match. Please try to write it again.')
cfg.set('TWIT_ACCOUNT', 'Pswd', pswd1)
if os.access(conf_file, os.F_OK) == False:
try:
touch(conf_file)
except IOError:
try:
os.mkdir(conf_dir, 0700)
except IOError:
print _('It was impossible to create the config dir.')
raise
try:
touch(conf_file)
except:
print _('It was impossible to create the config file.')
raise
try:
cfg.write(file(conf_file, 'w'))
except:
raise
# Manage connection errors
def manage_connection_error(e, code):
if e.getcode() == code:
print _('tUnauthorized acces, you should set a correct username and password.')
else:
print _('It seems twitter is down. Try it later.')
# Sends a message to a user
def send_msg_to_user(_user):
global user, pswd, cmd_version
api = twitter.Api(username=user, password=pswd)
msg = unicode(raw_input( _('Type your message:nt') ), encoding)[:140]
try:
api.PostDirectMessage(_user, msg)
except urllib2.HTTPError, e:
print _('Error sending the message:')
manage_connection_error(e, 401)
# Show the user status
def show_status():
global user, pswd
api = twitter.Api(username=user, password=pswd)
try:
status = api.GetUserTimeline(user)[0].text
print _('Your status is:nt' + status)
except urllib2.HTTPError, e:
print _('Error reading your status:')
manage_connection_error(e, 404)
# Format the timeline to show in the console
def show_timeline(num_msgs, statuses):
global color
_status = statuses[0]
print color['green']+_status.user.name + ' :n' + color['yellow'] + _status.GetRelativeCreatedAt() + ' > ' + color['nocolor'] + _status.text + 'n'
for status in statuses[1:num_msgs]:
if status.user.name == _status.user.name:
name = color['yellow'] + status.GetRelativeCreatedAt()+' > ' + color['nocolor']
else:
name = color['green']+status.user.name + ' :n' + color['yellow'] + status.GetRelativeCreatedAt() + ' > ' + color['nocolor']
print name + status.text + 'n'
_status = status
# Shows the timeline of a specific twitter user
def show_usertimeline(_user, num_msgs):
global user, pswd, color
api = twitter.Api(username=user, password=pswd)
try:
statuses = api.GetUserTimeline(_user)
print color['red'] + _user + _(' Timeline:n') + color['nocolor']
show_timeline(num_msgs, statuses)
except urllib2.HTTPError, e:
print _('Error reading the timeline:')
manage_connection_error(e, 401)
# Shows the Home timeline
def show_friendstimeline(num_msgs):
global user, pswd, color
api = twitter.Api(username=user, password=pswd)
try:
statuses = api.GetFriendsTimeline(user)
print color['red'] +_('Home Timeline:n') + color['nocolor']
show_timeline(num_msgs, statuses)
except urllib2.HTTPError, e:
print _('Error reading the timeline:')
manage_connection_error(e, 401)
# Shows the Home timeline
def show_publictimeline(num_msgs):
global user, pswd
api = twitter.Api(username=user, password=pswd)
try:
statuses = api.GetPublicTimeline()
print color['red'] + _('Public Timeline:n') + color['nocolor']
show_timeline(num_msgs, statuses)
except urllib2.HTTPError, e:
print _('Error reading the timeline:')
manage_connection_error(e, 401)
# Send a message to twitter
def send_msg(_user):
global user, pswd, cmd_version
api = twitter.Api(username=user, password=pswd)
try:
if _user != '':
_user = '@'+_user+' '
api.PostUpdate( ( _user + unicode(raw_input( _('Write your message and press Enter:nt') ), encoding) )[:140] )
except urllib2.HTTPError, e:
print _('Error sending the message:')
manage_connection_error(e, 401)
# The main program
def main(argv=None):
global user, pswd
if argv == None:
argv = sys.argv
# Internationalization
gettext.install('cmd-twitt')
init_vars()
cmd_parser = optparse.OptionParser(usage=cmd_usage, version=cmd_version, conflict_handler='resolve')
cmd_parser.add_option('-h', '--help', action='help', help=_('print this help text and exit'))
cmd_parser.add_option('-v', '--version', action='version', help=_('print program version and exit'))
cmd_parser.add_option('-c', '--config', dest='config', action='store_true', help=_('Configure your twitter account'))
cmd_parser.add_option('-s', '--status', dest='status', action='store_true', help=_('Shows your status'))
cmd_parser.add_option('-p', '--publictimeline', dest='publictimeline', action='store_true', help=_('Shows the public timeline'))
cmd_parser.add_option('-u', '--usertimeline', dest='usertimeline', action='store_true', help=_('Shows the user timeline'))
cmd_parser.add_option('-F', '--friendtimeline', dest='friendtimeline', metavar='FRIEND', help=_('Shows the timeline of a friend'))
cmd_parser.add_option('-f', '--friendstimeline', dest='friendstimeline', action='store_true', help=_('Shows the friends timeline'))
cmd_parser.add_option('-m', '--private-message', dest='private_msg', metavar='USER', help=_('Sends a private message to a twitter user'))
cmd_parser.add_option('-M', '--public-message', dest='public_msg', metavar='USER', help=_('Sends a public message to a twitter user'))
(cmd_opts, cmd_args) = cmd_parser.parse_args()
if cmd_opts.config:
try:
set_config()
except:
print _("It was impossible to set the settings.")
return 1
else:
try:
load_config()
except:
print _('There is not a config file, you should set your user and password in the settings option.n')
cmd_parser.print_help()
return 1
try:
num_msgs = int(cmd_args[0])
except:
num_msgs = 10
if cmd_opts.private_msg:
send_msg_to_user(cmd_opts.private_msg)
elif cmd_opts.public_msg:
send_msg(cmd_opts.public_msg)
elif cmd_opts.status:
show_status()
elif cmd_opts.usertimeline:
show_usertimeline(user, num_msgs)
elif cmd_opts.friendtimeline:
show_usertimeline(cmd_opts.friendtimeline, num_msgs)
elif cmd_opts.friendstimeline:
show_friendstimeline(num_msgs)
elif cmd_opts.publictimeline:
show_publictimeline (num_msgs)
else:
send_msg('')
if __name__ == "__main__":
sys.exit(main()) |