How To Make A Twitter App With Python & PumpkinDB

Python3 Icon

If you’re new to programming, you might be stuck on what to do with your new knowledge. Or maybe you aren’t new, and you just want a cool project to work on.

So, let’s build a console Twitter app.

Getting Started

First, we’re going to need to download Pumpkin Database (PumpkinDB) from GitHub. You can use git to install it in your terminal. Then go ahead and navigate into the pumpkin-database directory.

You can also download PumpkinDB Here if you don’t have git installed.

$ git clone https://github.com/CodeShady/pumpkin-database
$ cd pumpkin-database

Next we need to create a database with Pumpkin. So in your terminal, type python3 to get into the python3 console.

>>> from pumpkindb import PumpkinDB
>>> database = PumpkinDB()
>>> dbPassword = "SecureDatabasePassword"
>>> database.create("TwitterApp", dbPassword)

Make sure to remember your database password!

PumpkinDB was designed not to be confusing, and super easy to set up. So if you are confused, visit this link for a detailed documentation on Pumpkin Database.

Now that our database is set up. Let’s get coding!

First, we need to make a home feed section. This is where our script will grab all the feed from the database.

# Home "page"
def home():
	print("---------- Your Feed ----------")
	
	allTweets = database.fetch("tweets", "value")

	for tweet in allTweets:
		print(tweet)
		print("-------------------------------")

Next we need to be able to write tweets and add them to the database. Let’s create another function for this.

# New Tweet Option
def writeTweet():
	name = input("Name: ")
	tweet = input("Tweet: ")

	# Format tweet into a nice view.
	formattedTweet = f"{name} tweeted:\n{tweet}"

	# Add this data to the "tweets" table in the database
	database.add("tweets", formattedTweet)

Now, let’s finish it up by asking the user for input on what they want to do! At the bottom of the script, let’s add this code block.

# Main Loop
while True:
	# Display Home page
	home()

	# Ask user what they want to do
	print("1) Tweet Something")
	print("2) Refresh Your Feed")
	choice = input("> ")

	# New Tweet
	if choice == "1":
		writeTweet()

	# Refresh Home Page
	elif choice == "2":
		home()

	else:
		print("That's not a valid option!")

And we’re finished!

Just like that, we’re finished! That’s how simple and easy to use Pumpkin Database is! Of course this is a pretty basic script, you could always add more! Here are some ideas on cool ways to improve this script.

  • Use Sockets to use this app with anyone on the same WiFi network as you.
  • Make more options for the user! What if the user want’s to delete their tweet? Take a look here for info on deleting items from Pumpkin Database.
  • Tweet editing! What if the user doesn’t like their tweet? Add a feature to let them!

Here is the full source code in case you missed something!

from pumpkindb import PumpkinDB

# Initalize Pumpkin Database
database = PumpkinDB()

# Create Password
dbPassword = "dbPassword"

# Use the TwitterApp database
database.use("TwitterApp", dbPassword)

# Home "page"
def home():
	print("---------- Your Feed ----------")
	
	allTweets = database.fetch("tweets", "value")

	for tweet in allTweets:
		print(tweet)
		print("-------------------------------")

# New Tweet Option
def writeTweet():
	name = input("Name: ")
	tweet = input("Tweet: ")

	# Format tweet into a nice view.
	formattedTweet = f"{name} tweeted:\n{tweet}"

	# Add this data to the "tweets" table in the database
	database.add("tweets", formattedTweet)

	
# Main Loop
while True:
	# Display Home page
	home()

	# Ask user what they want to do
	print("1) Tweet Something")
	print("2) Refresh Your Feed")
	choice = input("> ")

	# New Tweet
	if choice == "1":
		writeTweet()

	# Refresh Home Page
	elif choice == "2":
		home()

	else:
		print("That's not a valid option!")

4 thoughts on “How To Make A Twitter App With Python & PumpkinDB”

  1. Great post with very straightforward and easy to follow steps! Can you write a post about database security, such as input checking, next?

Leave a Reply

Your email address will not be published. Required fields are marked *