0

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)
PeteG
  • 49
  • 7

1 Answers1

0
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.

WannaBeCoder
  • 1,280
  • 2
  • 17
  • 40
  • Sorry i am very new to python could you elaborate more on how i could get it into the URL, Many Thanks – PeteG Apr 06 '15 at 12:04
  • You Absolute legend thank you ever so much! owe you big time. – PeteG Apr 06 '15 at 12:11
  • It was not that tough. You better read documentation before you face some issue like this. :) – WannaBeCoder Apr 06 '15 at 12:12
  • Yeah i have tried, but programming is not that second nature to me (I'm a maths undergraduate student not a computer science student) – PeteG Apr 06 '15 at 12:29