Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support PROXY_URL from .env file #285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions librespot/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

from dotenv import load_dotenv


load_dotenv()


PROXY_URL = os.getenv("PROXY_URL")
94 changes: 18 additions & 76 deletions librespot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from librespot.audio import PlayableContentFeeder
from librespot.audio.storage import ChannelManager
from librespot.cache import CacheManager
from librespot.config import PROXY_URL
from librespot.crypto import CipherPair
from librespot.crypto import DiffieHellman
from librespot.crypto import Packet
Expand Down Expand Up @@ -1120,6 +1121,21 @@ def create_client(conf: Configuration) -> requests.Session:

"""
client = requests.Session()

if PROXY_URL:
proxy_schemes = {
"http://": "http",
"https://": "https",
"socks5://": "socks5",
}

for prefix, scheme in proxy_schemes.items():
if PROXY_URL.startswith(prefix):
client.proxies.update({
"http": PROXY_URL,
"https": PROXY_URL
})
break
return client

def dealer(self) -> DealerClient:
Expand Down Expand Up @@ -1635,16 +1651,6 @@ def create(self) -> Session:
return session

class Configuration:
""" """
# Proxy
# proxyEnabled: bool
# proxyType: Proxy.Type
# proxyAddress: str
# proxyPort: int
# proxyAuth: bool
# proxyUsername: str
# proxyPassword: str

# Cache
cache_enabled: bool
cache_dir: str
Expand All @@ -1659,27 +1665,13 @@ class Configuration:

def __init__(
self,
# proxy_enabled: bool,
# proxy_type: Proxy.Type,
# proxy_address: str,
# proxy_port: int,
# proxy_auth: bool,
# proxy_username: str,
# proxy_password: str,
cache_enabled: bool,
cache_dir: str,
do_cache_clean_up: bool,
store_credentials: bool,
stored_credentials_file: str,
retry_on_chunk_error: bool,
retry_on_chunk_error: bool
):
# self.proxyEnabled = proxy_enabled
# self.proxyType = proxy_type
# self.proxyAddress = proxy_address
# self.proxyPort = proxy_port
# self.proxyAuth = proxy_auth
# self.proxyUsername = proxy_username
# self.proxyPassword = proxy_password
self.cache_enabled = cache_enabled
self.cache_dir = cache_dir
self.do_cache_clean_up = do_cache_clean_up
Expand All @@ -1689,15 +1681,6 @@ def __init__(

class Builder:
""" """
# Proxy
# proxyEnabled: bool = False
# proxyType: Proxy.Type = Proxy.Type.DIRECT
# proxyAddress: str = None
# proxyPort: int = None
# proxyAuth: bool = None
# proxyUsername: str = None
# proxyPassword: str = None

# Cache
cache_enabled: bool = True
cache_dir: str = os.path.join(os.getcwd(), "cache")
Expand All @@ -1711,40 +1694,6 @@ class Builder:
# Fetching
retry_on_chunk_error: bool = True

# def set_proxy_enabled(
# self,
# proxy_enabled: bool) -> Session.Configuration.Builder:
# self.proxyEnabled = proxy_enabled
# return self

# def set_proxy_type(
# self,
# proxy_type: Proxy.Type) -> Session.Configuration.Builder:
# self.proxyType = proxy_type
# return self

# def set_proxy_address(
# self, proxy_address: str) -> Session.Configuration.Builder:
# self.proxyAddress = proxy_address
# return self

# def set_proxy_auth(
# self, proxy_auth: bool) -> Session.Configuration.Builder:
# self.proxyAuth = proxy_auth
# return self

# def set_proxy_username(
# self,
# proxy_username: str) -> Session.Configuration.Builder:
# self.proxyUsername = proxy_username
# return self

# def set_proxy_password(
# self,
# proxy_password: str) -> Session.Configuration.Builder:
# self.proxyPassword = proxy_password
# return self

def set_cache_enabled(
self,
cache_enabled: bool) -> Session.Configuration.Builder:
Expand Down Expand Up @@ -1824,19 +1773,12 @@ def build(self) -> Session.Configuration:

"""
return Session.Configuration(
# self.proxyEnabled,
# self.proxyType,
# self.proxyAddress,
# self.proxyPort,
# self.proxyAuth,
# self.proxyUsername,
# self.proxyPassword,
self.cache_enabled,
self.cache_dir,
self.do_cache_clean_up,
self.store_credentials,
self.stored_credentials_file,
self.retry_on_chunk_error,
self.retry_on_chunk_error
)

class ConnectionHolder:
Expand Down
10 changes: 6 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defusedxml==0.7.1
defusedxml~=0.7.1
protobuf==3.20.1
pycryptodomex==3.21.0
pyogg==0.6.14a.1
requests==2.32.3
websocket-client==1.8.0
zeroconf==0.136.0
requests~=2.32.3
websocket-client~=1.8.0
zeroconf~=0.136.0
setuptools~=65.5.1
python-dotenv~=1.0.1