import re import sys import optparse from datetime import datetime from operator import itemgetter parser = optparse.OptionParser(usage="!bus i.e: !bus Liberation 15 Outbound") parser.parse_args() def split(s): return filter(None, re.split(r'(\d+)', s)) ircinput = sys.stdin.read().lower() input = ircinput.split() stop = re.split('[\s][(\d)x]', ircinput)[0] bus = input[-2].strip() bound = input[-1].strip() busfile = bus.upper() + '.txt' now = datetime.now() today = now.strftime("%A").lower() datelist = [] if bound == "inbound": endbound = "outbound" else: endbound = "inbound" if now.isoweekday() in range(1, 5): day = "monday-friday" elif now.isoweekday() == 6: day = "saturday" else: day = "sunday" boundsearch = (bound + " - " + day) with open(busfile, "r") as busread: for line in busread: line = line.lower() if boundsearch in line: break for line in busread: linelw = line.lower() if stop in linelw: previousline = line.strip() break for line in busread: line = line.strip() if ":" in line: date = datetime.strptime(line, "%H:%M").time() if date > now.time(): datelist.append(date.strftime("%H:%M" + " ")) else: datelist.append("\n") liststring = ''.join(datelist) sys.stdout.write("The next " + bound + " #" + bus + " buses at " + previousline + " " + "are: " + liststring) break