Add requests session to reuse TCP connection (#159)

This commit is contained in:
sigma67 2021-01-27 14:59:59 +01:00
commit d4c132f10f
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
2 changed files with 20 additions and 5 deletions

View file

@ -7,7 +7,7 @@ from ytmusicapi.ytmusic import YTMusic # noqa: E402
config = configparser.RawConfigParser()
config.read('./test.cfg', 'utf-8')
youtube = YTMusic()
youtube = YTMusic(requests_session=False)
youtube_auth = YTMusic(config['auth']['headers_file'])
youtube_brand = YTMusic(config['auth']['headers'], config['auth']['brand_account'])

View file

@ -24,9 +24,11 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi
Permits both authenticated and non-authenticated requests.
Authentication header data must be provided on initialization.
"""
def __init__(self,
auth: str = None,
user: str = None,
requests_session=True,
proxies: dict = None,
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
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/
: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_.
.. _requests: https://requests.readthedocs.io/
@ -52,6 +58,15 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi
the ytmusicapi/locales directory.
"""
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
try:
@ -104,10 +119,10 @@ class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMi
if self.auth:
origin = self.headers.get('origin', self.headers.get('x-origin'))
self.headers["Authorization"] = get_authorization(self.sapisid + ' ' + origin)
response = requests.post(base_url + endpoint + params + additionalParams,
json=body,
headers=self.headers,
proxies=self.proxies)
response = self._session.post(base_url + endpoint + params + additionalParams,
json=body,
headers=self.headers,
proxies=self.proxies)
response_text = json.loads(response.text)
if response.status_code >= 400:
message = "Server returned HTTP " + str(