From 8bb24e31343c9707493d3c37668ebb55c69bcc0e Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Fri, 30 Dec 2016 10:55:24 -0500 Subject: [PATCH] lyrics: Set User-Agent header (fix #2357) --- beetsplug/lyrics.py | 11 +++++++++-- docs/changelog.rst | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 645d525598..a642159c21 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -51,6 +51,7 @@ class HTMLParseError(Exception): from beets import plugins from beets import ui +import beets DIV_RE = re.compile(r'<(/?)div>?', re.I) @@ -71,6 +72,7 @@ class HTMLParseError(Exception): u'\u2016': u'-', u'\u2026': u'...', } +USER_AGENT = 'beets/{}'.format(beets.__version__) # Utilities. @@ -210,7 +212,9 @@ def fetch_url(self, url): # We're not overly worried about the NSA MITMing our lyrics scraper with warnings.catch_warnings(): warnings.simplefilter('ignore') - r = requests.get(url, verify=False) + r = requests.get(url, verify=False, headers={ + 'User-Agent': USER_AGENT, + }) except requests.RequestException as exc: self._log.debug(u'lyrics request failed: {0}', exc) return @@ -263,7 +267,10 @@ class Genius(Backend): def __init__(self, config, log): super(Genius, self).__init__(config, log) self.api_key = config['genius_api_key'].as_str() - self.headers = {'Authorization': "Bearer %s" % self.api_key} + self.headers = { + 'Authorization': "Bearer %s" % self.api_key, + 'User-Agent': USER_AGENT, + } def search_genius(self, artist, title): query = u"%s %s" % (artist, title) diff --git a/docs/changelog.rst b/docs/changelog.rst index 2f7d35fac5..9be90c766c 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -34,6 +34,8 @@ Fixes: * :doc:`/plugins/discogs`: Fix a crash when a release did not contain Format information, and increased robustness when other fields are missing. :bug:`2302` +* :doc:`/plugins/lyrics`: The plugin now reports a beets-specific User-Agent + header when requesting lyrics. :bug:`2357` For plugin developers: new importer prompt choices (see :ref:`append_prompt_choices`), you can now provide new candidates for the user to consider.