main.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import pandas as pd
  2. from icalendar import Calendar, Event
  3. from datetime import datetime, timedelta
  4. import uuid
  5. import os
  6. def store_df_as_csv(df:pd.DataFrame)->None:
  7. """Store dataframe as a CSV file.
  8. Args:
  9. df (pd.DataFrame): Dataframe of fixtures.
  10. """
  11. df.to_csv('./fixtures.csv', index=False)
  12. def compare_dfs(df:pd.DataFrame)->bool:
  13. """Compare the latest DF with the stored DF for any changes.
  14. Args:
  15. df (pd.DataFrame): Latest copy of fixtures in dataframe
  16. Returns:
  17. bool: True if match, False if no match.
  18. """
  19. df2 = pd.read_csv('./fixtures.csv')
  20. return df.equals(df2)
  21. def write_calendar(cal:Calendar)->None:
  22. """Write the cal object to an ics file.
  23. Args:
  24. cal (Calendar): iCalendar object with all the ics details.
  25. """
  26. f = open(os.path.join('./', 'fixtures.ics'), 'wb')
  27. f.write(cal.to_ical())
  28. f.close()
  29. def does_csv_exist()->bool:
  30. """Check if the CSV file exists.
  31. Returns:
  32. bool: True if CSV file exists, False if not.
  33. """
  34. return os.path.isfile('./fixtures.csv')
  35. def create_ical_file(df:pd.DataFrame, cal:Calendar)->None:
  36. """Create an iCalendar file from a dataframe.
  37. Args:
  38. df (pd.DataFrame): Dataframe of fixtures.
  39. cal (Calendar): iCalendar object with all the ics details.
  40. """
  41. for index, row in df.iterrows():
  42. event = Event()
  43. print(row['Date / Time'], row['Home Team'], row['Away Team.1'], row['Venue'])
  44. start_date_time = datetime.strptime(row['Date / Time'], '%d/%m/%y %H:%M')
  45. # Set default 8am start time to normal 930 kickoff time.
  46. if start_date_time.hour == 8:
  47. start_date_time = start_date_time + timedelta(hours=1, minutes=30)
  48. # Arrival time is 30 mins before kickoff time.
  49. arrival_time = start_date_time + timedelta(minutes=-30)
  50. event.add('summary', str(row['Home Team']) + f" {str(row['Unnamed: 4'])} " + str(row['Away Team.1']))
  51. event.add('description', f'Arrive by - {arrival_time}')
  52. event.add('dtstart', start_date_time)
  53. # End 2 hours after start_date_time
  54. event.add('dtend', start_date_time + timedelta(hours=2))
  55. event.add('dtstamp', start_date_time)
  56. event.add('uid', str(uuid.uuid4()))
  57. event.add('location', str(row['Venue']))
  58. cal.add_component(event)
  59. write_calendar(cal)
  60. cal = Calendar()
  61. cal.add('prodid', 'Down Grange Pumas Fixtures')
  62. cal.add('version', '2.0')
  63. 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"
  64. df = pd.read_html(url)[0]
  65. df.head()
  66. exists = does_csv_exist()
  67. if exists:
  68. no_change = compare_dfs(df)
  69. if not no_change:
  70. print("Fixtures updated, ical updated")
  71. store_df_as_csv(df)
  72. create_ical_file(df, cal)
  73. else:
  74. print("Fixtures not updated, no update to ical")
  75. else:
  76. store_df_as_csv(df)
  77. create_ical_file(df, cal)
  78. print("New ical file created")