From 9baf1c06d60eae3303cf29e7ebd1a41cb91778ee Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 21 May 2021 03:49:47 +0000 Subject: [PATCH 1/5] Format code with yapf This commit fixes the style issues introduced in 56fd16e according to the output from yapf. Details: https://deepsource.io/gh/kokarare1212/librespot-python/transform/0548e0b8-69d4-4403-9575-38fa30cd76a6/ --- librespot/audio/cdn/CdnManager.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/librespot/audio/cdn/CdnManager.py b/librespot/audio/cdn/CdnManager.py index f2b358a..b7144c9 100644 --- a/librespot/audio/cdn/CdnManager.py +++ b/librespot/audio/cdn/CdnManager.py @@ -159,7 +159,8 @@ class CdnManager: else: self._expiration = -1 - class Streamer(GeneralAudioStream.GeneralAudioStream, GeneralWritableStream.GeneralWritableStream): + class Streamer(GeneralAudioStream.GeneralAudioStream, + GeneralWritableStream.GeneralWritableStream): _session: Session = None _streamId: StreamId = None _executorService = concurrent.futures.ThreadPoolExecutor() From efe2840fbb45354d40ae44be57c56b7a69311d36 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 21 May 2021 03:50:07 +0000 Subject: [PATCH 2/5] Restyled by black --- librespot/audio/cdn/CdnManager.py | 151 ++++++++++++++++++------------ 1 file changed, 92 insertions(+), 59 deletions(-) diff --git a/librespot/audio/cdn/CdnManager.py b/librespot/audio/cdn/CdnManager.py index b7144c9..c5e830e 100644 --- a/librespot/audio/cdn/CdnManager.py +++ b/librespot/audio/cdn/CdnManager.py @@ -30,9 +30,11 @@ class CdnManager: self._session = session def get_head(self, file_id: bytes): - resp = self._session.client() \ - .get(self._session.get_user_attribute("head-files-url", "https://heads-fa.spotify.com/head/{file_id}") - .replace("{file_id}", Utils.bytes_to_hex(file_id))) + resp = self._session.client().get( + self._session.get_user_attribute( + "head-files-url", "https://heads-fa.spotify.com/head/{file_id}" + ).replace("{file_id}", Utils.bytes_to_hex(file_id)) + ) if resp.status_code != 200: raise IOError("{}".format(resp.status_code)) @@ -43,27 +45,45 @@ class CdnManager: return body - def stream_external_episode(self, episode: Metadata.Episode, - external_url: str, - halt_listener: HaltListener): - return CdnManager.Streamer(self._session, StreamId(episode), - SuperAudioFormat.MP3, - CdnManager.CdnUrl(self, None, external_url), - self._session.cache(), NoopAudioDecrypt(), - halt_listener) + def stream_external_episode( + self, episode: Metadata.Episode, external_url: str, halt_listener: HaltListener + ): + return CdnManager.Streamer( + self._session, + StreamId(episode), + SuperAudioFormat.MP3, + CdnManager.CdnUrl(self, None, external_url), + self._session.cache(), + NoopAudioDecrypt(), + halt_listener, + ) - def stream_file(self, file: Metadata.AudioFile, key: bytes, url: str, - halt_listener: HaltListener): - return CdnManager.Streamer(self._session, StreamId.StreamId(file), - SuperAudioFormat.get(file.format), - CdnManager.CdnUrl(self, file.file_id, url), - self._session.cache(), AesAudioDecrypt(key), - halt_listener) + def stream_file( + self, + file: Metadata.AudioFile, + key: bytes, + url: str, + halt_listener: HaltListener, + ): + return CdnManager.Streamer( + self._session, + StreamId.StreamId(file), + SuperAudioFormat.get(file.format), + CdnManager.CdnUrl(self, file.file_id, url), + self._session.cache(), + AesAudioDecrypt(key), + halt_listener, + ) def get_audio_url(self, file_id: bytes): resp = self._session.api().send( - "GET", "/storage-resolve/files/audio/interactive/{}".format( - Utils.bytes_to_hex(file_id)), None, None) + "GET", + "/storage-resolve/files/audio/interactive/{}".format( + Utils.bytes_to_hex(file_id) + ), + None, + None, + ) if resp.status_code != 200: raise IOError(resp.status_code) @@ -76,11 +96,13 @@ class CdnManager: proto.ParseFromString(body) if proto.result == StorageResolve.StorageResolveResponse.Result.CDN: url = random.choice(proto.cdnurl) - self._LOGGER.debug("Fetched CDN url for {}: {}".format( - Utils.bytes_to_hex(file_id), url)) + self._LOGGER.debug( + "Fetched CDN url for {}: {}".format(Utils.bytes_to_hex(file_id), url) + ) return url raise CdnManager.CdnException( - "Could not retrieve CDN url! result: {}".format(proto.result)) + "Could not retrieve CDN url! result: {}".format(proto.result) + ) class CdnException(Exception): pass @@ -134,13 +156,14 @@ class CdnManager: continue if s[0][:i] == "exp": - expire_at = int(s[0][i + 1:]) + expire_at = int(s[0][i + 1 :]) break if expire_at is None: self._expiration = -1 self._cdnManager._LOGGER.warning( - "Invalid __token__ in CDN url: {}".format(url)) + "Invalid __token__ in CDN url: {}".format(url) + ) return self._expiration = expire_at * 1000 @@ -150,8 +173,10 @@ class CdnManager: except ValueError: self._expiration = -1 self._cdnManager._LOGGER.warning( - "Couldn't extract expiration, invalid parameter in CDN url: " - .format(url)) + "Couldn't extract expiration, invalid parameter in CDN url: ".format( + url + ) + ) return self._expiration = int(token_url.query[:i]) * 1000 @@ -159,8 +184,10 @@ class CdnManager: else: self._expiration = -1 - class Streamer(GeneralAudioStream.GeneralAudioStream, - GeneralWritableStream.GeneralWritableStream): + class Streamer( + GeneralAudioStream.GeneralAudioStream, + GeneralWritableStream.GeneralWritableStream, + ): _session: Session = None _streamId: StreamId = None _executorService = concurrent.futures.ThreadPoolExecutor() @@ -175,10 +202,16 @@ class CdnManager: _internalStream: CdnManager.Streamer.InternalStream = None _haltListener: HaltListener = None - def __init__(self, session: Session, stream_id: StreamId, - audio_format: SuperAudioFormat, cdn_url, - cache: CacheManager, audio_decrypt: AudioDecrypt, - halt_listener: HaltListener): + def __init__( + self, + session: Session, + stream_id: StreamId, + audio_format: SuperAudioFormat, + cdn_url, + cache: CacheManager, + audio_decrypt: AudioDecrypt, + halt_listener: HaltListener, + ): self._session = session self._streamId = stream_id self._audioFormat = audio_format @@ -186,39 +219,38 @@ class CdnManager: self._cdnUrl = cdn_url self._haltListener = halt_listener - resp = self.request(range_start=0, - range_end=ChannelManager.CHUNK_SIZE - 1) + resp = self.request(range_start=0, range_end=ChannelManager.CHUNK_SIZE - 1) content_range = resp._headers.get("Content-Range") if content_range is None: raise IOError("Missing Content-Range header!") split = Utils.split(content_range, "/") self._size = int(split[1]) - self._chunks = int( - math.ceil(self._size / ChannelManager.CHUNK_SIZE)) + self._chunks = int(math.ceil(self._size / ChannelManager.CHUNK_SIZE)) first_chunk = resp._buffer self._available = [False for _ in range(self._chunks)] self._requested = [False for _ in range(self._chunks)] self._buffer = [bytearray() for _ in range(self._chunks)] - self._internalStream = CdnManager.Streamer.InternalStream( - self, False) + self._internalStream = CdnManager.Streamer.InternalStream(self, False) self._requested[0] = True self.write_chunk(first_chunk, 0, False) - def write_chunk(self, chunk: bytes, chunk_index: int, - cached: bool) -> None: + def write_chunk(self, chunk: bytes, chunk_index: int, cached: bool) -> None: if self._internalStream.is_closed(): return self._session._LOGGER.debug( "Chunk {}/{} completed, cached: {}, stream: {}".format( - chunk_index + 1, self._chunks, cached, self.describe())) + chunk_index + 1, self._chunks, cached, self.describe() + ) + ) self._buffer[chunk_index] = self._audioDecrypt.decrypt_chunk( - chunk_index, chunk) + chunk_index, chunk + ) self._internalStream.notify_chunk_available(chunk_index) def stream(self) -> AbsChunkedInputStream: @@ -229,8 +261,7 @@ class CdnManager: def describe(self) -> str: if self._streamId.is_episode(): - return "episode_gid: {}".format( - self._streamId.get_episode_gid()) + return "episode_gid: {}".format(self._streamId.get_episode_gid()) return "file_id: {}".format(self._streamId.get_file_id()) def decrypt_time_ms(self) -> int: @@ -240,10 +271,9 @@ class CdnManager: resp = self.request(index) self.write_chunk(resp._buffer, index, False) - def request(self, - chunk: int = None, - range_start: int = None, - range_end: int = None) -> CdnManager.InternalResponse: + def request( + self, chunk: int = None, range_start: int = None, range_end: int = None + ) -> CdnManager.InternalResponse: if chunk is None and range_start is None and range_end is None: raise TypeError() @@ -251,12 +281,10 @@ class CdnManager: range_start = ChannelManager.CHUNK_SIZE * chunk range_end = (chunk + 1) * ChannelManager.CHUNK_SIZE - 1 - resp = self._session.client().get(self._cdnUrl._url, - headers={ - "Range": - "bytes={}-{}".format( - range_start, range_end) - }) + resp = self._session.client().get( + self._cdnUrl._url, + headers={"Range": "bytes={}-{}".format(range_start, range_end)}, + ) if resp.status_code != 206: raise IOError(resp.status_code) @@ -291,16 +319,21 @@ class CdnManager: def request_chunk_from_stream(self, index: int) -> None: self.streamer._executorService.submit( - lambda: self.streamer.request_chunk(index)) + lambda: self.streamer.request_chunk(index) + ) def stream_read_halted(self, chunk: int, _time: int) -> None: if self.streamer._haltListener is not None: self.streamer._executorService.submit( lambda: self.streamer._haltListener.stream_read_halted( - chunk, _time)) + chunk, _time + ) + ) def stream_read_resumed(self, chunk: int, _time: int) -> None: if self.streamer._haltListener is not None: self.streamer._executorService.submit( - lambda: self.streamer._haltListener. - stream_read_resumed(chunk, _time)) + lambda: self.streamer._haltListener.stream_read_resumed( + chunk, _time + ) + ) From 20acbb40d12328660b70fefcd6c7f9f80c9aa518 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 21 May 2021 03:50:09 +0000 Subject: [PATCH 3/5] Restyled by isort --- librespot/audio/cdn/CdnManager.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/librespot/audio/cdn/CdnManager.py b/librespot/audio/cdn/CdnManager.py index c5e830e..8acc876 100644 --- a/librespot/audio/cdn/CdnManager.py +++ b/librespot/audio/cdn/CdnManager.py @@ -1,11 +1,5 @@ from __future__ import annotations -from librespot.audio.AbsChunkedInputStream import AbsChunkedInputStream -from librespot.audio import GeneralAudioStream, GeneralWritableStream, StreamId -from librespot.audio.decrypt import AesAudioDecrypt, NoopAudioDecrypt -from librespot.audio.format import SuperAudioFormat -from librespot.audio.storage import ChannelManager -from librespot.common import Utils -from librespot.proto import StorageResolve + import concurrent.futures import logging import math @@ -14,9 +8,17 @@ import time import typing import urllib.parse +from librespot.audio import GeneralAudioStream, GeneralWritableStream, StreamId +from librespot.audio.AbsChunkedInputStream import AbsChunkedInputStream +from librespot.audio.decrypt import AesAudioDecrypt, NoopAudioDecrypt +from librespot.audio.format import SuperAudioFormat +from librespot.audio.storage import ChannelManager +from librespot.common import Utils +from librespot.proto import StorageResolve + if typing.TYPE_CHECKING: - from librespot.audio.HaltListener import HaltListener from librespot.audio.decrypt.AudioDecrypt import AudioDecrypt + from librespot.audio.HaltListener import HaltListener from librespot.cache.CacheManager import CacheManager from librespot.core.Session import Session from librespot.proto import Metadata From 80fc2bc837352f6db56e54ef938c284457c56057 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 21 May 2021 03:50:14 +0000 Subject: [PATCH 4/5] Restyled by reorder-python-imports --- librespot/audio/cdn/CdnManager.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/librespot/audio/cdn/CdnManager.py b/librespot/audio/cdn/CdnManager.py index 8acc876..f25b4c8 100644 --- a/librespot/audio/cdn/CdnManager.py +++ b/librespot/audio/cdn/CdnManager.py @@ -8,9 +8,12 @@ import time import typing import urllib.parse -from librespot.audio import GeneralAudioStream, GeneralWritableStream, StreamId +from librespot.audio import GeneralAudioStream +from librespot.audio import GeneralWritableStream +from librespot.audio import StreamId from librespot.audio.AbsChunkedInputStream import AbsChunkedInputStream -from librespot.audio.decrypt import AesAudioDecrypt, NoopAudioDecrypt +from librespot.audio.decrypt import AesAudioDecrypt +from librespot.audio.decrypt import NoopAudioDecrypt from librespot.audio.format import SuperAudioFormat from librespot.audio.storage import ChannelManager from librespot.common import Utils From f0f0afd28c881a49b5e50dcb6e6843491d59464b Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Fri, 21 May 2021 03:50:21 +0000 Subject: [PATCH 5/5] Restyled by yapf --- librespot/audio/cdn/CdnManager.py | 84 ++++++++++++++----------------- 1 file changed, 39 insertions(+), 45 deletions(-) diff --git a/librespot/audio/cdn/CdnManager.py b/librespot/audio/cdn/CdnManager.py index f25b4c8..080bdde 100644 --- a/librespot/audio/cdn/CdnManager.py +++ b/librespot/audio/cdn/CdnManager.py @@ -37,9 +37,9 @@ class CdnManager: def get_head(self, file_id: bytes): resp = self._session.client().get( self._session.get_user_attribute( - "head-files-url", "https://heads-fa.spotify.com/head/{file_id}" - ).replace("{file_id}", Utils.bytes_to_hex(file_id)) - ) + "head-files-url", + "https://heads-fa.spotify.com/head/{file_id}").replace( + "{file_id}", Utils.bytes_to_hex(file_id))) if resp.status_code != 200: raise IOError("{}".format(resp.status_code)) @@ -50,9 +50,9 @@ class CdnManager: return body - def stream_external_episode( - self, episode: Metadata.Episode, external_url: str, halt_listener: HaltListener - ): + def stream_external_episode(self, episode: Metadata.Episode, + external_url: str, + halt_listener: HaltListener): return CdnManager.Streamer( self._session, StreamId(episode), @@ -84,8 +84,7 @@ class CdnManager: resp = self._session.api().send( "GET", "/storage-resolve/files/audio/interactive/{}".format( - Utils.bytes_to_hex(file_id) - ), + Utils.bytes_to_hex(file_id)), None, None, ) @@ -101,13 +100,11 @@ class CdnManager: proto.ParseFromString(body) if proto.result == StorageResolve.StorageResolveResponse.Result.CDN: url = random.choice(proto.cdnurl) - self._LOGGER.debug( - "Fetched CDN url for {}: {}".format(Utils.bytes_to_hex(file_id), url) - ) + self._LOGGER.debug("Fetched CDN url for {}: {}".format( + Utils.bytes_to_hex(file_id), url)) return url raise CdnManager.CdnException( - "Could not retrieve CDN url! result: {}".format(proto.result) - ) + "Could not retrieve CDN url! result: {}".format(proto.result)) class CdnException(Exception): pass @@ -161,14 +158,13 @@ class CdnManager: continue if s[0][:i] == "exp": - expire_at = int(s[0][i + 1 :]) + expire_at = int(s[0][i + 1:]) break if expire_at is None: self._expiration = -1 self._cdnManager._LOGGER.warning( - "Invalid __token__ in CDN url: {}".format(url) - ) + "Invalid __token__ in CDN url: {}".format(url)) return self._expiration = expire_at * 1000 @@ -178,10 +174,8 @@ class CdnManager: except ValueError: self._expiration = -1 self._cdnManager._LOGGER.warning( - "Couldn't extract expiration, invalid parameter in CDN url: ".format( - url - ) - ) + "Couldn't extract expiration, invalid parameter in CDN url: " + .format(url)) return self._expiration = int(token_url.query[:i]) * 1000 @@ -190,8 +184,8 @@ class CdnManager: self._expiration = -1 class Streamer( - GeneralAudioStream.GeneralAudioStream, - GeneralWritableStream.GeneralWritableStream, + GeneralAudioStream.GeneralAudioStream, + GeneralWritableStream.GeneralWritableStream, ): _session: Session = None _streamId: StreamId = None @@ -224,38 +218,39 @@ class CdnManager: self._cdnUrl = cdn_url self._haltListener = halt_listener - resp = self.request(range_start=0, range_end=ChannelManager.CHUNK_SIZE - 1) + resp = self.request(range_start=0, + range_end=ChannelManager.CHUNK_SIZE - 1) content_range = resp._headers.get("Content-Range") if content_range is None: raise IOError("Missing Content-Range header!") split = Utils.split(content_range, "/") self._size = int(split[1]) - self._chunks = int(math.ceil(self._size / ChannelManager.CHUNK_SIZE)) + self._chunks = int( + math.ceil(self._size / ChannelManager.CHUNK_SIZE)) first_chunk = resp._buffer self._available = [False for _ in range(self._chunks)] self._requested = [False for _ in range(self._chunks)] self._buffer = [bytearray() for _ in range(self._chunks)] - self._internalStream = CdnManager.Streamer.InternalStream(self, False) + self._internalStream = CdnManager.Streamer.InternalStream( + self, False) self._requested[0] = True self.write_chunk(first_chunk, 0, False) - def write_chunk(self, chunk: bytes, chunk_index: int, cached: bool) -> None: + def write_chunk(self, chunk: bytes, chunk_index: int, + cached: bool) -> None: if self._internalStream.is_closed(): return self._session._LOGGER.debug( "Chunk {}/{} completed, cached: {}, stream: {}".format( - chunk_index + 1, self._chunks, cached, self.describe() - ) - ) + chunk_index + 1, self._chunks, cached, self.describe())) self._buffer[chunk_index] = self._audioDecrypt.decrypt_chunk( - chunk_index, chunk - ) + chunk_index, chunk) self._internalStream.notify_chunk_available(chunk_index) def stream(self) -> AbsChunkedInputStream: @@ -266,7 +261,8 @@ class CdnManager: def describe(self) -> str: if self._streamId.is_episode(): - return "episode_gid: {}".format(self._streamId.get_episode_gid()) + return "episode_gid: {}".format( + self._streamId.get_episode_gid()) return "file_id: {}".format(self._streamId.get_file_id()) def decrypt_time_ms(self) -> int: @@ -276,9 +272,10 @@ class CdnManager: resp = self.request(index) self.write_chunk(resp._buffer, index, False) - def request( - self, chunk: int = None, range_start: int = None, range_end: int = None - ) -> CdnManager.InternalResponse: + def request(self, + chunk: int = None, + range_start: int = None, + range_end: int = None) -> CdnManager.InternalResponse: if chunk is None and range_start is None and range_end is None: raise TypeError() @@ -288,7 +285,9 @@ class CdnManager: resp = self._session.client().get( self._cdnUrl._url, - headers={"Range": "bytes={}-{}".format(range_start, range_end)}, + headers={ + "Range": "bytes={}-{}".format(range_start, range_end) + }, ) if resp.status_code != 206: @@ -324,21 +323,16 @@ class CdnManager: def request_chunk_from_stream(self, index: int) -> None: self.streamer._executorService.submit( - lambda: self.streamer.request_chunk(index) - ) + lambda: self.streamer.request_chunk(index)) def stream_read_halted(self, chunk: int, _time: int) -> None: if self.streamer._haltListener is not None: self.streamer._executorService.submit( lambda: self.streamer._haltListener.stream_read_halted( - chunk, _time - ) - ) + chunk, _time)) def stream_read_resumed(self, chunk: int, _time: int) -> None: if self.streamer._haltListener is not None: self.streamer._executorService.submit( - lambda: self.streamer._haltListener.stream_read_resumed( - chunk, _time - ) - ) + lambda: self.streamer._haltListener. + stream_read_resumed(chunk, _time))