# Buggy code
def authenticate(self, c_key, c_secret):
    # Get the link for the OAuth page.
    auth_client = Client(USER_AGENT, c_key, c_secret)
    _, _, url = auth_client.get_authorize_url()
    beets.ui.print_("To authenticate with Discogs, visit:")
    beets.ui.print_(url)

    # Ask for the code and validate it.
    code = beets.ui.input_("Enter the code:")
    try:
        token, secret = auth_client.get_access_token(code)
    except DiscogsAPIError:
        raise beets.ui.UserError("Discogs authorization failed")

    # Save the token for later use.
    self._log.debug("Discogs token {0}, secret {1}", token, secret)
    with open(self._tokenfile(), "w") as f:
        json.dump({"token": token, "secret": secret}, f)

    return token, secret


# Traceback (socket.error)
Apply, More candidates, Skip, Use as-is, as Tracks, Group albums,
Enter search, enter Id, aBort? a
Traceback (most recent call last):
File "C:\Python27\Scripts\beet-script.py", line 9, in <module>
load_entry_point('beets==1.3.11', 'console_scripts', 'beet')()
File "c:\users\100557855\src\beets\beets\ui\__init__.py", line 953, in main
_raw_main(args)
File "c:\users\100557855\src\beets\beets\ui\__init__.py", line 943, in _raw_m
in
subcommand.func(lib, suboptions, subargs)
File "c:\users\100557855\src\beets\beets\ui\commands.py", line 870, in import
func
import_files(lib, paths, query)
File "c:\users\100557855\src\beets\beets\ui\commands.py", line 847, in import
files
session.run()
File "c:\users\100557855\src\beets\beets\importer.py", line 316, in run
pl.run_parallel(QUEUE_SIZE)
File "c:\users\100557855\src\beets\beets\util\pipeline.py", line 301, in run
out = self.coro.send(msg)
File "c:\users\100557855\src\beets\beets\util\pipeline.py", line 183, in coro
func(*(args + (task,)))
File "c:\users\100557855\src\beets\beets\importer.py", line 1227, in lookup_c
ndidates
task.lookup_candidates()
File "c:\users\100557855\src\beets\beets\importer.py", line 558, in lookup_ca
didates
autotag.tag_album(self.items)
File "c:\users\100557855\src\beets\beets\autotag\match.py", line 433, in tag_
lbum
search_album, va_likely)
File "c:\users\100557855\src\beets\beets\autotag\hooks.py", line 563, in albu
_candidates
out.extend(plugins.candidates(items, artist, album, va_likely))
File "c:\users\100557855\src\beets\beets\plugins.py", line 360, in candidates
out.extend(plugin.candidates(items, artist, album, va_likely))
File "c:\users\100557855\src\beets\beetsplug\discogs.py", line 121, in candid
tes
return self.get_albums(query)
File "c:\users\100557855\src\beets\beetsplug\discogs.py", line 173, in get_al
ums
return [self.get_album_info(release) for release in releases[:5]]
File "c:\users\100557855\src\beets\beetsplug\discogs.py", line 178, in get_al
um_info
artist, artist_id = self.get_artist([a.data for a in result.artists])
File "C:\Python27\lib\site-packages\discogs_client\models.py", line 96, in __
et__
return [wrapper_class(instance.client, d) for d in instance.fetch(self.name
[])]
File "C:\Python27\lib\site-packages\discogs_client\models.py", line 237, in f
tch
self.refresh()
File "C:\Python27\lib\site-packages\discogs_client\models.py", line 203, in r
fresh
data = self.client._get(self.data['resource_url'])
File "C:\Python27\lib\site-packages\discogs_client\client.py", line 110, in _
et
return self._request('GET', url)
File "C:\Python27\lib\site-packages\discogs_client\client.py", line 97, in _r
quest
content, status_code = self._fetcher.fetch(self, method, url, data=data, he
ders=headers)
File "C:\Python27\lib\site-packages\discogs_client\fetchers.py", line 73, in
etch
resp, content = self.oauth_client.request(url, method, headers=headers)
File "C:\Python27\lib\site-packages\oauth2\__init__.py", line 682, in request
connection_type=connection_type)
File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1593, in requ
st
(response, content) = self._request(conn, authority, uri, request_uri, meth
d, body, headers, redirections, cachekey)
File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1335, in _req
est
(response, content) = self._conn_request(conn, request_uri, method, body, h
aders)
File "C:\Python27\lib\site-packages\httplib2\__init__.py", line 1291, in _con
_request
response = conn.getresponse()
File "C:\Python27\lib\httplib.py", line 1074, in getresponse
response.begin()
File "C:\Python27\lib\httplib.py", line 415, in begin
version, status, reason = self._read_status()
File "C:\Python27\lib\httplib.py", line 371, in _read_status
line = self.fp.readline(_MAXLINE + 1)
File "C:\Python27\lib\socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
socket.error: [Errno 10054] An existing connection was forcibly closed by the r
mote host

# Fixed code
def authenticate(self, c_key, c_secret):
    # Get the link for the OAuth page.
    auth_client = Client(USER_AGENT, c_key, c_secret)
    _, _, url = auth_client.get_authorize_url()
    beets.ui.print_("To authenticate with Discogs, visit:")
    beets.ui.print_(url)

    # Ask for the code and validate it.
    code = beets.ui.input_("Enter the code:")
    try:
        token, secret = auth_client.get_access_token(code)
    except DiscogsAPIError:
        raise beets.ui.UserError("Discogs authorization failed")
    except (ConnectionError, socket.error) as e:
        self._log.debug("connection error: {0}", e)
        raise beets.ui.UserError("communication with Discogs failed")

    # Save the token for later use.
    self._log.debug("Discogs token {0}, secret {1}", token, secret)
    with open(self._tokenfile(), "w") as f:
        json.dump({"token": token, "secret": secret}, f)

    return token, secret