Search code examples
datepython-3.xcurrencycurrency-exchange-rates

Inserting date into URL and Getting currency data for that date


I am trying to write a function which gathers currency data from a certain date from https://openexchangerates.org/ and return it as a dicitionary. I am a bit stuck on how to insert a date in the url at the point where it says YYYY-MM-DD and then also how to get it onto python.

Any help will be hugely appreciated

My code that i have so far is as follows:

def _fetch_exrates(date):
    import json
    import urllib.request
    f = urllib.request.urlopen('http://openexchangerates.org/api/historical/YYYY-MM-DD.json?app_id=188bd7b8d2634e4cb0602ab5b3c223e7')
    charset = f.info().get_param('charset', 'utf8')
    data = f.read()
    decoded = json.loads(data.decode(charset))
    print(json.dumps(decoded, indent=4))

import datetime 
print('Please but the year in the form of YYYY and the month as MM and day as DD')
a = int(input('choose a year :',))
b = int(input('choose a month :',))
c = int(input('choose a day :',))
date = datetime.date(a,b,c)
print(date)

Solution

  • def _fetch_exrates(year,month,day):
        import json
        import urllib.request
        f = urllib.request.urlopen('http://openexchangerates.org/api/historical/'+year+'-'+month+'-'+day+'.json?app_id=188bd7b8d2634e4cb0602ab5b3c223e7')
        charset = f.info().get_param('charset', 'utf8')
        data = f.read()
        decoded = json.loads(data.decode(charset))
        print(json.dumps(decoded, indent=4))
    
    print('Please but the year in the form of YYYY and the month as MM and day as DD')
    year = raw_input('choose a year :',)
    month = raw_input('choose a month :',)
    day = raw_input('choose a day :',)
    _fetch_exrates(year,month,day)
    

    Then using the strings as above construct it.