2023-09-23 21:41:48 +02:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import asyncio
|
|
|
|
import json
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
2023-10-02 20:06:18 +02:00
|
|
|
MONGO_URI = os.getenv('MONGO_URI')
|
2023-09-23 21:41:48 +02:00
|
|
|
|
|
|
|
async def log_rated_key(key: str) -> None:
|
|
|
|
"""Logs a key that has been rate limited to the database."""
|
|
|
|
|
|
|
|
client = AsyncIOMotorClient(MONGO_URI)
|
|
|
|
|
|
|
|
scheme = {
|
2023-10-02 20:06:18 +02:00
|
|
|
'key': key,
|
|
|
|
'timestamp_added': int(time.time())
|
2023-09-23 21:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
collection = client['Liabilities']['rate-limited-keys']
|
|
|
|
await collection.insert_one(scheme)
|
|
|
|
|
|
|
|
|
|
|
|
async def key_is_rated(key: str) -> bool:
|
|
|
|
"""Checks if a key is rate limited."""
|
|
|
|
|
|
|
|
client = AsyncIOMotorClient(MONGO_URI)
|
|
|
|
collection = client['Liabilities']['rate-limited-keys']
|
|
|
|
|
|
|
|
query = {
|
2023-10-02 20:06:18 +02:00
|
|
|
'key': key
|
2023-09-23 21:41:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result = await collection.find_one(query)
|
|
|
|
return result is not None
|
|
|
|
|
|
|
|
|
|
|
|
async def cached_key_is_rated(key: str) -> bool:
|
2023-10-02 20:06:18 +02:00
|
|
|
path = os.path.join(os.getcwd(), 'cache', 'rate_limited_keys.json')
|
2023-09-23 21:41:48 +02:00
|
|
|
|
2023-10-02 20:06:18 +02:00
|
|
|
with open(path, 'r') as file:
|
2023-09-23 21:41:48 +02:00
|
|
|
keys = json.load(file)
|
|
|
|
|
|
|
|
return key in keys
|
|
|
|
|
|
|
|
async def remove_rated_keys() -> None:
|
|
|
|
"""Removes all keys that have been rate limited for more than a day."""
|
|
|
|
|
|
|
|
a_day = 86400
|
|
|
|
|
|
|
|
client = AsyncIOMotorClient(MONGO_URI)
|
|
|
|
collection = client['Liabilities']['rate-limited-keys']
|
|
|
|
|
|
|
|
keys = await collection.find().to_list(length=None)
|
|
|
|
|
|
|
|
marked_for_removal = []
|
|
|
|
for key in keys:
|
|
|
|
if int(time.time()) - key['timestamp_added'] > a_day:
|
|
|
|
marked_for_removal.append(key['_id'])
|
|
|
|
|
|
|
|
query = {
|
2023-10-02 20:06:18 +02:00
|
|
|
'_id': {
|
|
|
|
'$in': marked_for_removal
|
2023-09-23 21:41:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await collection.delete_many(query)
|
|
|
|
|
|
|
|
async def cache_all_keys() -> None:
|
|
|
|
"""Clones all keys from the database to the cache."""
|
|
|
|
|
|
|
|
client = AsyncIOMotorClient(MONGO_URI)
|
|
|
|
collection = client['Liabilities']['rate-limited-keys']
|
|
|
|
|
|
|
|
keys = await collection.find().to_list(length=None)
|
|
|
|
keys = [key['key'] for key in keys]
|
|
|
|
|
2023-10-02 20:06:18 +02:00
|
|
|
path = os.path.join(os.getcwd(), 'cache', 'rate_limited_keys.json')
|
|
|
|
with open(path, 'w') as file:
|
2023-09-23 21:41:48 +02:00
|
|
|
json.dump(keys, file)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(remove_rated_keys())
|