main.py 3.2 KB

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