Skip to content
Open
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
10 changes: 7 additions & 3 deletions lyrics_extractor/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class SongLyrics:
'lyricsmint': scraper_factory.lyricsmint_scraper,
}

headers = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to pass the user agent? Is this only to trick the websites thinking it's a legitimate user so it doesn't get blocked by the firewalls?

Does it fail without this header?

"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
}

def __init__(self, gcs_api_key: str, gcs_engine_id: str):
if type(gcs_api_key) != str or type(gcs_engine_id) != str:
raise TypeError("API key and engine ID must be a string.")
Expand All @@ -137,22 +141,22 @@ def __init__(self, gcs_api_key: str, gcs_engine_id: str):
self.GCS_ENGINE_ID = gcs_engine_id

def __handle_search_request(self, song_name):
url = "https://www.googleapis.com/customsearch/v1/siterestrict"
url = "https://www.googleapis.com/customsearch/v1"
params = {
'key': self.GCS_API_KEY,
'cx': self.GCS_ENGINE_ID,
'q': '{} lyrics'.format(song_name),
}

response = requests.get(url, params=params)
response = requests.get(url, params=params, headers=self.headers)
data = response.json()
if response.status_code != 200:
raise LyricScraperException(data)
return data

def __extract_lyrics(self, result_url, title):
# Get the page source code
page = requests.get(result_url)
page = requests.get(result_url, headers=self.headers)
source_code = BeautifulSoup(page.content, 'lxml')

self.scraper_factory(source_code, title)
Expand Down