]>
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 set set your twitter status
14 -e --email <email> your email to login to twitter
15 -p --password <password> your twitter password
19 from getopt
import getopt
21 from api
import Twitter
, TwitterError
32 def parse_args(args
, options
):
33 long_opts
= ['email', 'password', 'help']
35 opts
, extra_args
= getopt(args
, short_opts
, long_opts
)
38 if opt
in ('-e', '--email'):
39 options
['email'] = arg
40 elif opt
in ('-p', '--password'):
41 options
['password'] = arg
42 elif opt
in ('-?', '-h', '--help'):
47 options
['action'] = extra_args
[0]
48 options
['extra_args'] = extra_args
[1:]
50 class StatusFormatter(object):
51 def __call__(self
, status
):
53 status
['user']['screen_name'], status
['text'])).encode(
54 sys
.stdout
.encoding
, 'replace')
56 def no_action(twitter
, options
):
57 print >> sys
.stderr
, "No such action: ", options
['action']
60 def action_friends(twitter
, options
):
61 statuses
= reversed(twitter
.statuses
.friends_timeline())
62 sf
= StatusFormatter()
63 for status
in statuses
:
66 def action_public(twitter
, options
):
67 statuses
= reversed(twitter
.statuses
.public_timeline())
68 sf
= StatusFormatter()
69 for status
in statuses
:
72 def action_set_status(twitter
, options
):
73 twitter
.statuses
.update(
74 status
=(u
" ".join(options
['extra_args'])).encode(
78 'friends': action_friends
,
79 'public': action_public
,
80 'set': action_set_status
,
85 return main_with_args(sys
.argv
[1:])
87 def main_with_args(args
):
88 parse_args(args
, options
)
89 twitter
= Twitter(options
['email'], options
['password'])
90 action
= actions
.get(options
['action'], no_action
)
92 action(twitter
, options
)
93 except TwitterError
, e
:
94 print >> sys
.stderr
, e
.message
95 print >> sys
.stderr
, "Use 'twitter -h' for help."