model.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import datetime
  2. import re
  3. import pytz
  4. import _html
  5. import _bs
  6. import database
  7. import psycopg2.extras
  8. from pprint import pprint
  9. local_timezones = {
  10. "NSW": "Australia/Sydney",
  11. "VIC": "Australia/Melbourne",
  12. "QLD": "Australia/Brisbane",
  13. "WA": "Australia/Perth",
  14. "SA": "Australia/Adelaide",
  15. "TAS": "Australia/Hobart",
  16. "ACT": "Australia/Sydney",
  17. "NT": "Australia/Darwin"}
  18. def scrape_main_page(row=-1):
  19. """
  20. Scrapes the main page of Racing Australia and returns the
  21. venues for this weeks races
  22. :param row:
  23. :return list of RaceDayShort namedtuples:
  24. """
  25. this_url = """https://racingaustralia.horse/Home.aspx"""
  26. this_data = _html.get_page(this_url)
  27. venues_all = _bs.get_today_row(this_data, row)
  28. return venues_all
  29. def convert_to_date(weird_string):
  30. """
  31. Converts a string like 'MONDAY 15 JUL' to a python datetime object
  32. :param weird_string:
  33. :return datetime object:
  34. """
  35. weird_string = re.sub(r' (\d) ', ' 0\1 ', weird_string)
  36. local_timezone = pytz.timezone('Australia/Sydney')
  37. now = datetime.datetime.now(local_timezone)
  38. calculated_date = datetime.datetime.strptime(str(now.year) + ' ' + weird_string, "%Y %A %d %b").date()
  39. # print(calculated_date)
  40. return calculated_date
  41. def convert_to_tz_aware_datetime(date, time, state):
  42. """
  43. Creates a datetime object to be stored as timestamptz in PostgreSQL
  44. :param date:
  45. :param time:
  46. :param state:
  47. :return timestamp:
  48. """
  49. tz = pytz.timezone(local_timezones[state])
  50. am_or_pm = time[-2:].lower()
  51. # print(am_or_pm)
  52. time_match = re.match(r'^(\d{1,2}):(\d{2})[AP]M$', time)
  53. hour = 0
  54. minute = 0
  55. if time_match:
  56. hour = int(time_match.group(1))
  57. minute = int(time_match.group(2))
  58. if am_or_pm == 'pm':
  59. hour = (hour % 12) + 12
  60. timestamp = datetime.datetime(date.year, date.month, date.day, hour, minute, 0, 0)
  61. locale_aware_timestamp = tz.localize(timestamp)
  62. return locale_aware_timestamp
  63. def get_program_data(this_url):
  64. """
  65. Retrieve the page from this_url
  66. :param this_url:
  67. :return:
  68. """
  69. program_page = _html.get_page(this_url)
  70. races = _bs.separate_races(program_page)
  71. pprint(races)
  72. return races
  73. def create_json(this_db):
  74. """
  75. Creates a json file with today's race data
  76. :return:
  77. """
  78. query = """
  79. SELECT venue, race, start_time, utctime, state
  80. FROM race_program
  81. WHERE race_date = %s;"""
  82. local_timezone = pytz.timezone('Australia/Sydney')
  83. now = datetime.datetime.now(local_timezone)
  84. cursor = this_db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
  85. def setup_database():
  86. """
  87. Set up for the database table
  88. :return:
  89. """
  90. query = """
  91. CREATE TABLE IF NOT EXISTS race_program (
  92. id SERIAL,
  93. race_date DATE NOT NULL,
  94. venue TEXT NOT NULL,
  95. state TEXT,
  96. race INTEGER,
  97. start_time TIME,
  98. utctime TIMESTAMP WITH TIME ZONE,
  99. UNIQUE (race_date, venue, race));
  100. """
  101. db = database.db
  102. cursor = db.cursor()
  103. cursor.execute(query)
  104. db.commit()
  105. cursor.close()
  106. db.close()