Skip to content

Commit

Permalink
4.0.4 (#129)
Browse files Browse the repository at this point in the history
* Don't default to -1 if frame stats missing

* Rudimentary track encoding support

* Fix "TypeError: Passing coroutines is forbidden, use tasks explicitly." (#128)

* fix typos

* 4.0.4

Co-authored-by: Khilseith <[email protected]>
  • Loading branch information
devoxin and Khilseith authored Nov 2, 2022
1 parent d4436bc commit cc8df70
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async def play(self, ctx, *, query: str):
results = await player.node.get_tracks(query)

# Results could be None if Lavalink returns an invalid response (non-JSON/non-200 (OK)).
# ALternatively, resullts.tracks could be an empty array if the query yielded no tracks.
# Alternatively, results.tracks could be an empty array if the query yielded no tracks.
if not results or not results.tracks:
return await ctx.send('Nothing found!')

Expand Down
5 changes: 3 additions & 2 deletions lavalink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__author__ = 'Devoxin'
__license__ = 'MIT'
__copyright__ = 'Copyright 2017-present Devoxin'
__version__ = '4.0.3'
__version__ = '4.0.4'


import inspect
Expand All @@ -25,7 +25,8 @@
from .nodemanager import NodeManager
from .playermanager import PlayerManager
from .stats import Penalty, Stats
from .utils import decode_track, format_time, parse_time, timestamp_to_millis
from .utils import (decode_track, encode_track, format_time, parse_time,
timestamp_to_millis)
from .websocket import WebSocket


Expand Down
2 changes: 1 addition & 1 deletion lavalink/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ async def _hook_wrapper(hook, event):
# clarity.

tasks = [_hook_wrapper(hook, event) for hook in itertools.chain(generic_hooks, targeted_hooks)]
await asyncio.wait(tasks)
await asyncio.gather(*tasks)

_log.debug('Dispatched \'%s\' to all registered hooks', type(event).__name__)

Expand Down
17 changes: 5 additions & 12 deletions lavalink/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,8 @@ class Penalty:
def __init__(self, stats):
self.player_penalty = stats.playing_players
self.cpu_penalty = 1.05 ** (100 * stats.system_load) * 10 - 10
self.null_frame_penalty = 0
self.deficit_frame_penalty = 0

if stats.frames_nulled != -1:
self.null_frame_penalty = (1.03 ** (500 * (stats.frames_nulled / 3000))) * 300 - 300
self.null_frame_penalty *= 2

if stats.frames_deficit != -1:
self.deficit_frame_penalty = (1.03 ** (500 * (stats.frames_deficit / 3000))) * 600 - 600
self.null_frame_penalty = ((1.03 ** (500 * (stats.frames_nulled / 3000))) * 300 - 300) * 2
self.deficit_frame_penalty = (1.03 ** (500 * (stats.frames_deficit / 3000))) * 600 - 600

self.total = self.player_penalty + self.cpu_penalty + self.null_frame_penalty + self.deficit_frame_penalty

Expand Down Expand Up @@ -128,9 +121,9 @@ def __init__(self, node, data):
self.lavalink_load = cpu['lavalinkLoad']

frame_stats = data.get('frameStats', {})
self.frames_sent = frame_stats.get('sent', -1)
self.frames_nulled = frame_stats.get('nulled', -1)
self.frames_deficit = frame_stats.get('deficit', -1)
self.frames_sent = frame_stats.get('sent', 0)
self.frames_nulled = frame_stats.get('nulled', 0)
self.frames_deficit = frame_stats.get('deficit', 0)
self.penalty = Penalty(self)

@classmethod
Expand Down
37 changes: 19 additions & 18 deletions lavalink/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
SOFTWARE.
"""
import struct
from base64 import b64encode
from typing import Tuple

from .datarw import DataReader
from .datarw import DataReader, DataWriter
from .models import AudioTrack


Expand Down Expand Up @@ -159,23 +160,23 @@ def decode_track(track: str) -> AudioTrack:
return AudioTrack(track_object, 0, position=position, encoder_version=version)


# def encode_track(track: dict):
# assert {'title', 'author', 'length', 'identifier', 'is_stream', 'uri', 'source', 'position'} == track.keys()
def encode_track(track: dict):
assert {'title', 'author', 'length', 'identifier', 'isStream', 'uri', 'sourceName', 'position'} == track.keys()

# writer = DataWriter()
writer = DataWriter()

# version = struct.pack('B', 2)
# writer.write_byte(version)
# writer.write_utf(track['title'])
# writer.write_utf(track['author'])
# writer.write_long(track['length'])
# writer.write_utf(track['identifier'])
# writer.write_boolean(track['is_stream'])
# writer.write_boolean(track['uri'])
# writer.write_utf(track['uri'])
# writer.write_utf(track['source'])
# writer.write_long(track['position'])
version = struct.pack('B', 2)
writer.write_byte(version)
writer.write_utf(track['title'])
writer.write_utf(track['author'])
writer.write_long(track['length'])
writer.write_utf(track['identifier'])
writer.write_boolean(track['isStream'])
writer.write_boolean(track['uri'])
writer.write_utf(track['uri'])
writer.write_utf(track['sourceName'])
writer.write_long(track['position'])

# enc = writer.finish()
# b64 = b64encode(enc)
# return b64
enc = writer.finish()
b64 = b64encode(enc)
return b64

0 comments on commit cc8df70

Please sign in to comment.