Advertisement
kopyl

Untitled

Apr 23rd, 2022 (edited)
1,304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. import pymongo
  2. from dynaconf import Dynaconf
  3.  
  4.  
  5. settings = Dynaconf(
  6.     settings_files=['settings.yaml']
  7. )
  8.  
  9.  
  10. client = pymongo.MongoClient(
  11.     f"mongodb://"
  12.     f"{settings.mongo.username}:{settings.mongo.password}@"
  13.     f"{settings.mongo.host}:{settings.mongo.port}"
  14. )
  15.  
  16.  
  17. db = client["idied"]
  18. users_collection = db["authorized_users"]
  19. notes_collection = db["notes"]
  20.  
  21.  
  22. date_expression = (
  23.     "Math.floor(Date.now() / 1000) - "
  24.     "this.last_online_timestamp > 60"
  25. )
  26.  
  27.  
  28. def get_users_away_for_30_days() -> list:
  29.     users = users_collection.find(
  30.         {
  31.             "$where": date_expression,
  32.             "has_died": False
  33.         },
  34.         {"_id": 0, "user_id": 1}
  35.     )
  36.     users = list(users)
  37.     users = [ user.values() for user in users ]
  38.     users = [ list(user) for user in users ]
  39.     users = [ user[0] for user in users ]
  40.     return users
  41.  
  42.  
  43. def update_databases(users_away_for_30_days):
  44.     if not users_away_for_30_days:
  45.         return
  46.  
  47.     notes_collection.update_many(
  48.         {
  49.             "user_id": {"$in": users_away_for_30_days}
  50.         },
  51.         {
  52.             "$set": {
  53.                 "is_accessible": True
  54.             }
  55.         }
  56.     )
  57.  
  58.     users_collection.update_many(
  59.         {
  60.             "user_id": {"$in": users_away_for_30_days}
  61.         },
  62.         {
  63.             "$set": {
  64.                 "has_died": True
  65.             }
  66.         }
  67.     )
  68.  
  69.  
  70. if __name__ == "__main__":
  71.     print("Launching...")
  72.     users_away_for_30_days = get_users_away_for_30_days()
  73.     update_databases(users_away_for_30_days)
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement