opengsq / opengsq-python

Python library designed for querying game servers. It supports 24 different query protocols and has been downloaded over 34,000 times.
https://python.opengsq.com/
MIT License
24 stars 3 forks source link

Fivem support? #5

Closed Pok4 closed 7 months ago

Pok4 commented 8 months ago

Does this library support fivem ?

Pok4 commented 7 months ago

Ok, i write it: fivem.py:

import asyncio
import json
import httpx
from opengsq.protocol_base import ProtocolBase

class FiveM(ProtocolBase):
    """FiveM Protocol"""
    full_name = 'FiveM Multiplayer Protocol'

    async def make_request(self, url, headers):
        async with httpx.AsyncClient() as client:
            response = await client.get(url, headers=headers)

            if response.status_code == httpx.codes.OK:
                return response.json()

            print(f"HTTP request failed with status code: {response.status_code}")
            return None

    async def get_info(self):
        url = f'http://{self._host}:{self._port}/dynamic.json'
        headers = {'Content-Type': 'application/json'}  # Set the Content-Type header

        dynamic_data = await self.make_request(url, headers)
        result = {
            'hostname': dynamic_data.get('hostname', ''),
            'players': dynamic_data.get('clients', 0),
            'maxplayers': dynamic_data.get('sv_maxclients', 0),
            'gametype': dynamic_data.get('gametype', ''),
            'mapname': dynamic_data.get('mapname', '')
        }
        return result

`

it works with the command : opengsq fivem --host 45.81.17.148 --port 30120 --function get_info

but i can make it work with


        if game.strip() == "fivem":
            fivem = FiveM(host=ip_real.strip(), port=port_real.strip())
            info = await fivem.get_info()

            return {
                'hostname': info['hostname'] if info else hostname_untouch,
                'players': info['players'] if info else 0,
                'maxplayers': info['maxplayers'] if info else 0,
                'mapname': info['mapname'] if info else 'unknown',
                'bots': 0,
                'status': 'online' if info else 'offline'
            }
`

I get:

  File "/usr/lib/python3/dist-packages/mysql/connector/network.py", line 133, in __del__
  File "/usr/lib/python3/dist-packages/mysql/connector/network.py", line 121, in shutdown
TypeError: catching classes that do not inherit from BaseException is not allowed

Can you implement this in next release ? It works very well. Every fivem have builtin web server and this file is there... (http://45.81.17.148:30120/dynamic.json)

Ps: The script above needs: pip install httpx Thank you!

BattlefieldDuck commented 7 months ago

@Pok4 This is a good suggestion!

I will implement that in the next release.