pytwis package

Submodules

pytwis.pytwis module

pytwis – A Twitter-toy-clone backend using Python and Redis.

This module implements the backend for a simplified Twitter clone based on Redis. We follow the Redis tutorial (https://redis.io/topics/twitter-clone) to design the data layout of the Redis database.

It supports the following features:

  • Register new users
  • Log in/out
  • Change user password
  • Post tweets
  • Follower/Following
  • General timeline for anonymous user
  • User timeline
  • Get tweets posted by a user

TODOs:

  • Search users
  • Delete a user
  • Recover user password
  • #hashtags
  • @mentions
  • Retweets
  • Replies
  • Conversations
  • Edit/Delete tweets
  • And more
class pytwis.pytwis.Pytwis(hostname='127.0.0.1', port=6379, socket='', db=0, password='')

Bases: object

This class implements all the interfaces to the Redis database of the Twitter-toy-clone.

change_password(auth_secret, old_password, new_password)

Change the user password.

Parameters:
  • auth_secret (str) – The authentication secret which will be used for user authentication.
  • old_password (str) – The old password before the change.
  • new_password (str) – The new password after the change.
Returns:

  • bool – True if the password is successfully changed, False otherwise.
  • result – A dict containing the new authentication secret with the key AUTH_KEY if the password is successfully changed, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NEW_PASSWORD_NO_CHANGE
  • ERROR_NOT_LOGGED_IN
  • ERROR_INCORRECT_OLD_PASSWORD
  • ERROR_WEAK_PASSWORD
follow(auth_secret, followee_username)

Follow a user.

Parameters:
  • auth_secret (str) – The authentication secret of the logged-in user.
  • followee_username (str) – The username of the followee.
Returns:

  • bool – True if the follow is successful, False otherwise.
  • result – None if the follow is successful, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
  • ERROR_FOLLOWEE_NOT_EXIST_FORMAT.format(followee_username)
  • ERROR_FOLLOW_YOURSELF_FORMAT.format(followee_username)
get_followers(auth_secret)

Get the follower list of a logged-in user.

Parameters:auth_secret (str) – The authentication secret of the logged-in user.
Returns:
  • bool – True if the follower list is successfully obtained, False otherwise.
  • result – A dict containing the follower list with the key FOLLOWER_LIST_KEY if the follower list is successfully obtained, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
get_following(auth_secret)

Get the following list of a logged-in user.

Parameters:auth_secret (str) – The authentication secret of the logged-in user.
Returns:
  • bool – True if the following list is successfully obtained, False otherwise.
  • result – A dict containing the following list with the key FOLLOWING_LIST_KEY if the follower list is successfully obtained, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
get_timeline(auth_secret, max_cnt_tweets)

Get the general or user timeline.

If an empty authentication secret is given, this method returns the general timeline. If an authentication secret is given and it is valid, this method returns the user timeline. If an authentication secret is given but it is invalid, this method returns an error.

Parameters:
  • auth_secret (str) – Either the authentication secret of the logged-in user or an empty string.
  • max_cnt_tweets (int) – The maximum number of tweets included in the timeline. If it is set to -1, then all the available tweets will be included.
Returns:

  • bool – True if the timeline is successfully retrieved, False otherwise.
  • result – A dict containing a list of tweets with the key TWEETS_KEY if the timeline is successfully retrieved, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
get_user_profile(auth_secret)

Get the profile (i.e., username, password, etc.) of a user.

Parameters:auth_secret (str) – The authentication secret of the logged-in user.
Returns:
  • bool – True if the logout is successful, False otherwise.
  • result – A dict containing the following keys:
    • USERNAME_KEY
    • PASSWORD_HASH_KEY
    • AUTH_KEY

    if the user profile is obtained successfully; otherwise a dict containing the error string with the key ERROR_KEY.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
get_user_tweets(auth_secret, username, max_cnt_tweets)

Get the tweets posted by one user.

Parameters:
  • auth_secret (str) – The authentication secret of the logged-in user.
  • username – The name of the user who post the tweets and may not be the logged-in user.
  • max_cnt_tweets (int) – The maximum number of tweets included in the return. If it is set to -1, then all the tweets posted by the user will be included.
Returns:

  • bool – True if the tweets are successfully retrieved, False otherwise.
  • result – A dict containing a list of tweets with the key TWEETS_KEY if the tweets are successfully retrieved, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
  • ERROR_USERNAME_NOT_EXIST_FORMAT.format(username)
login(username, password)

Log into a user.

Parameters:
  • username (str) – The username.
  • password (str) – The password.
Returns:

  • bool – True if the login is successful, False otherwise.
  • result – A dict containing the authentication secret with the key AUTH_KEY if the login is successful, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_USERNAME_NOT_EXIST_FORMAT.format(username)
  • ERROR_INCORRECT_PASSWORD
logout(auth_secret)

Log out of a user.

Parameters:auth_secret (str) – The authentication secret of the logged-in user.
Returns:
  • bool – True if the logout is successful, False otherwise.
  • result – None if the logout is successful, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
post_tweet(auth_secret, tweet)

Post a tweet.

Parameters:
  • auth_secret (str) – The authentication secret of the logged-in user.
  • tweet (str) – The tweet that will be posted.
Returns:

  • bool – True if the tweet is successfully posted, False otherwise.
  • result – None if the tweet is successfully posted, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
register(username, password)

Register a new user.

Parameters:
  • username (str) – The username.
  • password (str) – The password.
Returns:

  • bool – True if the new user is successfully registered, False otherwise.
  • result – An empty dict if the new user is successfully registered, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_USERNAME_ALREADY_EXISTS.format(username)
  • ERROR_WEAK_PASSWORD
unfollow(auth_secret, followee_username)

Unfollow a user.

Parameters:
  • auth_secret (str) – The authentication secret of the logged-in user.
  • followee_username (str) – The username of the followee.
Returns:

  • bool – True if the unfollow is successful, False otherwise.
  • result – None if the unfollow is successful, a dict containing the error string with the key ERROR_KEY otherwise.

Note

Possible error strings are listed as below:

  • ERROR_NOT_LOGGED_IN
  • ERROR_FOLLOWEE_NOT_EXIST_FORMAT.format(followee_username)

pytwis.pytwis_clt module

A command-line tool which uses pytwis to interact with the Redis database of a Twitter toy clone.

To see the help information,

$ ./pytwis_clt.py -h
$ ./pytwis_clt.py --help

After launching pytwis_clt.py, you will be able to use the following commands:

  • Register a new user:
127.0.0.1:6379> register {username} {password}
  • Log into a user:
127.0.0.1:6379> login {username} {password}
  • Log out of a user:
127.0.0.1:6379> logout
  • Change the password:
127.0.0.1:6379> changepwd {old_password} {new_password} {confirmed_new_password}
  • Get the profile of the current user:
127.0.0.1:6379> userprofile
  • Post a tweet:
127.0.0.1:6379> post {tweet}
  • Follow a user:
127.0.0.1:6379> follow {followee_username}
  • Unfollow a user:
127.0.0.1:6379> unfollow {followee_username}
  • Get the follower list:
127.0.0.1:6379> followers
  • Get the following list:
127.0.0.1:6379> followings
  • Get the timeline:
127.0.0.1:6379> timeline
127.0.0.1:6379> timeline {max_tweet_count}

Note that if a user is logged in, timeline will return the user timeline; otherwise timeline will return the general timeline.

  • Get the tweets posted by a user:
127.0.0.1:6379> tweetsby
127.0.0.1:6379> tweetsby {username}
127.0.0.1:6379> tweetsby {username} {max_tweet_count}

Note that if no username is given, tweetsby will return the tweets posted by the currently logged-in user.

  • Exit the program:
127.0.0.1:6379> exit
127.0.0.1:6379> quit
pytwis.pytwis_clt.get_pytwis(epilog)

Connect to the Redis database and return the Pytwis instance.

Parameters:epilog (str) – An epilog string which will be displayed by ArgumentParser.
Returns:
  • pytwis (A Pytwis instance.)
  • prompt (str) – The prompt string which contains either the hostname and the port or the socket.
Raises:ValueError – If we fail to connect to the Redis server.
pytwis.pytwis_clt.print_tweets(tweets)

Print a list of tweets one by one separated by “=”s.

Parameters:tweets (list(dict)) – A list of tweets. Each tweet is a dict containing the username of the tweet’s author, the post time, and the tweet body.
pytwis.pytwis_clt.pytwis_clt()

The main routine of this command-line tool.

pytwis.pytwis_clt.pytwis_command_parser(raw_command)

Parse the command input.

Parameters:raw_command (str) – The raw command input, e.g., register xxxxxx yyyyyy.
Returns:arg_dict – The parsed command output. {‘command’:’register’, ‘username’: <username>, ‘password’: <password>} for register.
Return type:dict(str, str or int)
Raises:ValueError – If the raw command can’t be parsed correctly, e.g., it has an incorrect number of arguments or incorrect arguments.
pytwis.pytwis_clt.pytwis_command_processor(twis, auth_secret, args)

Process the parsed command.

Parameters:
  • twis (Pytwis) – A Pytwis instance which interacts with the Redis database of the Twitter toy clone.
  • auth_secret (str) – The authentication secret of a logged-in user.
  • args – The parsed command output by pytwis_command_parser().
pytwis.pytwis_clt.validate_command(raw_command)

Validate the command input.

Currently we only check the number of arguments according to the command type.

Parameters:raw_command (str) – The raw command input, e.g., register xxxxxx yyyyyy.
Raises:ValueError – If the raw command input doesn’t have the correct number of arguments.

pytwis.pytwis_clt_constants module

This module defines all the constants used by pytwis_clt.py.

pytwis.pytwis_constants module

This module defines all the constants used by pytwis.py.

Module contents

Define the package.