main.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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)
  54. if (
  55. # scratching.date == row.race_date and
  56. # scratching.venue == row.venue and
  57. # scratching.race == row.race and
  58. scratching.horse == row.horse
  59. ):
  60. message = 'Horse found: date = {}, venue = {}, race = {}, horse = {}'.format(scratching.date,
  61. scratching.venue,
  62. scratching.race,
  63. scratching.horse)
  64. print(message)
  65. print(row)
  66. found_in_database = True
  67. if not found_in_database:
  68. # report new scratching
  69. date_object = datetime.datetime.strptime(scratching.date, "%Y-%m-%d").date()
  70. day_abbr = date_object.strftime('%a')
  71. message = 'New scratching: venue = {} {} {}-{} | race = {} | horse = {}'.format(day_abbr,
  72. scratching.date,
  73. scratching.state,
  74. scratching.venue,
  75. scratching.race,
  76. scratching.horse)
  77. print(message)
  78. if broadcast:
  79. view.broadcast(message)
  80. # store new scratching
  81. query = "INSERT INTO horses(venue, race_date, race, horse) VALUES(%s, %s, %s, %s)"
  82. cursor.execute(query, (scratching.venue, scratching.date,
  83. scratching.race, scratching.horse))
  84. db.commit()
  85. time.sleep(0.5)
  86. cursor.close()
  87. db.close()
  88. interim = time.time()
  89. # print('interim 3 {}'.format(interim - start))