From Silicon Valley to Your Screen: Programming Gilfoyle's Bitcoin Alert with Python

2023-04-02

Silicon Valley is one of my favorite sitcoms, so I thought it might be a fun little project to recreate Gilfoyle's bitcoin alert using python. If you haven't watched the show, Gilfoyle is a software engineer who has a script that plays the metal song "You Suffer" by Napalm Death, whenever the price of bitcoin drops below or above a certain threshold.

Step 1: Setting up our work environment

Before we begin make sure that you have installed python3 on your computer

Next, we have to create our project folder and jump into it. We can do this by using the following commands in our terminal.

$ mkdir bitcoin-alert

$ cd bitcoin-alert

If you haven't installed pipenv before you can do so by using following command.

$ pip install --user pipenv

Now you can use pipenv to create a virtual environment for your project and manage your dependencies. We can install the necessary dependencies with the following command.

$ pipenv install requests playsound pyobjc

Now let's create our python file by typing the following command into our terminal.

$ touch alert.py

Step 2: Fetching the current bitcoin price

Before we start with the coding, we need to import some dependencies, which we'll use later.

import requests
import time
import argparse
from playsound import playsound

We define a function get_bitcoin_price, which takes currency as an input value, which has the default value of USD. Inside this function we use the request module, which we've imported before, to fetch the data from the coindesk API.

After, receiving the response, we use response.json() to check if we received a JSON object and store the received price in current_price. Next, we return its value after casting it to be of type float.

def get_bitcoin_price(currency='USD'):
    url = 'https://api.coindesk.com/v1/bpi/currentprice.json'
    response = requests.get(url)
    response_json = response.json()
    current_price = response_json['bpi'][currency]['rate']
    return float(current_price.replace(',', ''))

Step 3: Finishing it up

Next, we will continue by creating our main function. Before we write the part where we check whether the bitcoin price is below a certain threshold value. We use the argparse module, which we've imported before, to get some inputs from the command line.

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-C', '--currency', type=str, default='USD',
                        help='Currency to check the price. (Default: USD)')
    parser.add_argument('-M', '--min', type=float, default='30000',
                        help='Minimum price to alert. (Default: 30000)')
    parser.add_argument('-T', '--time', type=float, default='10',
                        help='Time in seconds to check the price. (Default: 10)')

    args = parser.parse_args()

Inside of our main function we create following while loop, which will run as long as our program runs. This loop checks if our price is below the threshold and if it evaluates true, it will use the imported playsound to play the song you suffer. If you want to you can also use another mp3 file. For that you just have to add it to your folder and add the correct naming into the playsound function.

    while True:
        if get_bitcoin_price(args.currency) < args.min:
            playsound('./you-suffer.mp3')

        time.sleep(args.time)

Step 4: Run our code

Now you can run your code by using the following command in your terminal.

$ python3 alert.py [-C currency] [-M minimum price] [-T time]

The information in curly braces are optional.

Feel free to check out the repository here Github.