-
Notifications
You must be signed in to change notification settings - Fork 1
/
tw_getopt.py
executable file
·69 lines (57 loc) · 1.8 KB
/
tw_getopt.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
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import getopt
import sys
import os
import twitterlib
# Maps command line flags to environment variables.
ENV_VARS = {
'--api-key': 'API_KEY',
'--api-secret': 'API_SECRET',
'--access-token': 'ACCESS_TOKEN',
'--access-secret': 'ACCESS_SECRET'
}
# Process command line using getopt module.
def parse_commandline(args=None):
"Process command line using getopt"
args = sys.argv[1:] if args is None else args
optlist, args = getopt.gnu_getopt(sys.argv[1:], 'h', [
'help', 'api-key=', 'api-secret=', 'access-token=', 'access-secret='
])
opts = dict(optlist)
if '-h' in opts or '--help' in opts:
print "Minimal Twitter client Demonstrate the use of getopt"
sys.exit(0)
for flag in ENV_VARS:
if flag in opts:
continue
opts[flag] = os.environ[ENV_VARS[flag]]
return opts, 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)