Skip to content

Instantly share code, notes, and snippets.

@dangayle
Last active December 20, 2015 11:59
Show Gist options
  • Save dangayle/6127265 to your computer and use it in GitHub Desktop.
Save dangayle/6127265 to your computer and use it in GitHub Desktop.
from StringIO import StringIO
import requests
from xmlutils.xml2csv import xml2csv
"""Grab xml schedules from gsl.tandemcal.com and convert to csv"""
varsity_sports_ids = {
"boys_baseball": 26,
"boys_basketball": 19,
"girls_basketball": 20,
"boys_cross_country": 7,
"girls_cross_country": 23,
"football": 3,
"boys_golf": 38,
"girls_golf": 39,
"gymnastics": 10,
"boys_soccer": 28,
"girls_soccer": 14,
"fast_pitch_softball": 32,
"slow_pitch_softball": 8,
"boys_tennis": 30,
"girls_tennis": 31,
"track": 36,
"volleyball": 12,
"wrestling": 9,
}
def convert_to_csv(sport):
"""Grab xml schedule, convert and save as csv.
Returns the amount of rows converted
>>> convert_to_csv('boys_cross_country')
33
"""
params = {
"export_type": "report",
"action": "xml",
"type": "export"
}
params['id'] = varsity_sports_ids[sport]
r = requests.get('http://gsl.tandemcal.com/', params=params)
# xml2csv expects a file, so the request text needs to be wrapped in StringIO
converter = xml2csv(StringIO(r.text), sport + ".csv")
return converter.convert(tag="event")
# Fails. Appends each sport onto newly created csv
# for sport in varsity_sports_ids:
# print convert_to_csv(sport)
# works as expected
# print convert_to_csv("wrestling")
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment