main.py 3.2 KB

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