]>
jfr.im git - z_archive/twitter.git/blob - twitter/cmdline.py
4 twitter [action] [options]
8 friends get latest tweets from your friends (default action)
9 public get latest public tweets
10 replies get latest replies
11 set set your twitter status
15 -e --email <email> your email to login to twitter
16 -p --password <password> your twitter password
17 -r --refresh run this command forever, polling every once
18 in a while (default: every 5 minutes)
19 -R --refresh-rate <rate> set the refresh rate (in seconds)
20 -f --format <format> specify the output format for status updates
21 -c --config <filename> read username and password from given config
22 file (default ~/.twitter)
24 FORMATS for the --format option
26 default one line per status
27 verbose multiple lines per status, more verbose status info
28 urls nothing but URLs. Dare you click them?
32 The config file should contain your email and password like so:
41 from getopt
import getopt
42 from getpass
import getpass
45 from ConfigParser
import SafeConfigParser
47 from api
import Twitter
, TwitterError
56 'config_filename': os
.environ
.get('HOME', '') + os
.sep
+ '.twitter',
60 def parse_args(args
, options
):
61 long_opts
= ['email', 'password', 'help', 'format', 'refresh',
62 'refresh-rate', 'config']
63 short_opts
= "e:p:f:h?rR:c:"
64 opts
, extra_args
= getopt(args
, short_opts
, long_opts
)
67 if opt
in ('-e', '--email'):
68 options
['email'] = arg
69 elif opt
in ('-p', '--password'):
70 options
['password'] = arg
71 elif opt
in ('-f', '--format'):
72 options
['format'] = arg
73 elif opt
in ('-r', '--refresh'):
74 options
['refresh'] = True
75 elif opt
in ('-R', '--refresh-rate'):
76 options
['refresh_rate'] = int(arg
)
77 elif opt
in ('-?', '-h', '--help'):
80 elif opt
in ('-c', '--config'):
81 options
['config_filename'] = arg
84 options
['action'] = extra_args
[0]
85 options
['extra_args'] = extra_args
[1:]
87 class StatusFormatter(object):
88 def __call__(self
, status
):
90 status
['user']['screen_name'], status
['text']))
92 class VerboseStatusFormatter(object):
93 def __call__(self
, status
):
94 return (u
"-- %s (%s) on %s\n%s\n" %(
95 status
['user']['screen_name'],
96 status
['user']['location'],
100 class URLStatusFormatter(object):
101 urlmatch
= re
.compile(r
'https?://\S+')
102 def __call__(self
, status
):
103 urls
= self
.urlmatch
.findall(status
['text'])
104 return u
'\n'.join(urls
) if urls
else ""
107 'default': StatusFormatter
,
108 'verbose': VerboseStatusFormatter
,
109 'urls': URLStatusFormatter
112 def get_status_formatter(options
):
113 sf
= formatters
.get(options
['format'])
116 "Unknown formatter '%s'" %(options
['format']))
119 class Action(object):
122 class NoSuchAction(Action
):
123 def __call__(self
, twitter
, options
):
124 print >> sys
.stderr
, "No such action: ", options
['action']
127 class StatusAction(Action
):
128 def __call__(self
, twitter
, options
):
129 statuses
= self
.getStatuses(twitter
)
130 sf
= get_status_formatter(options
)
131 for status
in statuses
:
132 statusStr
= sf(status
)
133 if statusStr
.strip():
134 print statusStr
.encode(sys
.stdout
.encoding
, 'replace')
136 class FriendsAction(StatusAction
):
137 def getStatuses(self
, twitter
):
138 return reversed(twitter
.statuses
.friends_timeline())
140 class PublicAction(StatusAction
):
141 def getStatuses(self
, twitter
):
142 return reversed(twitter
.statuses
.public_timeline())
144 class RepliesAction(StatusAction
):
145 def getStatuses(self
, twitter
):
146 return reversed(twitter
.statuses
.replies())
148 class SetStatusAction(Action
):
149 def __call__(self
, twitter
, options
):
150 statusTxt
= (u
" ".join(options
['extra_args'])
151 if options
['extra_args']
152 else unicode(raw_input("message: ")))
153 status
= (statusTxt
.encode('utf8', 'replace'))
154 twitter
.statuses
.update(status
=status
)
157 'friends': FriendsAction
,
158 'public': PublicAction
,
159 'replies': RepliesAction
,
160 'set': SetStatusAction
,
163 def loadConfig(filename
):
166 if os
.path
.exists(filename
):
167 cp
= SafeConfigParser()
169 email
= cp
.get('twitter', 'email', None)
170 password
= cp
.get('twitter', 'password', None)
171 return email
, password
174 return main_with_args(sys
.argv
[1:])
176 def main_with_args(args
):
177 parse_args(args
, options
)
179 email
, password
= loadConfig(options
['config_filename'])
180 if not options
['email']: options
['email'] = email
181 if not options
['password']: options
['password'] = password
183 if options
['refresh'] and options
['action'] == 'set':
184 print >> sys
.stderr
, "You can't repeatedly set your status, silly"
185 print >> sys
.stderr
, "Use 'twitter -h' for help."
187 if options
['email'] and not options
['password']:
188 options
['password'] = getpass("Twitter password: ")
189 twitter
= Twitter(options
['email'], options
['password'])
190 action
= actions
.get(options
['action'], NoSuchAction
)()
192 doAction
= lambda : action(twitter
, options
)
193 if (options
['refresh']):
196 time
.sleep(options
['refresh_rate'])
199 except TwitterError
, e
:
200 print >> sys
.stderr
, e
.args
[0]
201 print >> sys
.stderr
, "Use 'twitter -h' for help."
203 except KeyboardInterrupt: