Giter VIP home page Giter VIP logo

hltv-python's Introduction

HLTV Livescore

This is a HLTV livescore implementation in Python. It feels super weird that I don't find any implementations for this but I might be bad at looking. The two JavaScript versions I found helped me understand and get inspiration.

Borrowed with pride from:

Might integrate with other Python libraries in the future, such as

Documentation

General documentation about the Socket.IO streams can be found in DOCUMENTATION.md

Live scoring

So HLTV uses Socket.IO to stream the data they get from Valve and ESL (I think?). This data is pushed on a socket. See DOCUMENTATION for server information.

I actually have a really (really) hard time finding any documentation at all regarding this socket. Is it official? Is it documented? How's it rate limited? What events are pushed, how and when, and with what data? Because of this I've tried to document my findings in DOCUMENTATION.md.

This implementation

Luckily there's a great library named python-socketio which makes it easy for me to read from the socket. All I need to do after connecting is to parse the stream. โœŒ๐Ÿผ

Usage

import asyncio
from scorebot import Livescore

async def on_kill(frag):
    print("{} killed {}".format(frag.killer.name, frag.victim.name))

async def main():
    ls = Livescore(123456)
    ls.on(ls.EVENT_KILL, on_kill)

    socket = await ls.socket()
    await socket.wait()


if __name__ == "__main__":
    asyncio.run(main())

See examples folder for an implementation creating a kill feed.

hltv-python's People

Contributors

bombsimon avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

ruddan kamnsv

hltv-python's Issues

ConnectionError: Unexpected response from server for any url i pass

Hello, can you help me with that issue.
I get an error:

Traceback (most recent call last):
  File "C:\Users\Fedor\PycharmProjects\odds_scraper\main.py", line 26, in on_startup
    await scrap_hltv_scores()
  File "C:\Users\Fedor\PycharmProjects\odds_scraper\hltv_scraper\hltv_score_scraper.py", line 38, in scrap_hltv_scores
    socket = await ls.socket()
  File "C:\Users\Fedor\PycharmProjects\odds_scraper\hltv_scraper\scorebot\scorebot.py", line 303, in socket
    await sio.connect(self.socket_uri)
  File "C:\Users\Fedor\PycharmProjects\odds_scraper\venv\lib\site-packages\socketio\asyncio_client.py", line 144, in connect
    raise exceptions.ConnectionError(exc.args[0]) from None
socketio.exceptions.ConnectionError: Unexpected response from server

The code is:

async def scrap_hltv_scores():
    ls = Livescore()
    ls.from_url(
        "https://www.hltv.org/matches/2352176/8ballers-vs-hazard-esea-open-season-38-australia"
    )
    ls.on(ls.EVENT_CONNECT, on_connect)
    # ls.on(ls.EVENT_SCOREBOARD, on_scoreboard)
    ls.on(ls.EVENT_ROUND_END, on_round_end)

    socket = await ls.socket()
    feed = asyncio.create_task(socket.wait())
    await feed

I tried to change the socket_uri in Livescore class from https://scorebot-secure.hltv.org to https://scorebot-lb.hltv.org e.t.c from docs but nothing changed, except the fact that previous error changes to socketio.exceptions.ConnectionError: Connection refused by the server
What's going wrong?

Such events dont exists

I cant get events like MatchStarted or mapChange. Can someone get this type of updates? Im using pure socket-io, but issue cant be bound with that fact...

Implement winType

I've found your implementation for the scorebot very interesting. I understood how to use the events for end rounds, kills in the round etc.. but I'm struggling a bit with winType. Can you explain a little bit about how to use it ?

JSONDecodeError related to Invalid Namespace

Hey, having a hard time understanding a way to fix this "Invalid Namespace" response parsing issue.

The extra debug info was obtained using sio = socketio.AsyncClient(logger=True, engineio_logger=True) in the scorebot file.

Signal handler is unsupported
Attempting WebSocket connection to wss://scorebot-lb.hltv.org/socket.io/?transport=websocket&EIO=4
WebSocket connection accepted with {'sid': 'a60e8fb7-0da1-4394-9af2-d52894bd8887', 'upgrades': ['websocket'], 'pingInterval': 25000, 'pingTimeout': 60000}
Engine.IO connection established
Sending packet MESSAGE data 0{}
Received packet MESSAGE data 0
Namespace / is connected
Emitting event "readyForMatch" [/]
Sending packet MESSAGE data 2["readyForMatch","{\"listId\": 2354815}"]
connected to match 2354815!
running in background
Received packet MESSAGE data 4{},"Invalid namespace"
Task exception was never retrieved
future: <Task finished name='Task-14' coro=<AsyncClient._handle_eio_message() done, defined at C:\Python\Python39\lib\site-packages\socketio\asyncio_client.py:505> exception=JSONDecodeError('Extra data: line 1 column 3 (char 2)')>
Traceback (most recent call last):
  File "C:\Python\Python39\lib\site-packages\socketio\asyncio_client.py", line 516, in _handle_eio_message
    pkt = self.packet_class(encoded_packet=data)
  File "C:\Python\Python39\lib\site-packages\socketio\packet.py", line 43, in __init__
    self.attachment_count = self.decode(encoded_packet) or 0
  File "C:\Python\Python39\lib\site-packages\socketio\packet.py", line 128, in decode
    self.data = self.json.loads(ep)
  File "C:\Python\Python39\lib\site-packages\engineio\json.py", line 16, in loads
    return original_loads(*args, **kwargs)
  File "C:\Python\Python39\lib\json\__init__.py", line 359, in loads
    return cls(**kw).decode(s)
  File "C:\Python\Python39\lib\json\decoder.py", line 340, in decode
    raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2)

Connect to scorebot without socket().wait()

Hi,

So I've been working with our implementation for some time now. I would like to know if there's a way to connect to the scorebot without wait() because each time I try without this function, it just doesn't connect to the scorebot. Also, the fact that wait() blocks the thread is a bit annoying for me.

is there any possible way to register async function?

Example contains lines:

@classmethod
def on_connect(cls):
    """
    A simple callback to ensure we got connected.
    """
    print("connected!")

and:

live_score.on(live_score.EVENT_CONNECT, kill_feed.on_connect)
What if the "on_connect" etc functions should be async? For write something in DB as example.

And additional. What should i do to get info from 2+ matches in the same time? I tried to make several instanses of Livescore in cycle, but the first await socket.wait() blocks everything. So, during iteration i can get data only from the first match (1st iter)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.