racenet.py 3.2 KB

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