| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import psycopg2.extras
- import model
- # from pprint import pprint
- import time
- import database
- import sys
- import view
- if __name__ == '__main__':
- row = -1
- if len(sys.argv) > 1:
- try:
- row = int(sys.argv[1])
- except ValueError:
- sys.exit(1)
- start = time.time()
- db = database.db
- race_days_global = model.scrape_main_page(row)
- interim = time.time()
- # print('interim 1 {}'.format(interim - start))
- # pprint(race_days_global)
- race_days = []
- raw_data_dict = {}
- for race_day in race_days_global:
- raw_data = model.get_raw_scratchings(race_day)
- race_day_details = model.process_raw_data(raw_data, race_day)
- race_days.append(race_day_details)
- raw_data_dict[race_day.name] = raw_data
- interim = time.time()
- # print('interim 2 {}'.format(interim - start))
- # pprint(race_days)
- cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
- for race_day in race_days:
- if not raw_data_dict or not race_day:
- # raw_data_dict may be empty when there is no data available (yet)
- continue
- raw_data = raw_data_dict[race_day.name]
- scratchings = model.get_scratching_details(raw_data, race_day)
- if not scratchings:
- # model.get_scratchings_details may return empty
- continue
- # retrieve previous stored scratching for this venue / day
- query = "SELECT * FROM horses WHERE venue = %s AND race_date = %s;"
- cursor.execute(query, (race_day.name, race_day.date))
- db_data = cursor.fetchall()
- # compare retrieved scratchings with new data
- for scratching in scratchings:
- found_in_database = False
- for row in db_data:
- # print(row)
- if (
- scratching.date == row.race_date and
- scratching.venue == row.venue and
- scratching.race == row.race and
- scratching.horse == row.horse
- ):
- message = 'Horse found: date = {}, venue = {}, race = {}, horse = {}'.format(scratching.date,
- scratching.venue,
- scratching.race,
- scratching.horse)
- # print(message)
- found_in_database = True
- if not found_in_database:
- # report new scratching
- message = 'New scratching: date = {} venue = {}-{} race = {} horse = {}'.format(scratching.date,
- scratching.state,
- scratching.venue,
- scratching.race,
- scratching.horse)
- print(message)
- view.broadcast(message)
- # store new scratching
- query = "INSERT INTO horses(venue, race_date, race, horse) VALUES(%s, %s, %s, %s)"
- cursor.execute(query, (scratching.venue, scratching.date,
- scratching.race, scratching.horse))
- db.commit()
- cursor.close()
- db.close()
- interim = time.time()
- # print('interim 3 {}'.format(interim - start))
|