import sys
from bs4 import BeautifulSoup

if sys.version_info[0] < 3:
    from urllib2 import urlopen, URLError
else:
    from urllib.request import urlopen, URLError

# Author information.
__author__ = 'Salvador Eduardo Tropea'
__webpage__ = 'https://github.com/set-soft/'
__company__ = 'INTI-CMNB - Argentina'

url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'


def download_rates():
    content = ''
    try:
        content = urlopen(url).read().decode('utf8')
    except URLError:
        pass
    soup = BeautifulSoup(content, 'xml')
    rates = {'EUR': 1.0}
    date = ''
    for entry in soup.find_all('Cube'):
        currency = entry.get('currency')
        if currency is not None:
            rates[currency] = float(entry.get('rate'))
        elif entry.get('time'):
            date = entry.get('time')
    return date, rates

import os
import sys
from bs4 import BeautifulSoup

if sys.version_info[0] < 3:
    from urllib2 import urlopen, URLError
else:
    from urllib.request import urlopen, URLError

# Author information.
__author__ = 'Salvador Eduardo Tropea'
__webpage__ = 'https://github.com/set-soft/'
__company__ = 'INTI-CMNB - Argentina'

url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'


def download_rates():
    content = ''
    if os.environ.get('KICOST_CURRENCY_RATES'):
        with open(os.environ['KICOST_CURRENCY_RATES'], 'rt') as f:
            content = f.read()
    else:
        try:
            content = urlopen(url).read().decode('utf8')
        except URLError:
            pass
    soup = BeautifulSoup(content, 'xml')
    rates = {'EUR': 1.0}
    date = ''
    for entry in soup.find_all('Cube'):
        currency = entry.get('currency')
        if currency is not None:
            rates[currency] = float(entry.get('rate'))
        elif entry.get('time'):
            date = entry.get('time')
    return date, rates

Allow to fake the currency rates.
Allow to fake the currency rates.

- The envidonment variable KICOST_CURRENCY_RATES can be used to fake
  the cunrrency rates.
- It must indicate the name of an XML file containing the desired
  rates.
- The format is the one used by the European Central Bank.

+ import os
  import sys
  from bs4 import BeautifulSoup
  
  if sys.version_info[0] < 3:
      from urllib2 import urlopen, URLError
  else:
      from urllib.request import urlopen, URLError
  
  # Author information.
  __author__ = 'Salvador Eduardo Tropea'
  __webpage__ = 'https://github.com/set-soft/'
  __company__ = 'INTI-CMNB - Argentina'
  
  url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
  
  
  def download_rates():
      content = ''
+     if os.environ.get('KICOST_CURRENCY_RATES'):
+         with open(os.environ['KICOST_CURRENCY_RATES'], 'rt') as f:
+             content = f.read()
+     else:
-     try:
+         try:
-         content = urlopen(url).read().decode('utf8')
+             content = urlopen(url).read().decode('utf8')
-     except URLError:
+         except URLError:
-         pass
+             pass
      soup = BeautifulSoup(content, 'xml')
      rates = {'EUR': 1.0}
      date = ''
      for entry in soup.find_all('Cube'):
          currency = entry.get('currency')
          if currency is not None:
              rates[currency] = float(entry.get('rate'))
          elif entry.get('time'):
              date = entry.get('time')
      return date, rates
  
Allow to fake the currency rates.
## Code Before:
import sys
from bs4 import BeautifulSoup

if sys.version_info[0] < 3:
    from urllib2 import urlopen, URLError
else:
    from urllib.request import urlopen, URLError

# Author information.
__author__ = 'Salvador Eduardo Tropea'
__webpage__ = 'https://github.com/set-soft/'
__company__ = 'INTI-CMNB - Argentina'

url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'


def download_rates():
    content = ''
    try:
        content = urlopen(url).read().decode('utf8')
    except URLError:
        pass
    soup = BeautifulSoup(content, 'xml')
    rates = {'EUR': 1.0}
    date = ''
    for entry in soup.find_all('Cube'):
        currency = entry.get('currency')
        if currency is not None:
            rates[currency] = float(entry.get('rate'))
        elif entry.get('time'):
            date = entry.get('time')
    return date, rates

## Instruction:
Allow to fake the currency rates.
## Code After:
import os
import sys
from bs4 import BeautifulSoup

if sys.version_info[0] < 3:
    from urllib2 import urlopen, URLError
else:
    from urllib.request import urlopen, URLError

# Author information.
__author__ = 'Salvador Eduardo Tropea'
__webpage__ = 'https://github.com/set-soft/'
__company__ = 'INTI-CMNB - Argentina'

url = 'https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'


def download_rates():
    content = ''
    if os.environ.get('KICOST_CURRENCY_RATES'):
        with open(os.environ['KICOST_CURRENCY_RATES'], 'rt') as f:
            content = f.read()
    else:
        try:
            content = urlopen(url).read().decode('utf8')
        except URLError:
            pass
    soup = BeautifulSoup(content, 'xml')
    rates = {'EUR': 1.0}
    date = ''
    for entry in soup.find_all('Cube'):
        currency = entry.get('currency')
        if currency is not None:
            rates[currency] = float(entry.get('rate'))
        elif entry.get('time'):
            date = entry.get('time')
    return date, rates
