main.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. print(' 54. len(db_data): {}'.format(len(db_data)))
  50. print(' 55. type(db_data): {}'.format(type(db_data)))
  51. pprint(db_data)
  52. # compare retrieved scratchings with new data
  53. for scratching in scratchings:
  54. found_in_database = False
  55. if db_data is None:
  56. print(' 62. Caught db_data is None')
  57. found_in_database = False
  58. else:
  59. for row in db_data:
  60. # print('row: ', end = '')
  61. # print(type(row))
  62. # print(row)
  63. # if type(row) == 'datetime.time':
  64. # continue
  65. try:
  66. if not hasattr(db_data, 'name'):
  67. continue
  68. date_in_db_row_formatted = row.race_date.strftime('%Y-%m-%d')
  69. if (
  70. scratching.date == date_in_db_row_formatted and
  71. scratching.venue == row.venue and
  72. int(scratching.race) == int(row.race) and
  73. scratching.horse == row.horse
  74. ):
  75. message_string = 'Horse found: date = {}, venue = {}, race = {}, horse = {}'
  76. message = message_string.format(scratching.date,
  77. scratching.venue,
  78. scratching.race,
  79. scratching.horse)
  80. # print(message)
  81. # print(row)
  82. found_in_database = True
  83. except AttributeError as ae:
  84. print(ae)
  85. print(' 89. row: ', end='')
  86. print(row)
  87. pprint(db_data)
  88. continue
  89. if not found_in_database:
  90. # report new scratching
  91. date_object = datetime.datetime.strptime(scratching.date, "%Y-%m-%d").date()
  92. day_abbr = date_object.strftime('%a')
  93. query = """
  94. SELECT start_time, utctime, torn FROM race_program
  95. WHERE race_date = %s AND
  96. venue = %s AND
  97. race = %s;
  98. """
  99. cursor.execute(query, (scratching.date, scratching.venue, scratching.race))
  100. db_data = cursor.fetchone()
  101. if db_data is None or len(db_data) == 0:
  102. print('106. cursor.execute(query, (scratching.date, scratching.venue, scratching.race))')
  103. print('107. cursor.execute({}, ({}, {}, {}))'.format(
  104. query, scratching.date, scratching.venue, scratching.race
  105. ))
  106. print('110. No race found')
  107. continue
  108. flag = ''
  109. if db_data.torn:
  110. flag = 'FLAGGED!! '
  111. message_string = '{}venue = {} {} {}-{} | race = {} starts at {} | {} UTC | horse = {}'
  112. message = message_string.format(flag,
  113. day_abbr,
  114. scratching.date,
  115. scratching.state,
  116. scratching.venue,
  117. scratching.race,
  118. db_data.start_time.strftime('%H:%M'),
  119. db_data.utctime.strftime('%H:%M'),
  120. scratching.horse)
  121. print(message)
  122. if broadcast:
  123. view.broadcast(message)
  124. # store new scratching
  125. query = """INSERT INTO horses(venue, race_date, race, horse)
  126. VALUES(%s, %s, %s, %s)
  127. ON CONFLICT(venue, race_date, race, horse) DO NOTHING;"""
  128. cursor.execute(query, (scratching.venue, scratching.date,
  129. scratching.race, scratching.horse))
  130. db.commit()
  131. print('Stored: {}'.format(scratching))
  132. time.sleep(0.5)
  133. cursor.close()
  134. db.close()
  135. interim = time.time()
  136. # print('interim 3 {}'.format(interim - start))