Add requests session to reuse TCP connection (#159)
This commit is contained in:
parent
8f4dfd19ee
commit
d4c132f10f
2 changed files with 20 additions and 5 deletions
|
|
@ -7,7 +7,7 @@ from ytmusicapi.ytmusic import YTMusic # noqa: E402
|
||||||
config = configparser.RawConfigParser()
|
config = configparser.RawConfigParser()
|
||||||
config.read('./test.cfg', 'utf-8')
|
config.read('./test.cfg', 'utf-8')
|
||||||
|
|
||||||
youtube = YTMusic()
|
youtube = YTMusic(requests_session=False)
|
||||||
youtube_auth = YTMusic(config['auth']['headers_file'])
|
youtube_auth = YTMusic(config['auth']['headers_file'])
|
||||||
youtube_brand = YTMusic(config['auth']['headers'], config['auth']['brand_account'])
|
youtube_brand = YTMusic(config['auth']['headers'], config['auth']['brand_account'])
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,11 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi
|
||||||
Permits both authenticated and non-authenticated requests.
|
Permits both authenticated and non-authenticated requests.
|
||||||
Authentication header data must be provided on initialization.
|
Authentication header data must be provided on initialization.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
auth: str = None,
|
auth: str = None,
|
||||||
user: str = None,
|
user: str = None,
|
||||||
|
requests_session=True,
|
||||||
proxies: dict = None,
|
proxies: dict = None,
|
||||||
language: str = 'en'):
|
language: str = 'en'):
|
||||||
"""
|
"""
|
||||||
|
|
@ -42,6 +44,10 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi
|
||||||
Otherwise the default account is used. You can retrieve the user ID
|
Otherwise the default account is used. You can retrieve the user ID
|
||||||
by going to https://myaccount.google.com/brandaccounts and selecting your brand account.
|
by going to https://myaccount.google.com/brandaccounts and selecting your brand account.
|
||||||
The user ID will be in the URL: https://myaccount.google.com/b/user_id/
|
The user ID will be in the URL: https://myaccount.google.com/b/user_id/
|
||||||
|
:param requests_session: A Requests session object or a truthy value to create one.
|
||||||
|
A falsy value disables sessions.
|
||||||
|
It is generally a good idea to keep sessions enabled for
|
||||||
|
performance reasons (connection pooling).
|
||||||
:param proxies: Optional. Proxy configuration in requests_ format_.
|
:param proxies: Optional. Proxy configuration in requests_ format_.
|
||||||
|
|
||||||
.. _requests: https://requests.readthedocs.io/
|
.. _requests: https://requests.readthedocs.io/
|
||||||
|
|
@ -52,6 +58,15 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi
|
||||||
the ytmusicapi/locales directory.
|
the ytmusicapi/locales directory.
|
||||||
"""
|
"""
|
||||||
self.auth = auth
|
self.auth = auth
|
||||||
|
|
||||||
|
if isinstance(requests_session, requests.Session):
|
||||||
|
self._session = requests_session
|
||||||
|
else:
|
||||||
|
if requests_session: # Build a new session.
|
||||||
|
self._session = requests.Session()
|
||||||
|
else: # Use the Requests API module as a "session".
|
||||||
|
self._session = requests.api
|
||||||
|
|
||||||
self.proxies = proxies
|
self.proxies = proxies
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -104,10 +119,10 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi
|
||||||
if self.auth:
|
if self.auth:
|
||||||
origin = self.headers.get('origin', self.headers.get('x-origin'))
|
origin = self.headers.get('origin', self.headers.get('x-origin'))
|
||||||
self.headers["Authorization"] = get_authorization(self.sapisid + ' ' + origin)
|
self.headers["Authorization"] = get_authorization(self.sapisid + ' ' + origin)
|
||||||
response = requests.post(base_url + endpoint + params + additionalParams,
|
response = self._session.post(base_url + endpoint + params + additionalParams,
|
||||||
json=body,
|
json=body,
|
||||||
headers=self.headers,
|
headers=self.headers,
|
||||||
proxies=self.proxies)
|
proxies=self.proxies)
|
||||||
response_text = json.loads(response.text)
|
response_text = json.loads(response.text)
|
||||||
if response.status_code >= 400:
|
if response.status_code >= 400:
|
||||||
message = "Server returned HTTP " + str(
|
message = "Server returned HTTP " + str(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue