Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 fix(lyrics): update Google Custom Search API endpoint #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 = {
"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