Ronnie Atuhaire
Ronnie Atuhaire's Blog 🤓

Ronnie Atuhaire's Blog 🤓

Bitcoin Price Desktop Notifier With Python

Bitcoin Price Desktop Notifier With Python

Ronnie Atuhaire's photo
Ronnie Atuhaire
·Mar 13, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • 🔸 Intro
  • 🔸 Code
  • 🔸 Conclusion

Hey 👋, welcome to my little world again. Are you a crypto trader who constantly have to check on the internet or some web applications for the current crypto prices (BTC)?

This process is tedious especially if you are a busy person with other things to do. Today, we are going to build a simple Windows Desktop Notifier that alerts us of the current bitcoin price and your problem will be solved.

image.png

🔸 Intro

I am using Coin Desk API to retrieve the price but you could certainly get the API of your choice like Coingecko, Crypto.Com or any other available for free.

We shall be using the requests library to connect to the API and that means you will need constant internet for this app to wor

We shall also use plyer that interacts well with Windows Notifications, it is used to access the features of the hardware.

If you don't have these, please go ahead and install them first;

pip install requests
pip install plyer

🔸 Code

So let's start by importing the necessary modules. We shall be using the datetime module to get the current date and also use the time module to allow our notifier to sleep.

import requests
import time
from plyer import notification
import datetime

Now let's create necessary variables for our notifier

bitcoin_rate = None

now = datetime.datetime.now()
now = now.strftime("%d/%m %H:%M:")

URL = "https://api.coindesk.com/v1/bpi/currentprice.json"

We initialise bitcoin to None in the beginning because we currently don't know its price which will be updated by our API call of the URL variable.

We also get the current date & time using datetime

Now let's get our price;

try:
    response = requests.get(URL,
      headers={"Accept": "application/json"},
    )
    data = response.json()
    bpi = data['bpi']
    USD = bpi['USD']
    bitcoin_rate = int(USD['rate_float'])

    print("[+] We are live [+]")

except:
    print('Something is wrong, Do you have Internet!?')

💨 We used a try statement in order to handle exceptions in case there is no internet and also prevents our app from just breaking.

💨 We accept headers in our get call that will return JSON format and return the response.
💨 We then query the particular ey that has our price which is Bitcoin Price Index bpi
💨 We then read the USD value.

Now let's build our desktop notifier and let's wrap that code above in the while loop in order to get live updates.

So our while loop code looks like this now

while True:
    try:
        response = requests.get(URL,
          headers={"Accept": "application/json"},
        )
        data = response.json()
        bpi = data['bpi']
        USD = bpi['USD']
        bitcoin_rate = int(USD['rate_float'])

        print("[+] We are live [+]")

    except:
        print('Something is wrong, Do you have Internet!')


    notification.notify(
            #title of the notification,
            title = f"Bitcoin Price Alert!! {now}",

            #the body of the notification
            message = f"Current Bitcoin Price is {bitcoin_rate}",

            #creating icon for the notification
            #we need to download a icon of ico file format
            app_icon = "bitcoin.ico",

            # the notification stays for 60 seconds
            timeout  = 60
        )

        #notification repeats after every 5 Minutes
    time.sleep(30)

💨 We wrap it in a while loop that continuously monitors the current price and check if the bitcoin rate is updated and that is the condition that keeps it going.

Note: The icon should be in the same folder with this code.

💨 Mine is named bitcoin.ico

The rest of the explanation can be got from the code comments. Now run your script!
I named mine bitcoin_notifier.py

py bitcoin_notifier.py

You can find all the files and code on this repo.

What if we want to have it running in the background automatically without running it every time!
Just type this in the terminal

pythonw.exe .\bitcoin_notifier.py
image.png

To see if this is a success, view the background processes in the task manager

image.png

That's it, you can always kill the process to stop getting notifications.

🔸 Conclusion

Once again, hope you learned something today from my little closet.

Please consider subscribing or following me for related content, especially about Tech, Python & General Programming.

You can show extra love by buying me a coffee to support this free content and I am also open to partnerships, technical writing roles, collaborations and Python-related training or roles.

Buy Ronnie A Coffee 📢 You can also follow me on Twitter : ♥ ♥ Waiting for you! 🙂
 
Share this