-
Notifications
You must be signed in to change notification settings - Fork 1
/
tw_optparse.py
executable file
·58 lines (46 loc) · 1.59 KB
/
tw_optparse.py
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
#!/usr/bin/env python
from optparse import OptionParser
import os
import twitterlib
# Process command line using OptionParser.
def parse_commandline(args=None):
"Process command line using optparse"
parser = OptionParser()
parser.add_option("--api-key",
default=os.environ.get('API_KEY', None))
parser.add_option("--api-secret",
default=os.environ.get('API_SECRET', None))
parser.add_option("--access-token",
default=os.environ.get('ACCESS_TOKEN', None))
parser.add_option("--access-secret",
default=os.environ.get('ACCESS_SECRET', None))
return parser.parse_args(args)
# Sub-commands for select Twitter feeds.
def timeline(api):
"Display recent tweets from users timeline"
for status in api.timeline:
print u"%s: %s" % (status.user.screen_name, status.text)
def mentions(api):
"Display recent tweets mentioning user"
for status in api.mentions:
print u"%s: %s" % (status.user.screen_name, status.text)
def retweets(api):
"Display recent retweets from user's timeline"
for status in api.retweets:
print u"%s: %s" % (status.user.screen_name, status.text)
# Maps sub-command names to function calls.
SUB_COMMANDS = {
'timeline': timeline,
'mentions': mentions,
'retweets': retweets
}
# Command line execution.
if __name__ == '__main__':
opts, args = parse_commandline()
api = twitterlib.API(
opts.api_key,
opts.api_secret,
opts.access_token,
opts.access_secret
)
SUB_COMMANDS[args[0]](api)