runner.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from .profile import RemoteProfile, DBProfile
  2. from .credential import CredentialManager
  3. from .stat import CompetitiveStat
  4. from .utils.db import Mongo
  5. from bson import ObjectId
  6. import logging
  7. logger = logging.getLogger(__name__)
  8. class Runner:
  9. callbacks = []
  10. def __init__(self, _id: ObjectId):
  11. self._id: ObjectId = _id
  12. # Plugs
  13. # self.profile: Profile = Profile(self)
  14. self.new_profile = RemoteProfile(self)
  15. self.latest_profile = DBProfile(self)
  16. self.credential: CredentialManager = CredentialManager(self)
  17. self.competitive_stat = CompetitiveStat(self)
  18. def run(self):
  19. self.new_profile.refresh()
  20. self.latest_profile.refresh()
  21. if self.new_profile > self.latest_profile:
  22. logger.info("[%s] Found New Profile!", self._id)
  23. Mongo.db.profile.insert(self.new_profile.to_db_record())
  24. self.competitive_stat.calc()
  25. class RunnerProxy(Runner):
  26. def __init__(self, username: str):
  27. record = Mongo.db.user.find_one({'username': username})
  28. if not record:
  29. raise Exception('No Such User')
  30. self.username: str = username
  31. super(RunnerProxy, self).__init__(record['_id'])