Allow calculations from the command line
This commit is contained in:
parent
6e522170fb
commit
d7f0030c21
@ -48,9 +48,10 @@ def main():
|
|||||||
|
|
||||||
if args.calculate:
|
if args.calculate:
|
||||||
#position_calculations('Winelands Marathon')
|
#position_calculations('Winelands Marathon')
|
||||||
position_calculations()
|
position_calculations(args.calculate)
|
||||||
return
|
return
|
||||||
elif args.scrape_web:
|
elif args.scrapeurl:
|
||||||
|
requrls = args.scrapeurl
|
||||||
spreadsheets = []
|
spreadsheets = []
|
||||||
uniqurl = []
|
uniqurl = []
|
||||||
wpa = 'http://www.wpa.org.za/Events/DynamicEvents.asmx/BuildEventDisplay'
|
wpa = 'http://www.wpa.org.za/Events/DynamicEvents.asmx/BuildEventDisplay'
|
||||||
@ -79,10 +80,16 @@ def main():
|
|||||||
if dist is not None:
|
if dist is not None:
|
||||||
raceinfo['distance'] = dist.string
|
raceinfo['distance'] = dist.string
|
||||||
spreadsheets.append(raceinfo)
|
spreadsheets.append(raceinfo)
|
||||||
#pp.pprint(spreadsheets)
|
|
||||||
#sys.exit(1)
|
|
||||||
for race in spreadsheets:
|
for race in spreadsheets:
|
||||||
url = race['url']
|
url = race['url']
|
||||||
|
event = race['event']
|
||||||
|
''' Only parse one spreadsheet from the WPA website, from the commandline '''
|
||||||
|
isthisevent = False
|
||||||
|
for checkurl in requrls:
|
||||||
|
if re.search(checkurl, event, flags=re.IGNORECASE):
|
||||||
|
isthisevent = True
|
||||||
|
if type(requrls[0]) != None and url not in requrls and not isthisevent:
|
||||||
|
continue
|
||||||
with urllib.request.urlopen(url) as response, tempfile.TemporaryDirectory() as tmpdir:
|
with urllib.request.urlopen(url) as response, tempfile.TemporaryDirectory() as tmpdir:
|
||||||
log.info("Loading data from URL {}".format(race['url']))
|
log.info("Loading data from URL {}".format(race['url']))
|
||||||
data = response.read()
|
data = response.read()
|
||||||
@ -98,6 +105,7 @@ def main():
|
|||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
load_into_db(rows)
|
load_into_db(rows)
|
||||||
|
position_calculations(event)
|
||||||
log.debug("\n")
|
log.debug("\n")
|
||||||
|
|
||||||
|
|
||||||
@ -106,6 +114,7 @@ def main():
|
|||||||
rows = read_spreadsheet(args.input_file, src=args.input_file)
|
rows = read_spreadsheet(args.input_file, src=args.input_file)
|
||||||
log.info("Loading data from file {}".format(args.input_file))
|
log.info("Loading data from file {}".format(args.input_file))
|
||||||
load_into_db(rows)
|
load_into_db(rows)
|
||||||
|
position_calculations()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
for message in mailbox.Maildir(MAILDIR):
|
for message in mailbox.Maildir(MAILDIR):
|
||||||
@ -133,18 +142,25 @@ def main():
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
load_into_db(rows)
|
load_into_db(rows)
|
||||||
|
position_calculations()
|
||||||
|
|
||||||
position_calculations()
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def position_calculations(event=None):
|
def position_calculations(events=None):
|
||||||
db = MySQLdb.connect(user='aac', passwd='saOAcCWHg4LaoSSA', db='AAC',
|
db = MySQLdb.connect(user='aac', passwd='saOAcCWHg4LaoSSA', db='AAC',
|
||||||
use_unicode=True, charset="utf8", cursorclass=MySQLdb.cursors.DictCursor)
|
use_unicode=True, charset="utf8", cursorclass=MySQLdb.cursors.DictCursor)
|
||||||
c = db.cursor()
|
c = db.cursor()
|
||||||
|
|
||||||
where = ''
|
where = ''
|
||||||
if event:
|
wheres = []
|
||||||
where = 'WHERE event LIKE "%{}%"'.format(event)
|
if isinstance(events, list):
|
||||||
|
for event in events:
|
||||||
|
if event:
|
||||||
|
wheres.append('event LIKE "%{}%"'.format(event))
|
||||||
|
elif isinstance(events, str):
|
||||||
|
wheres.append('event LIKE "%{}%"'.format(events))
|
||||||
|
if wheres:
|
||||||
|
where = 'WHERE ' + ' OR '.join(wheres)
|
||||||
sql = 'SELECT event, date, distance FROM `results` {} GROUP BY event, date, distance'.format(where)
|
sql = 'SELECT event, date, distance FROM `results` {} GROUP BY event, date, distance'.format(where)
|
||||||
c.execute(sql)
|
c.execute(sql)
|
||||||
#log.debug(c._last_executed)
|
#log.debug(c._last_executed)
|
||||||
@ -153,7 +169,7 @@ def position_calculations(event=None):
|
|||||||
for race in eventlist:
|
for race in eventlist:
|
||||||
log.debug(race)
|
log.debug(race)
|
||||||
|
|
||||||
log.debug("Recalculating postion information for {}".format(race['event'])
|
log.debug("Recalculating postion information for {}".format(race['event']))
|
||||||
''' Calculate total finishers per race '''
|
''' Calculate total finishers per race '''
|
||||||
sql = 'UPDATE `results` AS r, (SELECT event, date, distance, COUNT(distance) AS finishers FROM `results` WHERE event LIKE "{}" AND date = "{}" AND distance LIKE "{}" GROUP BY event, date, distance) AS f SET r.finishers = f.finishers WHERE r.event = f.event AND r.date = f.date AND r.distance = f.distance AND r.finishers IS NULL;'.format(race['event'], race['date'], race['distance'])
|
sql = 'UPDATE `results` AS r, (SELECT event, date, distance, COUNT(distance) AS finishers FROM `results` WHERE event LIKE "{}" AND date = "{}" AND distance LIKE "{}" GROUP BY event, date, distance) AS f SET r.finishers = f.finishers WHERE r.event = f.event AND r.date = f.date AND r.distance = f.distance AND r.finishers IS NULL;'.format(race['event'], race['date'], race['distance'])
|
||||||
c.execute(sql)
|
c.execute(sql)
|
||||||
@ -476,11 +492,11 @@ def clean_data(input_rows):
|
|||||||
def parse_arguments():
|
def parse_arguments():
|
||||||
parser = argparse.ArgumentParser(description='Load a spreadsheet containing WPA results into a database')
|
parser = argparse.ArgumentParser(description='Load a spreadsheet containing WPA results into a database')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--web', '-w', action='store_true', required=False, dest='scrape_web',
|
'--url', '-u', action='append', required=False, dest='scrapeurl', nargs="?",
|
||||||
help='Scrape WPA website')
|
help='Scrape WPA website, or, if a link is specified, the spreadsheet at that link')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--calc', '-c', action='store_true', required=False, dest='calculate',
|
'--calc', '-c', action='append', required=False, dest='calculate', nargs="?",
|
||||||
help='Calculate unset positions in the database')
|
help='Calculate unset positions in the database, optionally just for specified races')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--input', '-i', action='store', required=False, type=str, dest='input_file',
|
'--input', '-i', action='store', required=False, type=str, dest='input_file',
|
||||||
help='Manually select the spreadsheet to be imported')
|
help='Manually select the spreadsheet to be imported')
|
||||||
|
Loading…
Reference in New Issue
Block a user