Skip to content

Bitcoin dashboard Cloud Function code

File Structure:

  • main.py
  • requirements.txt
  • service-account-key.json
  • .env

main.py


from dotenv import load_dotenv
import os
import pandas as pd
from google.oauth2 import service_account
import requests
import ssl

# load variables into environment
load_dotenv()


def collect_and_upload_bitcoin_data(request):

    # set up healthchecks.io monitoring
    healthcheck_url = "https://hc-ping.com/dd321f03-1643-43b7-a38e-ea6886a14ba6"
    requests.get(healthcheck_url + "/start")

    # load variables into environment
    #load_dotenv()



    filepath = "https://www.cryptodatadownload.com/cdd/Binance_BTCUSDT_d.csv"

    ssl._create_default_https_context = ssl._create_unverified_context

    data = pd.read_csv(filepath, skiprows=1)

    # Tidy columns
    data.columns = [column.replace(" ", "_") for column in list(data.columns) ]
    data.columns = [column.lower() for column in list(data.columns) ]

    service_account_json = os.getenv("SERVICE_ACCOUNT_JSON")
    project_id = os.getenv("PROJECT_ID")



    # create Google credentials from service account json file

    credentials = service_account.Credentials.from_service_account_file(
        service_account_json,
    )


    data.to_gbq(
        "marquinsmith_dot_com.bitcoinusdprice",
        project_id=project_id,
        if_exists="replace",
        credentials=credentials,
    )

    # tell healthchecks.io the script is finished
    requests.get(healthcheck_url)

requirements.txt

# Function dependencies, for example:
# package>=version

backcall==0.1.0
cachetools==4.1.0
certifi==2020.4.5.1
chardet==3.0.4
decorator==4.4.2
google-api-core==1.16.0
google-auth==1.13.1
google-auth-oauthlib==0.4.1
google-cloud-bigquery==1.24.0
google-cloud-core==1.3.0
google-resumable-media==0.5.0
googleapis-common-protos==1.51.0
idna==2.9
ipython==7.13.0
ipython-genutils==0.2.0
jedi==0.16.0
numpy==1.18.2
oauthlib==3.1.0
pandas==1.0.3
pandas-gbq==0.13.1
parso==0.6.2
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.5
protobuf==3.11.3
ptyprocess==0.6.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pydata-google-auth==0.3.0
Pygments==2.6.1
python-dateutil==2.8.1
python-dotenv==0.12.0
pytz==2019.3
requests==2.23.0
requests-oauthlib==1.3.0
rsa==4.0
six==1.14.0
traitlets==4.3.3
urllib3==1.25.8
wcwidth==0.1.9

.env


SERVICE_ACCOUNT_JSON=service-account-key.json
PROJECT_ID=marquin-personal-tools