Python problem with JSON


#1

So here’s my code so far, I’ve made a simple program to call to the CoinDesk API for the current Value of BTC (it does this correctly). I now want it to call the same API for the historical data of BTC for a specific date but I can’t seem to select the key (key error). I’m looking for:

  1. A solution to calling the historical BTC data for the entered date
  2. General areas of improvement for the program as a whole (it’s my first useful program)

Typical API Response:
https://api.coindesk.com/v1/bpi/historical/close.json?start=2018-07-01nd=2018-07-01

My code:

##########################################################
#Program to get the current value of BTC via an API call.#
##########################################################
#Import the libraries that are needed to handle JSON and URLs.
import urllib.request as url
import json
import ast

#Set a variable that will be changed later.
api_url_current = ''
api_url_historical = ''

#Get the currency the user wishes to operate in.
currency_input = input("What currency do you want to have? \neur (Euro) \ngbp (Great British Pound) \nusd (United States Dollar) \n:")

#This function takes what currency the user sets and manipulates that into the URL we're going to use to call the API.
def currency_selection(currency_input,api_url):
    base_url = "https://api.coindesk.com/v1/bpi/currentprice/x.json"
    currency_input = currency_input.upper()
    
    if currency_input == 'EUR':
        api_url = "https://api.coindesk.com/v1/bpi/currentprice/%s.json" % currency_input
        return api_url
    
    if currency_input == 'GBP':
        api_url = "https://api.coindesk.com/v1/bpi/currentprice/%s.json" % currency_input
        return api_url
    
    if currency_input == 'USD':
        api_url = "https://api.coindesk.com/v1/bpi/currentprice/%s.json" % currency_input
        return api_url
        
    else:
        print("Invalid Currency!")
        exit()

#Fetches the URL from the above function and displays the API URL that will be called.
api_url_current = currency_selection(currency_input,api_url_current)
current_api_call = ("The current API URL that will be called is: \n%s" % api_url_current)

#This function actually calls to the API itself and fetches the data.
def DataGetCurrent(api_url_current):
    request = url.urlopen(url.Request(api_url_current, headers={'User-Agent' : "Mozilla/5.0"}))
    data = request.read()
    data = data.decode("utf-8")
    data = ast.literal_eval(data)
    return data

#Fetches the API data from the above API call.
data_current = DataGetCurrent(api_url_current)

#Sets the current value of BTC in the currency selected by the user.
BTC_Current_Value = ("The current price of BTC in %s (%s) is %s!" % (data_current['bpi'][currency_input.upper()]['code'],data_current['bpi'][currency_input.upper()]['description'],data_current['bpi'][currency_input.upper()]['rate']))
print(BTC_Current_Value)
###############################################################
#Program to get the historical value for BTC from an API call.#
###############################################################
#Select the date that the user purchased BTC on
BTC_Date = input("What date did you purchase BTC on? (format: yyyy-mm-dd): ")

def DataGetHistorical(BTC_Date):
    basic_url = ("https://api.coindesk.com/v1/bpi/historical/close.json?start=%snd=%s" % (BTC_Date,BTC_Date))
    print("API Call URL is: %s " % basic_url)
    request = url.urlopen(url.Request(basic_url, headers={'User-Agent' : "Mozilla/5.0"}))
    data = request.read()
    data = data.decode("utf-8")
    data = ast.literal_eval(data)
    return data

Data_Historical = DataGetHistorical(BTC_Date)

BTC_Value_Historical = "The value of BTC for %s (%s) on %s was %s!" % (data_current['bpi'][currency_input.upper()]['code'],data_current['bpi'][currency_input.upper()]['description'],Data_Historical['bpi'][str(BTC_Date)])
print(BTC_Value_Historical)