__main__.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Main app"""
  2. import time
  3. from app import SCHEDULER, LOGGER
  4. from app.api import get_factories
  5. from app.database import get_state, get_regions, save_factories
  6. def print_factories(factories):
  7. """Print professors"""
  8. for factory in factories:
  9. print(factory)
  10. def job_update_factories(state_id):
  11. """Update factories"""
  12. LOGGER.info('Run update factories for state "%s"', state_id)
  13. state = get_state(state_id)
  14. LOGGER.info('"%s": get regions', state.name)
  15. regions = get_regions(state.id)
  16. factory_list = []
  17. for region in regions:
  18. LOGGER.info('"%s": get factories', region.name)
  19. factories = get_factories(region.id)
  20. LOGGER.info('"%s": "%s" factories', region.name, len(factories))
  21. factory_list += factories
  22. save_factories(state_id, factory_list)
  23. LOGGER.info('"%s": done saving factories', state.name)
  24. def add_update_factories(state_id):
  25. """Add jobs"""
  26. SCHEDULER.add_job(
  27. job_update_factories,
  28. 'cron',
  29. args=[state_id],
  30. id='factories_{}'.format(state_id),
  31. replace_existing=True,
  32. hour='2,14'
  33. )
  34. if __name__ == '__main__':
  35. job_update_factories(2788)
  36. # jobs
  37. # Verenigde Nederlanden
  38. add_update_factories(2788)
  39. # Belgium
  40. add_update_factories(2604)
  41. # De Provincien
  42. add_update_factories(2620)
  43. try:
  44. while True:
  45. time.sleep(100)
  46. except KeyboardInterrupt:
  47. LOGGER.info('Exiting application')
  48. SCHEDULER.shutdown()
  49. exit()