Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
handle errors more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hamblen committed Dec 30, 2010
1 parent 6b6f6ae commit e3a395b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions xmlevents.py
Expand Up @@ -11,7 +11,7 @@
from datetime import datetime, timedelta

if len(sys.argv) != 4:
print("Usage: xmlevents.py <input file> <api key> <urlname>")
print "Usage: xmlevents.py <input file> <api key> <urlname>"
sys.exit(1)

cmd, file, key, urlname = sys.argv
Expand All @@ -34,15 +34,12 @@ def text(name):
else: return None

t_event_id = text("t_event_id")
print(t_event_id) # print event id now in case something goes wrong

try: dt = datetime.strptime(text("t_event_date").rstrip("0")[:-1], '%Y-%m-%d %H:%M:%S')
except: dt = None
if dt == None:
print "bad time format: %s" % text("t_event_date")
elif dt < now:
print "skipping past event"
else:
print "t_event_id %s - Bad time format: %s" % (t_event_id, text("t_event_date"))
elif dt > now:
# default to event create
endpoint = "/ew/event"
conn = HTTPConnection(host)
Expand All @@ -52,7 +49,10 @@ def text(name):
"udf_t_event_id": t_event_id,
"key": key}))
res = conn.getresponse()
assert res.status == 200 # abort if not an okay response
if res.status != 200:
# abort processing as this indicates there may be a service problem
print "Error querying t_event_id %s:\n%s\nExport halted." % (t_event_id, res.read())
sys.exit(1)
resdoc = xml.dom.minidom.parseString(res.read())
for item in resdoc.getElementsByTagName("item")[:1]:
for id_node in [ch for ch in item.childNodes if ch.tagName.lower() == "id"][:1]:
Expand All @@ -65,6 +65,8 @@ def text(name):
"local_time": int(time.mktime(dt.timetuple())) * 1000,
"zip": text("t_store_zip_code"),
"address1": text("t_store_address"),
"city": text("t_store_city"),
"state": text("t_store_state"),
"venue_name": "[OFFICIAL] " + "%s %s %s" % (first or "", last or "", text("t_event_type")),
"title": text("t_short_description"), # not visible on site
"description": desc,
Expand All @@ -74,5 +76,6 @@ def text(name):
headers = {"Content-type": "application/x-www-form-urlencoded"}
conn.request("POST", endpoint, urllib.urlencode(params), headers)
res = conn.getresponse()
print res.read() # may be useful for debugging
assert res.status in [200,201] # abort if not an okay/created response
if res.status not in [200,201]:
# most likely bad input, continue processing...
print "t_event_id %s - Unable to add event:\n%s" % (t_event_id, res.read())

0 comments on commit e3a395b

Please sign in to comment.