main.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import datetime
  2. import psycopg2.extras
  3. import model
  4. # from pprint import pprint
  5. import time
  6. import database
  7. import sys
  8. import view
  9. if __name__ == '__main__':
  10. row = -1
  11. if len(sys.argv) > 1:
  12. try:
  13. row = int(sys.argv[1])
  14. except ValueError:
  15. sys.exit(1)
  16. broadcast = True
  17. if len(sys.argv) > 2:
  18. broadcast = False
  19. start = time.time()
  20. db = database.db
  21. race_days_global = model.scrape_main_page(row)
  22. interim = time.time()
  23. # print('interim 1 {}'.format(interim - start))
  24. # pprint(race_days_global)
  25. race_days = []
  26. raw_data_dict = {}
  27. for race_day in race_days_global:
  28. raw_data = model.get_raw_scratchings(race_day)
  29. race_day_details = model.process_raw_data(raw_data, race_day)
  30. race_days.append(race_day_details)
  31. raw_data_dict[race_day.name] = raw_data
  32. interim = time.time()
  33. # print('interim 2 {}'.format(interim - start))
  34. # pprint(race_days)
  35. cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
  36. for race_day in race_days:
  37. if not raw_data_dict or not race_day:
  38. # raw_data_dict may be empty when there is no data available (yet)
  39. continue
  40. raw_data = raw_data_dict[race_day.name]
  41. scratchings = model.get_scratching_details(raw_data, race_day)
  42. if not scratchings:
  43. # model.get_scratchings_details may return empty
  44. continue
  45. # retrieve previous stored scratching for this venue / day
  46. query = "SELECT * FROM horses WHERE venue = %s AND race_date = %s;"
  47. cursor.execute(query, (race_day.name, race_day.date))
  48. db_data = cursor.fetchall()
  49. # compare retrieved scratchings with new data
  50. for scratching in scratchings:
  51. found_in_database = False
  52. for row in db_data:
  53. # print('row: ', end = '')
  54. # print(type(row))
  55. # print(row)
  56. # if type(row) == 'datetime.time':
  57. # continue
  58. if (
  59. scratching.date == row.race_date.strftime('%Y-%m-%d') and
  60. scratching.venue == row.venue and
  61. int(scratching.race) == int(row.race) and
  62. scratching.horse == row.horse
  63. ):
  64. message_string = 'Horse found: date = {}, venue = {}, race = {}, horse = {}'
  65. message = message_string.format(scratching.date,
  66. scratching.venue,
  67. scratching.race,
  68. scratching.horse)
  69. # print(message)
  70. # print(row)
  71. found_in_database = True
  72. if not found_in_database:
  73. # report new scratching
  74. date_object = datetime.datetime.strptime(scratching.date, "%Y-%m-%d").date()
  75. day_abbr = date_object.strftime('%a')
  76. query = """
  77. SELECT start_time, utctime, torn FROM race_program
  78. WHERE race_date = %s AND
  79. venue = %s AND
  80. race = %s;
  81. """
  82. cursor.execute(query, (scratching.date, scratching.venue, scratching.race))
  83. db_data = cursor.fetchone()
  84. flag = ''
  85. if db_data.torn:
  86. flag = 'FLAGGED!! '
  87. message_string = '{}venue = {} {} {}-{} | race = {} starts at {} | {} UTC | horse = {}'
  88. message = message_string.format(flag,
  89. day_abbr,
  90. scratching.date,
  91. scratching.state,
  92. scratching.venue,
  93. scratching.race,
  94. db_data.start_time.strftime('%H:%M'),
  95. db_data.utctime.strftime('%H:%M'),
  96. scratching.horse)
  97. print(message)
  98. if broadcast:
  99. view.broadcast(message)
  100. # store new scratching
  101. query = "INSERT INTO horses(venue, race_date, race, horse) VALUES(%s, %s, %s, %s)"
  102. cursor.execute(query, (scratching.venue, scratching.date,
  103. scratching.race, scratching.horse))
  104. db.commit()
  105. time.sleep(0.5)
  106. cursor.close()
  107. db.close()
  108. interim = time.time()
  109. # print('interim 3 {}'.format(interim - start))