From 856a28a8a07273d8ddfdffb7e2df71fcebf70946 Mon Sep 17 00:00:00 2001 From: Johannes Date: Mon, 10 Mar 2014 08:50:19 +0000 Subject: [PATCH] Revert "no module-global session (#597)" This reverts commit c56412a1d54dd0f10c8ae20f3f846f66727f5fef. - The Requests library advertises itself as thread safe. - Cookies aren't so interesting, but keep-alive / connection pooling would be nice to have. --- beetsplug/fetchart.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/beetsplug/fetchart.py b/beetsplug/fetchart.py index 863365faa..b335cd32f 100644 --- a/beetsplug/fetchart.py +++ b/beetsplug/fetchart.py @@ -32,10 +32,12 @@ from beets import config IMAGE_EXTENSIONS = ['png', 'jpg', 'jpeg'] CONTENT_TYPES = ('image/jpeg',) DOWNLOAD_EXTENSION = '.jpg' -HEADERS = {'User-Agent': 'beets'} log = logging.getLogger('beets') +requests_session = requests.Session() +requests_session.headers = {'User-Agent': 'beets'} + def _fetch_image(url): """Downloads an image from a URL and checks whether it seems to @@ -44,14 +46,13 @@ def _fetch_image(url): """ log.debug(u'fetchart: downloading art: {0}'.format(url)) try: - with closing(requests.get(url, stream=True, headers=HEADERS)) as resp: + with closing(requests_session.get(url, stream=True)) as resp: if not resp.headers['Content-Type'] in CONTENT_TYPES: log.debug(u'fetchart: not an image') return # Generate a temporary file with the correct extension. - with NamedTemporaryFile(suffix=DOWNLOAD_EXTENSION, delete=False) \ - as fh: + with NamedTemporaryFile(suffix=DOWNLOAD_EXTENSION, delete=False) as fh: for chunk in resp.iter_content(): fh.write(chunk) log.debug(u'fetchart: downloaded art to: {0}'.format( @@ -101,7 +102,7 @@ def aao_art(asin): """Return art URL from AlbumArt.org given an ASIN.""" # Get the page from albumart.org. try: - resp = requests.get(AAO_URL, params={'asin': asin}, headers=HEADERS) + resp = requests_session.get(AAO_URL, params={'asin': asin}) log.debug(u'fetchart: scraped art URL: {0}'.format(resp.url)) except requests.RequestException: log.debug(u'fetchart: error scraping art page')