Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # snakefood.py - Python command-line twitter client
- import sys
- import twitter
- username = ''
- password = ''
- # If we have two arguments (3 is because the command itself is in argv), then
- # identify on startup
- if len(sys.argv) == 3:
- username = sys.argv[1]
- password = sys.argv[2]
- # Log in to Twitter
- api = twitter.Api(username=username, password=password)
- # Main loop, here we go!
- while True:
- # Give the user a command prompt
- command = raw_input('SnakeFood> ')
- # If the command is exit, exit with success
- if command == 'exit':
- exit(0)
- # If the command is help, give the user a list of commands and their
- # subcommands.
- elif command == 'help':
- print '''About:
- SnakeFood is a Twitter client written in Python. The name comes from the fact
- that birds are often snake food.
- Commands:
- exit: Quit SnakeFood
- help: Print this message
- login: Gives a login> prompt at which you may input a username and password,
- space separated.
- logout: Ends an authenticated session.
- status*: Gives a status> prompt at which you may input a new status
- timeline: Gives a timeline> prompt at which you may type one of the following
- commands:
- friends*: Print a timeline of the most recent tweets of you and all the
- users you're following
- public: Print a timeline of the most recent tweets
- replies*: Print a timeline of the most recent tweets with @yourusername in
- them.
- user: Print a timeline of a user's most recent tweets, no user for the
- authenticated user.
- *requires authentication'''
- # If the command is login, give the user a login prompt
- elif command == 'login':
- cred = raw_input('SnakeFood> login> ')
- try:
- username, password = cred.split(' ')
- except ValueError, e:
- print 'Wrong number of values'
- api.SetCredentials(username=username, password=password)
- # If the command is logout, log the user out.
- elif command == 'logout':
- api.ClearCredentials()
- username = ''
- password = ''
- # If the command is status, give the user a status prompt
- elif command == 'status':
- # Be sure we're identified before we give a status prompt
- if username and password:
- statusmessage = raw_input('SnakeFood> status> ')
- # Be sure the status is a valid length
- if len(statusmessage) <= 140:
- api.PostUpdate(statusmessage)
- # If not, tell the user that it didn't work
- else:
- print 'Status messages must be 140 characters or fewer.'
- else:
- print 'You must be identified to set a status'
- continue
- # If the command is timeline, give the user a timeline prompt
- elif command == 'timeline':
- tltype = raw_input('SnakeFood> timeline> ')
- # Give the user the timeline he asked for
- if tltype == 'friends' and (username and password):
- timeline = api.GetFriendsTimeline()
- elif tltype == 'public':
- timeline = api.GetPublicTimeline()
- elif tltype == 'replies' and (username and password):
- timeline = api.GetReplies()
- # If it's a user timeline, ask what user
- elif tltype == 'user':
- tluser = raw_input('SnakeFood> timeline> user> ') or ''
- if tluser == '' and not (username and password):
- print 'A username is required if you are not authenticated.'
- continue
- timeline = api.GetUserTimeline(user=tluser)
- else:
- print 'Invalid timeline'
- continue
- # We want the most recent at the bottom and the oldest at the top
- timeline.reverse()
- # Print the user and status
- for status in timeline:
- print '\033[36m\033[1m%s (%s)\033[0m'%(status.GetUser().GetName(),
- status.GetUser().GetScreenName())
- print status.GetText().encode('utf-8')
- print
- # Wow, we fell through all the elif statements!
- else:
- print 'Invalid command'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement