racenet.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. if venue_name == 'Sandown Lakeside':
  38. venue_name = 'Ladbrokes Park Lakeside'
  39. # venue = '{} {}{} {}'.format(venue_name, d['venue']['countryId'],
  40. # '/' + d['venue']['state'] if is_aus else '',
  41. # timezone)
  42. # print(venue)
  43. races = d['races']
  44. scratchings = []
  45. for race in races:
  46. raceNumber = race['raceNumber']
  47. startTimeLocal = race['startTimeLocal'][11:-3]
  48. # print(race['startTimeLocal'])
  49. arrowStartTimeLocal = arrow.get(race['startTimeLocal']+' '+timezone, 'YYYY-MM-DDTHH:mm:ss ZZZ')
  50. # print(arrowStartTimeLocal)
  51. startTimeUtc = race['startTimeUtc']
  52. race_date = race['startTimeLocal'][:10]
  53. # print(race_date)
  54. # print('race {:2d}: {} {}'.format(raceNumber, startTimeLocal, startTimeUtc))
  55. for runner in race['runners']:
  56. if runner['scratched']:
  57. horseDisplayName = runner['horseDisplayName']
  58. horseNumber = runner['horseNumber']
  59. # print('\t{:2d}: {}'.format(horseNumber, horseDisplayName))
  60. # data_without_flag = Data(d['venue']['name'], d['venue']['state'] if is_aus else 'NZL',
  61. # raceNumber, arrowStartTimeLocal,
  62. # horseNumber, horseDisplayName, False)
  63. data_without_flag = _bs.Scratching(venue_name, d['venue']['state'] if is_aus else 'NZL',
  64. arrowStartTimeLocal.date(), raceNumber,
  65. arrowStartTimeLocal.format('HH:mm'),
  66. arrowStartTimeLocal.to('utc').format('HH:mm'), horseNumber,
  67. horseDisplayName, False)
  68. scratchings.append(data_without_flag)
  69. if arrow.utcnow().minute % 20 == 0:
  70. pprint(scratchings)
  71. db = database.db
  72. scratchings_to_be_broadcast = model.store_scratched_horses(db, scratchings)
  73. # print('len(scratchings_to_be_broadcast): {}'.format(len(scratchings_to_be_broadcast)))
  74. model.send_messages(scratchings_to_be_broadcast, 'RN')