__main__.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Main app"""
  2. import time
  3. from app import scheduler, session
  4. from app.api import get_citizens, get_residents
  5. from app.database import get_state_regions, save_citizens, save_residents
  6. def print_players(players):
  7. """Print professors"""
  8. for player in players:
  9. print('{:20} {:30}'.format(
  10. player['id'],
  11. player['name'],
  12. ))
  13. def job_update_citizens(state_id):
  14. """Update citizens"""
  15. regions = get_state_regions(state_id)
  16. for region in regions:
  17. citizens = get_citizens(region.id)
  18. print_players(citizens)
  19. save_citizens(region.id, citizens)
  20. def job_update_residents(state_id):
  21. """Update residents"""
  22. regions = get_state_regions(state_id)
  23. for region in regions:
  24. residents = get_residents(region.id)
  25. print_players(residents)
  26. save_residents(region.id, residents)
  27. def add_update_citizens(state_id):
  28. """Add jobs"""
  29. scheduler.add_job(
  30. job_update_citizens,
  31. 'cron',
  32. args=[state_id],
  33. id='citizens_{}'.format(state_id),
  34. replace_existing=True,
  35. hour='1,3,5,7,9,11,13,15,17,19,21,23'
  36. )
  37. def add_update_residents(state_id):
  38. """Add jobs"""
  39. scheduler.add_job(
  40. job_update_residents,
  41. 'cron',
  42. args=[state_id],
  43. id='citizens_{}'.format(state_id),
  44. replace_existing=True,
  45. hour='1,7,13,19'
  46. )
  47. if __name__ == '__main__':
  48. # jobs
  49. # job_update_citizens(2788)
  50. # job_update_residents(2788)
  51. # Verenigde Nederlanden
  52. add_update_citizens(2788)
  53. add_update_residents(2788)
  54. # Belgium
  55. add_update_citizens(2604)
  56. add_update_residents(2604)
  57. # De Provincien
  58. add_update_citizens(2620)
  59. add_update_residents(2620)
  60. try:
  61. while True:
  62. time.sleep(100)
  63. except KeyboardInterrupt:
  64. print('Exiting application')
  65. session.close()
  66. exit()