racenet.py 3.0 KB

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