Skip to content

Commit

Permalink
If TDSCatalog detects html, warn and try an XML link.
Browse files Browse the repository at this point in the history
Fixes #29.
  • Loading branch information
dopplershift committed Jun 24, 2015
1 parent 39eddeb commit 9ec0b34
Show file tree
Hide file tree
Showing 3 changed files with 392 additions and 2 deletions.
20 changes: 18 additions & 2 deletions siphon/catalog.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
import xml.etree.ElementTree as ET
from io import BytesIO

from .metadata import TDSCatalogMetadata
from .http_util import urlopen
from .http_util import create_http_session, urlopen

log = logging.getLogger("siphon.catalog")
log.setLevel(logging.WARNING)
Expand Down Expand Up @@ -41,8 +42,23 @@ def __init__(self, catalog_url):
# top level server url
self.catalog_url = catalog_url
self.base_tds_url = catalog_url.split('/thredds/')[0]

session = create_http_session()

# get catalog.xml file
xml_fobj = urlopen(catalog_url)
resp = session.get(self.catalog_url)

# If we were given an HTML link, warn about it and try to fix to xml
if 'html' in resp.headers['content-type']:
import warnings
new_url = self.catalog_url.replace('html', 'xml')
warnings.warn('URL %s returned HTML. Changing to: %s' % (self.catalog_url,
new_url))
self.catalog_url = new_url
resp = session.get(self.catalog_url)

xml_fobj = BytesIO(resp.content)

# begin parsing the xml doc
tree = ET.parse(xml_fobj)
root = tree.getroot()
Expand Down
Loading

0 comments on commit 9ec0b34

Please sign in to comment.