MCPcopy Create free account
hub / github.com/devoxin/Lavalink.py / encode_track

Function encode_track

lavalink/utils.py:231–277  ·  view source on GitHub ↗

Encodes a track dict into a base64 string, readable by the Lavalink server. A track should have *at least* the following keys: ``title``, ``author``, ``length``, ``identifier``, ``isStream``, ``uri``, ``sourceName`` and ``position``. If the track is a v3 track, it should have the

(track: Dict[str, Any],
                 source_encoders: Mapping[str, Callable[[DataWriter, Dict[str, Any]], None]] = MISSING)

Source from the content-addressed store, hash-verified

229
230
231def encode_track(track: Dict[str, Any],
232 source_encoders: Mapping[str, Callable[[DataWriter, Dict[str, Any]], None]] = MISSING) -> Tuple[int, str]:
233 """
234 Encodes a track dict into a base64 string, readable by the Lavalink server.
235
236 A track should have *at least* the following keys:
237 ``title``, ``author``, ``length``, ``identifier``, ``isStream``, ``uri``, ``sourceName`` and ``position``.
238
239 If the track is a v3 track, it should have the following additional fields:
240 ``artworkUrl`` and ``isrc``. isrc can be ``None`` if not applicable.
241
242 Parameters
243 ----------
244 track: Dict[str, Union[Optional[str], int, bool]]
245 The track dict to serialize.
246 source_encoders: Mapping[:class:`str`, Callable[[:class:`DataWriter`]]
247 A mapping of source-specific encoders to use.
248 Some Lavaplayer sources have additional fields encoded on a per-source manager basis, so you can
249 specify a mapping of encoders that will handle encoding these additional fields. This isn't required
250 for all sources, so ensure that you need them before specifying.
251
252 The mapping must be in the format of something like ``{'http': http_encoder_function}``, where the
253 key ``str`` is the name of the source. These functions will only be called if track's ``sourceName``
254 field matches.
255
256 Raises
257 ------
258 :class:`InvalidTrack`
259 If the track has unexpected, or missing keys, possibly due to an incompatible version or another reason.
260
261 Returns
262 -------
263 Tuple[int, str]
264 A tuple containing (track_version, encoded_track).
265 For example, if a track was encoded as version 3, the return value will be ``(3, '...really long track string...')``.
266 """
267 track_keys = track.keys() # set(track) is faster for larger collections, but slower for smaller.
268
269 if not V2_KEYSET <= track_keys: # V2_KEYSET contains the minimum number of fields required to successfully encode a track.
270 missing_keys = [k for k in V2_KEYSET if k not in track]
271
272 raise InvalidTrack(f'Track object is missing keys required for serialization: {", ".join(missing_keys)}')
273
274 if V3_KEYSET <= track_keys:
275 return (3, encode_track_v3(track, source_encoders))
276
277 return (2, encode_track_v2(track, source_encoders))
278
279
280def encode_track_v2(track: Dict[str, Any],

Callers

nothing calls this directly

Calls 3

InvalidTrackClass · 0.85
encode_track_v3Function · 0.85
encode_track_v2Function · 0.85

Tested by

no test coverage detected