model.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import re
  2. import _html
  3. import _bs
  4. import pytz
  5. import datetime
  6. # import time
  7. # import psycopg2.extras
  8. """
  9. Modules _html and _bs4 contain specialized methods.
  10. """
  11. local_timezones = {
  12. "NSW": "Australia/Sydney",
  13. "VIC": "Australia/Melbourne",
  14. "QLD": "Australia/Brisbane",
  15. "WA": "Australia/Perth",
  16. "SA": "Australia/Adelaide",
  17. "TAS": "Australia/Hobart",
  18. "ACT": "Australia/Sydney",
  19. "NT": "Australia/Darwin"}
  20. def scrape_main_page(row):
  21. this_url = """https://racingaustralia.horse/Home.aspx"""
  22. this_data = _html.get_page(this_url)
  23. venues_all = _bs.get_today_row(this_data, row)
  24. return venues_all
  25. def get_raw_scratchings(this_venue):
  26. this_raw_data = _html.get_page(this_venue.scratchings_url)
  27. return this_raw_data
  28. def process_raw_data(this_raw_data, this_venue):
  29. """
  30. Processes the raw data from the Scratchings page to obtain meta data.
  31. this_venue is passed to _bs.process_scratchings() to create the inherited namedTuple
  32. :param this_raw_data:
  33. :param this_venue:
  34. :return:
  35. """
  36. race_day_info = _bs.get_meta_data(this_raw_data, this_venue)
  37. return race_day_info
  38. def get_scratching_details(this_raw_data, this_venue):
  39. # this_data = _html.get_page(this_venue.scratchings_url)
  40. scratchings_info = _bs.process_scratchings(this_raw_data, this_venue)
  41. return scratchings_info
  42. def convert_to_unixtime(dt_object):
  43. """
  44. Simple utility function that returns the unixtime from a timezone aware dateTime object
  45. :param dt_object:
  46. :return:
  47. """
  48. utc = pytz.UTC
  49. d = dt_object.astimezone(utc)
  50. epoch = datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=pytz.UTC)
  51. ts = int((d - epoch).total_seconds())
  52. return ts
  53. def convert_to_date(weird_string):
  54. """
  55. Converts a string like 'MONDAY 15 JUL' to a python datetime object
  56. :param weird_string:
  57. :return datetime object:
  58. """
  59. weird_string = re.sub(r' (\d) ', ' 0\1 ', weird_string)
  60. local_timezone = pytz.timezone('Australia/Sydney')
  61. now = datetime.datetime.now(local_timezone)
  62. calculated_date = datetime.datetime.strptime(str(now.year) + ' ' + weird_string, "%Y %A %d %b").date()
  63. print(calculated_date)
  64. return calculated_date