racenet.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import json
  2. from pprint import pprint
  3. from model import local_timezones
  4. import arrow
  5. # import collections
  6. import _bs
  7. import model
  8. # import re
  9. import textwrap
  10. import database
  11. import time
  12. # Data = collections.namedtuple('Data', 'venue state race time horse_no horse flag')
  13. start = time.time()
  14. my_json = model.scrape_racenet_scratchings_page()
  15. my_json = textwrap.fill(my_json[:-1], 1e6)
  16. # print(my_json)
  17. # exit(0)
  18. # with open('scratchings.json', 'r') as f:
  19. data_all = json.loads(my_json)
  20. # pprint(data_all)
  21. data = data_all["MainPageState"]["receivedData"][0]
  22. name = data['name']
  23. # print(name)
  24. items = 0
  25. for d in data['data']:
  26. items += 1
  27. # pprint(d)
  28. # print('item {}'.format(items))
  29. country = d['venue']['countryId']
  30. is_aus = True if country == 'AUS' else False
  31. timezone = local_timezones[d['venue']['state']] if is_aus else 'Pacific/Auckland'
  32. venue_name = d['venue']['name']
  33. if venue_name == 'Ballarat Synthetic':
  34. venue_name = 'Sportsbet-Ballarat Synthetic'
  35. if venue_name == 'Moonee Valley':
  36. venue_name = 'The Valley'
  37. if venue_name == 'Gold Coast':
  38. venue_name = 'Aquis Park Gold Coast'
  39. if venue_name == 'Sandown Lakeside':
  40. venue_name = 'Ladbrokes Park Lakeside'
  41. # venue = '{} {}{} {}'.format(venue_name, d['venue']['countryId'],
  42. # '/' + d['venue']['state'] if is_aus else '',
  43. # timezone)
  44. # print(venue)
  45. races = d['races']
  46. scratchings = []
  47. for race in races:
  48. raceNumber = race['raceNumber']
  49. startTimeLocal = race['startTimeLocal'][11:-3]
  50. # print(race['startTimeLocal'])
  51. arrowStartTimeLocal = arrow.get(race['startTimeLocal']+' '+timezone, 'YYYY-MM-DDTHH:mm:ss ZZZ')
  52. # print(arrowStartTimeLocal)
  53. startTimeUtc = race['startTimeUtc']
  54. race_date = race['startTimeLocal'][:10]
  55. # print(race_date)
  56. # print('race {:2d}: {} {}'.format(raceNumber, startTimeLocal, startTimeUtc))
  57. for runner in race['runners']:
  58. if runner['scratched']:
  59. horseDisplayName = runner['horseDisplayName']
  60. horseNumber = runner['horseNumber']
  61. # print('\t{:2d}: {}'.format(horseNumber, horseDisplayName))
  62. # data_without_flag = Data(d['venue']['name'], d['venue']['state'] if is_aus else 'NZL',
  63. # raceNumber, arrowStartTimeLocal,
  64. # horseNumber, horseDisplayName, False)
  65. data_without_flag = _bs.Scratching(venue_name, d['venue']['state'] if is_aus else 'NZL',
  66. arrowStartTimeLocal.date(), raceNumber,
  67. arrowStartTimeLocal.format('HH:mm'),
  68. arrowStartTimeLocal.to('utc'), horseNumber,
  69. horseDisplayName, False)
  70. scratchings.append(data_without_flag)
  71. if arrow.utcnow().minute % 20 == 0:
  72. pprint(scratchings)
  73. db = database.db
  74. scratchings_to_be_broadcast = model.store_scratched_horses(db, scratchings)
  75. # print('len(scratchings_to_be_broadcast): {}'.format(len(scratchings_to_be_broadcast)))
  76. model.send_messages(scratchings_to_be_broadcast, 'RN', start)