Skip to content

Commit

Permalink
Make scrobbler plugin use proxy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitris Dalianis committed Sep 5, 2016
1 parent c6a2ac8 commit b103e16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
18 changes: 15 additions & 3 deletions mopidy_scrobbler/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ def __init__(self, config, core):
def on_start(self):
try:
self.lastfm = pylast.LastFMNetwork(
api_key=API_KEY, api_secret=API_SECRET,
username=self.config['scrobbler']['username'],
password_hash=pylast.md5(self.config['scrobbler']['password']))
api_key=API_KEY,
api_secret=API_SECRET
)
if self.config.get('proxy'):
self.lastfm.enable_proxy(
self.config['proxy']['hostname'],
self.config['proxy']['port']
)
self.lastfm.username = self.config['scrobbler']['username']
self.lastfm.password_hash = pylast.md5(
self.config['scrobbler']['password'])
sk_gen = pylast.SessionKeyGenerator(self.lastfm)
self.lastfm.session_key = sk_gen.get_session_key(
self.lastfm.username,
self.lastfm.password_hash)
logger.info('Scrobbler connected to Last.fm')
except (pylast.NetworkError, pylast.MalformedResponseError,
pylast.WSError) as e:
Expand Down
19 changes: 16 additions & 3 deletions tests/test_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,34 @@ def setUp(self):
'scrobbler': {
'username': 'alice',
'password': 'secret',
},
'proxy': {
'hostname': 'myhost',
'port': 8080
}
}
self.frontend = frontend_lib.ScrobblerFrontend(
self.config, mock.sentinel.core)

def test_on_start_creates_lastfm_network(self, pylast_mock):
self.frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
sk_gen = mock.Mock(spec=pylast.SessionKeyGenerator)
pylast_mock.SessionKeyGenerator.return_value = sk_gen
pylast_mock.md5.return_value = mock.sentinel.password_hash

self.frontend.on_start()

pylast_mock.LastFMNetwork.assert_called_with(
api_key=frontend_lib.API_KEY,
api_secret=frontend_lib.API_SECRET,
username='alice',
password_hash=mock.sentinel.password_hash)
api_secret=frontend_lib.API_SECRET)
self.frontend.lastfm.enable_proxy.assert_called_with(
'myhost',
8080)
pylast_mock.SessionKeyGenerator.assert_called_with(
self.frontend.lastfm)
sk_gen.get_session_key.assert_called_with(
'alice',
mock.sentinel.password_hash)

def test_on_start_stops_actor_on_error(self, pylast_mock):
pylast_mock.NetworkError = pylast.NetworkError
Expand Down

0 comments on commit b103e16

Please sign in to comment.