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:
TODOs:
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: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
follow
(auth_secret, followee_username)¶Follow a user.
Parameters: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
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: |
|
Note
Possible error strings are listed as below:
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: |
|
Note
Possible error strings are listed as below:
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: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
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: |
|
Note
Possible error strings are listed as below:
get_user_tweets
(auth_secret, username, max_cnt_tweets)¶Get the tweets posted by one user.
Parameters: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
login
(username, password)¶Log into a user.
Parameters: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
logout
(auth_secret)¶Log out of a user.
Parameters: | auth_secret (str) – The authentication secret of the logged-in user. |
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
post_tweet
(auth_secret, tweet)¶Post a tweet.
Parameters: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
register
(username, password)¶Register a new user.
Parameters: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
unfollow
(auth_secret, followee_username)¶Unfollow a user.
Parameters: |
|
---|---|
Returns: |
|
Note
Possible error strings are listed as below:
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:
127.0.0.1:6379> register {username} {password}
127.0.0.1:6379> login {username} {password}
127.0.0.1:6379> logout
127.0.0.1:6379> changepwd {old_password} {new_password} {confirmed_new_password}
127.0.0.1:6379> userprofile
127.0.0.1:6379> post {tweet}
127.0.0.1:6379> follow {followee_username}
127.0.0.1:6379> unfollow {followee_username}
127.0.0.1:6379> followers
127.0.0.1:6379> followings
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.
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.
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: |
|
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: |
|
---|
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. |
This module defines all the constants used by pytwis_clt.py.
This module defines all the constants used by pytwis.py.
Define the package.