90 lines
3.2 KiB
Python
Raw Normal View History

2023-11-26 10:30:42 +00:00
import pandas as pd
from icalendar import Calendar, Event
from datetime import datetime, timedelta
import uuid
import os
def store_df_as_csv(df:pd.DataFrame)->None:
2023-11-27 09:41:06 +00:00
"""Store dataframe as a CSV file.
2023-11-26 10:30:42 +00:00
Args:
2023-11-27 09:41:06 +00:00
df (pd.DataFrame): Dataframe of fixtures.
2023-11-26 10:30:42 +00:00
"""
df.to_csv('./fixtures.csv', index=False)
def compare_dfs(df:pd.DataFrame)->bool:
2023-11-27 09:41:06 +00:00
"""Compare the latest DF with the stored DF for any changes.
2023-11-26 10:30:42 +00:00
Args:
2023-11-27 09:41:06 +00:00
df (pd.DataFrame): Latest copy of fixtures in dataframe
2023-11-26 10:30:42 +00:00
Returns:
2023-11-27 09:41:06 +00:00
bool: True if match, False if no match.
2023-11-26 10:30:42 +00:00
"""
df2 = pd.read_csv('./fixtures.csv')
return df.equals(df2)
def write_calendar(cal:Calendar)->None:
2023-11-27 09:41:06 +00:00
"""Write the cal object to an ics file.
2023-11-26 10:30:42 +00:00
Args:
2023-11-27 09:41:06 +00:00
cal (Calendar): iCalendar object with all the ics details.
2023-11-26 10:30:42 +00:00
"""
f = open(os.path.join('./', 'fixtures.ics'), 'wb')
f.write(cal.to_ical())
f.close()
def does_csv_exist()->bool:
2023-11-27 09:41:06 +00:00
"""Check if the CSV file exists.
2023-11-26 10:30:42 +00:00
Returns:
2023-11-27 09:41:06 +00:00
bool: True if CSV file exists, False if not.
2023-11-26 10:30:42 +00:00
"""
return os.path.isfile('./fixtures.csv')
2023-11-27 09:41:06 +00:00
def create_ical_file(df:pd.DataFrame, cal:Calendar)->None:
"""Create an iCalendar file from a dataframe.
Args:
df (pd.DataFrame): Dataframe of fixtures.
cal (Calendar): iCalendar object with all the ics details.
"""
2023-11-26 10:30:42 +00:00
for index, row in df.iterrows():
event = Event()
print(row['Date / Time'], row['Home Team'], row['Away Team.1'], row['Venue'])
start_date_time = datetime.strptime(row['Date / Time'], '%d/%m/%y %H:%M')
2023-11-27 09:41:06 +00:00
# Set default 8am start time to normal 930 kickoff time.
2023-11-26 10:30:42 +00:00
if start_date_time.hour == 8:
start_date_time = start_date_time + timedelta(hours=1, minutes=30)
2023-11-27 09:41:06 +00:00
# Arrival time is 30 mins before kickoff time.
2023-11-26 10:30:42 +00:00
arrival_time = start_date_time + timedelta(minutes=-30)
2023-11-27 09:41:06 +00:00
event.add('summary', str(row['Home Team']) + f" {str(row['Unnamed: 4'])} " + str(row['Away Team.1']))
2023-11-26 10:30:42 +00:00
event.add('description', f'Arrive by - {arrival_time}')
event.add('dtstart', start_date_time)
2023-11-27 09:42:15 +00:00
# End 2 hours after start_date_time
2023-11-26 10:30:42 +00:00
event.add('dtend', start_date_time + timedelta(hours=2))
event.add('dtstamp', start_date_time)
event.add('uid', str(uuid.uuid4()))
event.add('location', str(row['Venue']))
cal.add_component(event)
write_calendar(cal)
cal = Calendar()
cal.add('prodid', 'Down Grange Pumas Fixtures')
cal.add('version', '2.0')
url = "https://fulltime.thefa.com/fixtures.html?selectedSeason=19010414&selectedFixtureGroupAgeGroup=11&selectedFixtureGroupKey=1_579285719&selectedDateCode=all&selectedClub=&selectedTeam=466317969&selectedRelatedFixtureOption=3&selectedFixtureDateStatus=&selectedFixtureStatus=&previousSelectedFixtureGroupAgeGroup=11&previousSelectedFixtureGroupKey=1_579285719&previousSelectedClub=&itemsPerPage=25"
df = pd.read_html(url)[0]
df.head()
exists = does_csv_exist()
if exists:
no_change = compare_dfs(df)
if not no_change:
2023-11-27 14:03:29 +00:00
print("Fixtures updated, ical updated")
2023-11-26 10:30:42 +00:00
store_df_as_csv(df)
create_ical_file(df, cal)
2023-11-27 14:03:29 +00:00
else:
2023-11-28 11:47:36 +00:00
print("Fixtures not updated, no update to ical")
2023-11-26 10:30:42 +00:00
else:
store_df_as_csv(df)
2023-11-27 14:03:29 +00:00
create_ical_file(df, cal)
print("New ical file created")