Skip to content

Commit

Permalink
genius.search_song returns 403 Forbidden (#288)
Browse files Browse the repository at this point in the history
* genius.search_song returns 403 Forbidden
Fixes #287 and Closes #289 by making User-Agent configurable and adding optional proxy support.

* Add optional user_agent argument

* UPDATE add[proxy], remove[headers]

* Add proxy docstring

---------

Co-authored-by: John William Ruth Miller <[email protected]>
Co-authored-by: Vauth <[email protected]>
  • Loading branch information
3 people authored Dec 22, 2024
1 parent 795a81b commit 7635cff
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lyricsgenius/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
8 changes: 8 additions & 0 deletions lyricsgenius/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -55,13 +57,17 @@ def __init__(self,
timeout=5,
sleep_time=0.2,
retries=0,
user_agent='',
proxy=None,
):
super().__init__(
access_token=access_token,
response_format=response_format,
timeout=timeout,
sleep_time=sleep_time,
retries=retries,
user_agent=user_agent,
proxy=proxy,
)

def account(self, text_format=None):
Expand Down Expand Up @@ -524,6 +530,7 @@ def __init__(
timeout=5,
sleep_time=0.2,
retries=0,
user_agent='',
**kwargs
):

Expand All @@ -538,5 +545,6 @@ def __init__(
sleep_time=sleep_time,
retries=retries,
public_api_constructor=public_api_constructor,
user_agent=user_agent,
**kwargs
)
8 changes: 7 additions & 1 deletion lyricsgenius/api/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import time
import os
import platform
from json.decoder import JSONDecodeError

import requests
Expand All @@ -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')

Expand Down
8 changes: 7 additions & 1 deletion lyricsgenius/genius.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -71,14 +73,18 @@ 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__(
access_token=access_token,
response_format=response_format,
timeout=timeout,
sleep_time=sleep_time,
retries=retries
retries=retries,
user_agent=user_agent,
proxy=proxy,
)

self.verbose = verbose
Expand Down

0 comments on commit 7635cff

Please sign in to comment.