main.py 3.8 KB

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