gatherMarket.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from market import Market
  2. from player import Player
  3. import marketMonitor
  4. import schedule
  5. import time
  6. from datetime import datetime
  7. from logger import logger
  8. import argparse
  9. import sys
  10. def parse_args():
  11. parser = argparse.ArgumentParser(
  12. formatter_class=argparse.RawDescriptionHelpFormatter,
  13. description="Gather Market Data Script",
  14. epilog="",
  15. )
  16. parser.add_argument(
  17. "-p",
  18. "--player",
  19. action='store_true',
  20. help="Collect player data?"
  21. )
  22. parser.add_argument(
  23. "-l",
  24. "--login",
  25. action='store_true',
  26. help="Login to Browser?"
  27. )
  28. return parser.parse_args()
  29. def create_market(login):
  30. return Market(login=login)
  31. def create_player():
  32. return Player()
  33. def main():
  34. try:
  35. args = parse_args()
  36. except argparse.ArgumentError as e:
  37. logger.error(f"Argument parsing error: {e}")
  38. sys.exit(1)
  39. m = create_market(login=args.login)
  40. p = create_player()
  41. merr = 0
  42. perr = 0
  43. def jobM():
  44. nonlocal m, merr
  45. logger.info("Gathering Market Data.")
  46. try:
  47. m.fetchData()
  48. m.commit()
  49. logger.info("Market data gathering complete.")
  50. except Exception as e:
  51. logger.error(f"Market data gathering failed: {e}")
  52. merr += 1
  53. if merr >= 3:
  54. merr = 0
  55. m = create_market(login=args.login)
  56. def jobP():
  57. nonlocal p, perr
  58. logger.info("Gathering Player Data.")
  59. try:
  60. p.fetchData()
  61. p.commit()
  62. logger.info("Player data gathering complete.")
  63. except Exception as e:
  64. logger.error(f"Player data gathering failed: {e}")
  65. perr += 1
  66. if perr >= 3:
  67. perr = 0
  68. p = create_player()
  69. schedule.every(60).minutes.do(jobM)
  70. jobM()
  71. if args.player:
  72. schedule.every(60).minutes.do(jobP)
  73. jobP()
  74. try:
  75. while True:
  76. schedule.run_pending()
  77. time.sleep(1)
  78. except KeyboardInterrupt:
  79. logger.info("Script terminated by user.")
  80. sys.exit()
  81. if __name__ == "__main__":
  82. main()