Skip to content

Commit

Permalink
Support BeamMP
Browse files Browse the repository at this point in the history
  • Loading branch information
BattlefieldDuck committed Mar 11, 2024
1 parent 82b608c commit 3fe82fc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
63 changes: 63 additions & 0 deletions BeamMP.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions Factorio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from MasterServer import MasterServer

from BeamMP import BeamMP
from Factorio import Factorio
from Palworld import Palworld

Expand Down Expand Up @@ -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())
Expand Down
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import schedule

from MasterServer import MasterServer

from BeamMP import BeamMP
from Factorio import Factorio
from Palworld import Palworld

Expand All @@ -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())

Expand Down

0 comments on commit 3fe82fc

Please sign in to comment.