main.py 4.0 KB

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