|
@@ -6,6 +6,9 @@ import pytz
|
|
|
import datetime
|
|
import datetime
|
|
|
# import time
|
|
# import time
|
|
|
import psycopg2.extras
|
|
import psycopg2.extras
|
|
|
|
|
+from pprint import pprint
|
|
|
|
|
+import arrow
|
|
|
|
|
+
|
|
|
|
|
|
|
|
import view
|
|
import view
|
|
|
|
|
|
|
@@ -83,49 +86,110 @@ def convert_to_date(weird_string):
|
|
|
return calculated_date
|
|
return calculated_date
|
|
|
|
|
|
|
|
|
|
|
|
|
-def send_messages(messages):
|
|
|
|
|
|
|
+def send_messages(scratches, source):
|
|
|
long_message = ''
|
|
long_message = ''
|
|
|
- leftover_message = ''
|
|
|
|
|
- for this_message in messages:
|
|
|
|
|
- print('this_message: {}'.format(this_message))
|
|
|
|
|
- leftover_message = this_message
|
|
|
|
|
|
|
+
|
|
|
|
|
+ message_string = '{} {}venue = {} {} {}-{} | race = {} starts at {} | {} UTC | horse = {}'
|
|
|
|
|
+
|
|
|
|
|
+ for m in scratches:
|
|
|
|
|
+ flag = ''
|
|
|
|
|
+ if m.torn:
|
|
|
|
|
+ flag = 'FLAGGED!! '
|
|
|
|
|
+ message = message_string.format(source,
|
|
|
|
|
+ flag,
|
|
|
|
|
+ m.date.format('%a'),
|
|
|
|
|
+ m.date,
|
|
|
|
|
+ m.state,
|
|
|
|
|
+ m.venue,
|
|
|
|
|
+ m.race,
|
|
|
|
|
+ m.time.strftime('%H:%M'),
|
|
|
|
|
+ m.utc.strftime('%H:%M'),
|
|
|
|
|
+ '{} {}'.format(m.horse_no, m.horse_display_name))
|
|
|
|
|
+ print('this_message: {}'.format(message))
|
|
|
# Append message if possible
|
|
# Append message if possible
|
|
|
- if len(long_message) + len(this_message) < 5997:
|
|
|
|
|
|
|
+ if len(long_message) + len(message) < 5997:
|
|
|
if len(long_message) == 0:
|
|
if len(long_message) == 0:
|
|
|
- long_message = this_message
|
|
|
|
|
|
|
+ long_message = message
|
|
|
else:
|
|
else:
|
|
|
- long_message += '\n' + this_message
|
|
|
|
|
- leftover_message = ''
|
|
|
|
|
|
|
+ long_message += '\n' + message
|
|
|
else:
|
|
else:
|
|
|
# Send long message (max 6k characters)
|
|
# Send long message (max 6k characters)
|
|
|
print('Sending very long message > {}'.format(len(long_message)))
|
|
print('Sending very long message > {}'.format(len(long_message)))
|
|
|
view.broadcast(long_message)
|
|
view.broadcast(long_message)
|
|
|
# Best would be to now store horses that were just broadcast
|
|
# Best would be to now store horses that were just broadcast
|
|
|
- long_message = this_message
|
|
|
|
|
- leftover_message = ''
|
|
|
|
|
|
|
+ long_message = m
|
|
|
|
|
|
|
|
# Send all messages
|
|
# Send all messages
|
|
|
if len(long_message) > 0:
|
|
if len(long_message) > 0:
|
|
|
print('Sending long_message > {}'.format(len(long_message)))
|
|
print('Sending long_message > {}'.format(len(long_message)))
|
|
|
view.broadcast(long_message)
|
|
view.broadcast(long_message)
|
|
|
- long_message = ''
|
|
|
|
|
-
|
|
|
|
|
- # Send only or last message
|
|
|
|
|
- if len(leftover_message) > 0:
|
|
|
|
|
- print('Sending leftover_message > {}'.format(len(leftover_message)))
|
|
|
|
|
- view.broadcast(leftover_message)
|
|
|
|
|
- leftover_message = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-def store_scratched_horses(horses, db):
|
|
|
|
|
- query = """INSERT INTO horses(venue, race_date, race, horse)
|
|
|
|
|
- VALUES(%s, %s, %s, %s)
|
|
|
|
|
- ON CONFLICT(venue, race_date, race, horse) DO NOTHING;"""
|
|
|
|
|
|
|
+def store_scratched_horses(db, full_scratches):
|
|
|
|
|
+ query = """INSERT INTO horses(venue, race_date, race, horse_no, horse_display_name)
|
|
|
|
|
+ VALUES(%s, %s, %s, %s, %s)
|
|
|
|
|
+ ON CONFLICT(venue, race_date, race, horse_no) DO NOTHING;"""
|
|
|
|
|
|
|
|
|
|
+ scratches_to_return = []
|
|
|
|
|
+ regex = r'^INSERT \d+ (\d+)$'
|
|
|
cur3 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
|
|
cur3 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
|
|
|
- for database_entry in horses:
|
|
|
|
|
|
|
+ for this_scratching in full_scratches:
|
|
|
|
|
+ database_entry = (this_scratching.venue, this_scratching.date,
|
|
|
|
|
+ this_scratching.race, this_scratching.horse_no,
|
|
|
|
|
+ this_scratching.horse_display_name)
|
|
|
cur3.execute(query, database_entry)
|
|
cur3.execute(query, database_entry)
|
|
|
print('Stored: {}'.format(database_entry))
|
|
print('Stored: {}'.format(database_entry))
|
|
|
print(cur3.statusmessage)
|
|
print(cur3.statusmessage)
|
|
|
|
|
+ match = re.match(regex, cur3.statusmessage)
|
|
|
|
|
+ status = 0
|
|
|
|
|
+ if match:
|
|
|
|
|
+ status = int(match.group(1))
|
|
|
|
|
+ if status > 0:
|
|
|
|
|
+ scratches_to_return.append(this_scratching)
|
|
|
|
|
+
|
|
|
cur3.close()
|
|
cur3.close()
|
|
|
db.commit()
|
|
db.commit()
|
|
|
|
|
+ return scratches_to_return
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def get_race_from_races(this_haystack, needle_date, needle_venue, needle_race):
|
|
|
|
|
+ """
|
|
|
|
|
+ From the previous acquired database data this will return 'start_time',
|
|
|
|
|
+ 'utctime' and torn (boolean) where the date, venue and race are given.
|
|
|
|
|
+ :param this_haystack:
|
|
|
|
|
+ :param needle_date:
|
|
|
|
|
+ :param needle_venue:
|
|
|
|
|
+ :param needle_race:
|
|
|
|
|
+ :return:
|
|
|
|
|
+ """
|
|
|
|
|
+ return_values = ()
|
|
|
|
|
+ needle_date = arrow.get(needle_date, 'YYYY-MM-DD').date()
|
|
|
|
|
+ for race in this_haystack:
|
|
|
|
|
+ # pprint(race)
|
|
|
|
|
+ # print(race.race_date == needle_date)
|
|
|
|
|
+ # print('race.race_date == needle_date: {} == {}'.format(race.race_date, needle_date))
|
|
|
|
|
+ # print('type(race.race_date) == type(needle_date): {} == {}'.format(type(race.race_date), type(needle_date)))
|
|
|
|
|
+ # print(race.venue == needle_venue)
|
|
|
|
|
+ # print(race.race == needle_race)
|
|
|
|
|
+ if ((race.race_date == needle_date) and (race.venue == needle_venue) and (
|
|
|
|
|
+ race.race == needle_race)):
|
|
|
|
|
+ return_values = (race.start_time, race.utctime, race.torn)
|
|
|
|
|
+ break
|
|
|
|
|
+ return return_values
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+def get_relevant_races_from_database(db):
|
|
|
|
|
+ query = """
|
|
|
|
|
+ SELECT venue, start_time, race_date, utctime, race, torn FROM race_program
|
|
|
|
|
+ WHERE race_date >= %s;
|
|
|
|
|
+ """
|
|
|
|
|
+ # Run this query once and use resulting NamedTuple for data
|
|
|
|
|
+ cur_races = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
|
|
|
|
|
+ today = arrow.utcnow().date()
|
|
|
|
|
+ print('today: {}'.format(today))
|
|
|
|
|
+ cur_races.execute(query, (today,))
|
|
|
|
|
+ races = cur_races.fetchall()
|
|
|
|
|
+ print('len(races): {}'.format(len(races)))
|
|
|
|
|
+ # pprint(races)
|
|
|
|
|
+ cur_races.close()
|
|
|
|
|
+ return races
|