diff --git a/lyricsgenius/__init__.py b/lyricsgenius/__init__.py index eac9a87..3900c1f 100644 --- a/lyricsgenius/__init__.py +++ b/lyricsgenius/__init__.py @@ -14,4 +14,4 @@ __url__ = 'https://github.com/johnwmillr/LyricsGenius' __description__ = 'A Python wrapper around the Genius API' __license__ = 'MIT' -__version__ = '3.1.2' +__version__ = '3.2.0' diff --git a/lyricsgenius/api/api.py b/lyricsgenius/api/api.py index 3827c62..38ddf48 100644 --- a/lyricsgenius/api/api.py +++ b/lyricsgenius/api/api.py @@ -36,6 +36,8 @@ class API(Sender): sleep_time (:obj:`str`, optional): time to wait between requests. retries (:obj:`int`, optional): Number of retries in case of timeouts and errors with a >= 500 response code. By default, requests are only made once. + user_agent (:obj:`str`, optional): User agent for the request header. + proxy (:obj:`dict[str, str]`, optional): Proxy settings. Attributes: response_format (:obj:`str`, optional): API response format (dom, plain, html). @@ -55,6 +57,8 @@ def __init__(self, timeout=5, sleep_time=0.2, retries=0, + user_agent='', + proxy=None, ): super().__init__( access_token=access_token, @@ -62,6 +66,8 @@ def __init__(self, timeout=timeout, sleep_time=sleep_time, retries=retries, + user_agent=user_agent, + proxy=proxy, ) def account(self, text_format=None): @@ -524,6 +530,7 @@ def __init__( timeout=5, sleep_time=0.2, retries=0, + user_agent='', **kwargs ): @@ -538,5 +545,6 @@ def __init__( sleep_time=sleep_time, retries=retries, public_api_constructor=public_api_constructor, + user_agent=user_agent, **kwargs ) diff --git a/lyricsgenius/api/base.py b/lyricsgenius/api/base.py index e6e6d3d..60a395a 100644 --- a/lyricsgenius/api/base.py +++ b/lyricsgenius/api/base.py @@ -1,5 +1,6 @@ import time import os +import platform from json.decoder import JSONDecodeError import requests @@ -21,12 +22,17 @@ def __init__( sleep_time=0.2, retries=0, public_api_constructor=False, + user_agent='', + proxy=None, ): self._session = requests.Session() + user_agent_root = f'{platform.system()} {platform.release()}; Python {platform.python_version()}' self._session.headers = { 'application': 'LyricsGenius', - 'User-Agent': 'https://github.com/johnwmillr/LyricsGenius' + 'User-Agent': f'({user_agent}) ({user_agent_root})' if user_agent else user_agent_root, } + if proxy: + self._session.proxies = proxy if access_token is None: access_token = os.environ.get('GENIUS_ACCESS_TOKEN') diff --git a/lyricsgenius/genius.py b/lyricsgenius/genius.py index d51c07f..556f0bb 100644 --- a/lyricsgenius/genius.py +++ b/lyricsgenius/genius.py @@ -36,6 +36,8 @@ class Genius(API, PublicAPI): excluded terms with user's. Default excluded terms are listed below. retries (:obj:`int`, optional): Number of retries in case of timeouts and errors with a >= 500 response code. By default, requests are only made once. + user_agent (:obj:`str`, optional): User agent for the request header. + proxy (:obj:`dict[str, str]`, optional): Proxy settings. Attributes: verbose (:obj:`bool`, optional): Turn printed messages on or off. @@ -71,6 +73,8 @@ def __init__(self, access_token=None, skip_non_songs=True, excluded_terms=None, replace_default_terms=False, retries=0, + user_agent='', + proxy=None, ): # Genius Client Constructor super().__init__( @@ -78,7 +82,9 @@ def __init__(self, access_token=None, response_format=response_format, timeout=timeout, sleep_time=sleep_time, - retries=retries + retries=retries, + user_agent=user_agent, + proxy=proxy, ) self.verbose = verbose