Revert "no module-global session (#597)"

This reverts commit c56412a1d5.

 - The Requests library advertises itself as thread safe.
 - Cookies aren't so interesting, but keep-alive /
   connection pooling would be nice to have.
This commit is contained in:
Johannes 2014-03-10 08:50:19 +00:00
parent fb038ecc30
commit 856a28a8a0

View file

@ -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')