diff --git a/nbp/table.py b/nbp/table.py index 1d689fa..eb737cd 100644 --- a/nbp/table.py +++ b/nbp/table.py @@ -109,7 +109,9 @@ def download(url): try: resp = urllib2.urlopen(url) if hasattr(resp, 'getcode') and resp.getcode() == 200: - return resp + if hasattr(resp, 'info') and resp.info()['content-type'] == 'text/xml': + return resp + return None except urllib2.URLError, e: resp = None return resp diff --git a/tests.py b/tests.py index 8422476..70a2faa 100644 --- a/tests.py +++ b/tests.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import nbp +from datetime import date from nose.tools import assert_equals @@ -18,3 +19,18 @@ def test_downloads_exchange_rate_properly(): } currency_data = nbp.download_exchange_rate(nbp.date(2010, 7, 11), 'EUR') assert_equals(expexted, currency_data) + +def test_downloads_exchange_rate_properly_even_with_broken_nbp_404_200(): + expexted = { + 'search_date': '2014-11-03', + 'table_no': u'213/A/NBP/2014', + 'pub_date': u'2014-11-03', + 'url': 'http://rss.nbp.pl/kursy/xml2/2014/a/14a213.xml', + 'currency': { + 'name': u'euro', + 'rate': 4.2209, + 'code': u'EUR', + }, + } + currency_data = nbp.download_exchange_rate(date.today(), 'EUR') + assert_equals(date.today().strftime('%Y-%m-%d'), currency_data['pub_date'])