Created
September 11, 2019 11:23
-
-
Save fredkingham/28ffab6b2cdeae9e039cef155f8d0b66 to your computer and use it in GitHub Desktop.
parse BH fixtures in to GCAL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Expects a file that looks like | |
Saturday 7 December 19 | |
Swansea Storm | |
Blackhawks 2 | |
14:00 | |
The LC Swansea, Oystermouth Road | |
Swansea, SA1 3ST | |
Sunday 19 January 20 | |
Derby Wheelblazers | |
Blackhawks 2 | |
13:30 | |
Rykneld Sports Centre, Richmond Park Road, Derby DE22 4LP | |
Saturday 8 February 20 | |
Thames Valley Kings 2 | |
Blackhawks 2 | |
12:00 | |
FBC Centre, Gorse Ride North, Finchampstead, Wokingham RG40 4ES | |
# etc | |
""" | |
import csv | |
import datetime | |
SOMETHING = "something.txt" | |
OUTPUT = "something.csv" | |
SUBJECT = "Subject" | |
START_DATE = "Start Date" | |
START_TIME = "Start Time" | |
END_DATE = "End Date" | |
END_TIME = "End Time" | |
DESCRIPTION = "Description" | |
LOCATION = "Location" | |
def parse_date(dt_row, time_row): | |
dt = dt_row.split(" ") | |
day, month = dt[1], dt[2] | |
m = datetime.datetime.strptime(month, "%B").month | |
if m < 8: | |
y = 2020 | |
else: | |
y = 2019 | |
if not time_row.lower() == "tbc": | |
time = parse_time(time_row) | |
return datetime.datetime(y, m, int(day), time.hour, time.minute) | |
else: | |
return datetime.datetime(y, m, int(day)) | |
def parse_time(time_row): | |
return datetime.time(*[int(i) for i in time_row.split(":")]) | |
def to_csv_row(splitted): | |
if len(splitted) < 4: | |
import ipdb; ipdb.set_trace() | |
dt = parse_date(splitted[0], splitted[3]) | |
home_team = splitted[1] | |
away_team = splitted[2] | |
address = ", ".join(splitted[4:]) | |
is_home = "black" in home_team.lower() and "hawk" in home_team.lower() | |
tbc = splitted[3].lower() == "tbc" | |
if is_home: | |
location = "(home)" | |
else: | |
location = "(away)" | |
subject = "{} vs {} {}".format(home_team, away_team, location) | |
if tbc: | |
start_time = "" | |
end_time = "" | |
description = "Time TBC" | |
else: | |
start_time = (dt - datetime.timedelta(hours=1)).time() | |
end_time = (dt + datetime.timedelta(hours=2)).time() | |
description = "Tip off {}".format(dt.strftime('%d/%m/%Y %H:%M:%S')) | |
return { | |
SUBJECT: subject, | |
START_DATE: dt.date(), | |
START_TIME: start_time, | |
END_DATE: dt.date(), | |
END_TIME: end_time, | |
DESCRIPTION: description, | |
LOCATION: address | |
} | |
def split_by_space(file_contents): | |
result = [] | |
temp_row = [] | |
for i in file_contents: | |
if not i.strip(): | |
result.append(temp_row) | |
temp_row = [] | |
else: | |
if i.strip(): | |
temp_row.append(i.strip()) | |
if temp_row: | |
result.append(temp_row) | |
result = [i for i in result if i] | |
return result | |
def to_csv(): | |
with open(SOMETHING) as old_file: | |
splitted = split_by_space(old_file) | |
rows = [] | |
for i in splitted: | |
rows.append(to_csv_row(i)) | |
with open(OUTPUT, "w+") as new_file: | |
writer = csv.DictWriter(new_file, fieldnames=rows[0].keys()) | |
writer.writeheader() | |
for i in rows: | |
writer.writerow(i) | |
if __name__ == "__main__": | |
to_csv() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment