Advertisement
Guest User

Untitled

a guest
May 9th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # snakefood.py - Python command-line twitter client
  3.  
  4. import sys
  5. import twitter
  6.  
  7. username = ''
  8. password = ''
  9.  
  10. # If we have two arguments (3 is because the command itself is in argv), then
  11. # identify on startup
  12. if len(sys.argv) == 3:
  13.     username = sys.argv[1]
  14.     password = sys.argv[2]
  15.  
  16. # Log in to Twitter
  17. api = twitter.Api(username=username, password=password)
  18.  
  19. # Main loop, here we go!
  20. while True:
  21.     # Give the user a command prompt
  22.     command = raw_input('SnakeFood> ')
  23.     # If the command is exit, exit with success
  24.     if command == 'exit':
  25.         exit(0)
  26.     # If the command is help, give the user a list of commands and their
  27.     # subcommands.
  28.     elif command == 'help':
  29.         print '''About:
  30. SnakeFood is a Twitter client written in Python.  The name comes from the fact
  31. that birds are often snake food.
  32.  
  33. Commands:
  34. exit:     Quit SnakeFood
  35. help:     Print this message
  36. login:    Gives a login> prompt at which you may input a username and password,
  37.          space separated.
  38. logout:   Ends an authenticated session.
  39. status*:  Gives a status> prompt at which you may input a new status
  40. timeline: Gives a timeline> prompt at which you may type one of the following
  41.          commands:
  42.    friends*: Print a timeline of the most recent tweets of you and all the
  43.              users you're following
  44.    public:   Print a timeline of the most recent tweets
  45.    replies*: Print a timeline of the most recent tweets with @yourusername in
  46.              them.
  47.    user:     Print a timeline of a user's most recent tweets, no user for the
  48.              authenticated user.
  49.  
  50. *requires authentication'''
  51.     # If the command is login, give the user a login prompt
  52.     elif command == 'login':
  53.         cred = raw_input('SnakeFood> login> ')
  54.         try:
  55.             username, password = cred.split(' ')
  56.         except ValueError, e:
  57.             print 'Wrong number of values'
  58.         api.SetCredentials(username=username, password=password)
  59.     # If the command is logout, log the user out.
  60.     elif command == 'logout':
  61.         api.ClearCredentials()
  62.         username = ''
  63.         password = ''
  64.     # If the command is status, give the user a status prompt
  65.     elif command == 'status':
  66.         # Be sure we're identified before we give a status prompt
  67.         if username and password:
  68.             statusmessage = raw_input('SnakeFood> status> ')
  69.             # Be sure the status is a valid length
  70.             if len(statusmessage) <= 140:
  71.                 api.PostUpdate(statusmessage)
  72.             # If not, tell the user that it didn't work
  73.             else:
  74.                 print 'Status messages must be 140 characters or fewer.'
  75.         else:
  76.             print 'You must be identified to set a status'
  77.             continue
  78.     # If the command is timeline, give the user a timeline prompt
  79.     elif command == 'timeline':
  80.         tltype = raw_input('SnakeFood> timeline> ')
  81.         # Give the user the timeline he asked for
  82.         if tltype == 'friends' and (username and password):
  83.             timeline = api.GetFriendsTimeline()
  84.         elif tltype == 'public':
  85.             timeline = api.GetPublicTimeline()
  86.         elif tltype == 'replies' and (username and password):
  87.             timeline = api.GetReplies()
  88.         # If it's a user timeline, ask what user
  89.         elif tltype == 'user':
  90.             tluser = raw_input('SnakeFood> timeline> user> ') or ''
  91.             if tluser == '' and not (username and password):
  92.                 print 'A username is required if you are not authenticated.'
  93.                 continue
  94.             timeline = api.GetUserTimeline(user=tluser)
  95.         else:
  96.             print 'Invalid timeline'
  97.             continue
  98.         # We want the most recent at the bottom and the oldest at the top
  99.         timeline.reverse()
  100.         # Print the user and status
  101.         for status in timeline:
  102.             print '\033[36m\033[1m%s (%s)\033[0m'%(status.GetUser().GetName(),
  103.                 status.GetUser().GetScreenName())
  104.             print status.GetText().encode('utf-8')
  105.             print
  106.     # Wow, we fell through all the elif statements!
  107.     else:
  108.         print 'Invalid command'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement