diff --git a/BeamMP.py b/BeamMP.py new file mode 100644 index 0000000..8d821ae --- /dev/null +++ b/BeamMP.py @@ -0,0 +1,63 @@ +from datetime import datetime, timezone +from pymongo import UpdateOne +import requests + +from MasterServer import MasterServer + + +class BeamMP(MasterServer): + def __init__(self) -> None: + super().__init__('BeamMP') + self.collection.create_index({'ip': 1, 'port': 1}) + + def job(self): + # Fetch data until empty + servers = self._fetch() + + # Perform bulk write (upsert) operation + self._upsert_bulk_write(servers) + + # Remove old documents (assuming this method exists) + self._remove_old_documents(minutes=30) + + def find(self, *, host: str, port: int): + # Define the query to find documents with a specific address and port + query = {'ip': host, 'port': str(port)} + + # Specify the projection to exclude certain fields + projection = {'_id': 0, '_created_at': 0, '_last_modified': 0} + + # Retrieve the result + result = self.collection.find_one(query, projection) + + return result + + def _fetch(self) -> list: + url = "https://backend.beammp.com/servers-info" + response = requests.get(url, timeout=15) + response.raise_for_status() + data = response.json() + return data + + def _upsert_bulk_write(self, server_list: list): + # Prepare the updates + updates = [ + UpdateOne( + {'ip': server['ip'], 'port': server['port']}, + { + '$set': server, + '$currentDate': {'_last_modified': True}, + '$setOnInsert': {'_created_at': datetime.now(timezone.utc)} + }, + upsert=True + ) + for server in server_list + ] + return self._bulk_write(updates) + + +if __name__ == "__main__": + beamMP = BeamMP() + # beamMP.job() + server = beamMP.find(host='91.233.187.44', port=30815) + print(server) diff --git a/Factorio.py b/Factorio.py index d64e02f..b493c49 100644 --- a/Factorio.py +++ b/Factorio.py @@ -9,6 +9,7 @@ class Factorio(MasterServer): def __init__(self) -> None: super().__init__('Factorio') + self.collection.create_index('server_id') self.collection.create_index('host_address') def job(self): diff --git a/app.py b/app.py index 7fc310a..6847482 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ from MasterServer import MasterServer +from BeamMP import BeamMP from Factorio import Factorio from Palworld import Palworld @@ -30,6 +31,11 @@ def search(args: dict, master_server: MasterServer): return jsonify(result) +@app.route('/beammp/search', methods=['GET']) +def beammp_search(): + return search(request.args, BeamMP()) + + @app.route('/factorio/search', methods=['GET']) def factorio_search(): return search(request.args, Factorio()) diff --git a/main.py b/main.py index 1e36d13..6e868c6 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,8 @@ import schedule from MasterServer import MasterServer + +from BeamMP import BeamMP from Factorio import Factorio from Palworld import Palworld @@ -15,6 +17,7 @@ def run_threaded(master_server: MasterServer): threads[master_server.key].start() +schedule.every(5).minutes.do(run_threaded, BeamMP()) schedule.every(5).minutes.do(run_threaded, Factorio()) schedule.every(5).minutes.do(run_threaded, Palworld())