| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import json
- from pprint import pprint
- from model import local_timezones
- import arrow
- # import collections
- import _bs
- import model
- # import re
- import textwrap
- import database
- import time
- # Data = collections.namedtuple('Data', 'venue state race time horse_no horse flag')
- start = time.time()
- my_json = model.scrape_racenet_scratchings_page()
- my_json = textwrap.fill(my_json[:-1], 1e6)
- # print(my_json)
- # exit(0)
- # with open('scratchings.json', 'r') as f:
- data_all = json.loads(my_json)
- # pprint(data_all)
- data = data_all["MainPageState"]["receivedData"][0]
- name = data['name']
- # print(name)
- items = 0
- for d in data['data']:
- items += 1
- # pprint(d)
- # print('item {}'.format(items))
- country = d['venue']['countryId']
- is_aus = True if country == 'AUS' else False
- timezone = local_timezones[d['venue']['state']] if is_aus else 'Pacific/Auckland'
- venue_name = d['venue']['name']
- if venue_name == 'Ballarat Synthetic':
- venue_name = 'Sportsbet-Ballarat Synthetic'
- if venue_name == 'Moonee Valley':
- venue_name = 'The Valley'
- if venue_name == 'Gold Coast':
- venue_name = 'Aquis Park Gold Coast'
- if venue_name == 'Sandown Lakeside':
- venue_name = 'Ladbrokes Park Lakeside'
- # venue = '{} {}{} {}'.format(venue_name, d['venue']['countryId'],
- # '/' + d['venue']['state'] if is_aus else '',
- # timezone)
- # print(venue)
- races = d['races']
- scratchings = []
- for race in races:
- raceNumber = race['raceNumber']
- startTimeLocal = race['startTimeLocal'][11:-3]
- # print(race['startTimeLocal'])
- arrowStartTimeLocal = arrow.get(race['startTimeLocal']+' '+timezone, 'YYYY-MM-DDTHH:mm:ss ZZZ')
- # print(arrowStartTimeLocal)
- startTimeUtc = race['startTimeUtc']
- race_date = race['startTimeLocal'][:10]
- # print(race_date)
- # print('race {:2d}: {} {}'.format(raceNumber, startTimeLocal, startTimeUtc))
- for runner in race['runners']:
- if runner['scratched']:
- horseDisplayName = runner['horseDisplayName']
- horseNumber = runner['horseNumber']
- # print('\t{:2d}: {}'.format(horseNumber, horseDisplayName))
- # data_without_flag = Data(d['venue']['name'], d['venue']['state'] if is_aus else 'NZL',
- # raceNumber, arrowStartTimeLocal,
- # horseNumber, horseDisplayName, False)
- data_without_flag = _bs.Scratching(venue_name, d['venue']['state'] if is_aus else 'NZL',
- arrowStartTimeLocal.date(), raceNumber,
- arrowStartTimeLocal.format('HH:mm'),
- arrowStartTimeLocal.to('utc').format('HH:mm'), horseNumber,
- horseDisplayName, False)
- scratchings.append(data_without_flag)
- if arrow.utcnow().minute % 20 == 0:
- pprint(scratchings)
- db = database.db
- scratchings_to_be_broadcast = model.store_scratched_horses(db, scratchings)
- # print('len(scratchings_to_be_broadcast): {}'.format(len(scratchings_to_be_broadcast)))
- model.send_messages(scratchings_to_be_broadcast, 'RN', start)
|