Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import pylast
import os
import yaml
import traceback
from beets import plugins
from beets import ui
from beets import config
from beets.util import normpath, plurality
from beets import library
LASTFM = pylast.LastFMNetwork(api_key=plugins.LASTFM_KEY)
PYLAST_EXCEPTIONS = (
pylast.WSError,
pylast.MalformedResponseError,
pylast.NetworkError,
)
REPLACE = {
u'\u2010': '-',
}
def deduplicate(seq):
"""Remove duplicates from sequence wile preserving order.
"""
seen = set()
return [x for x in seq if x not in seen and not seen.add(x)]
def _check_response_for_errors(self, response):
"""Checks the response for errors and raises one if any exists."""
try:
doc = minidom.parseString(_string(response))
except Exception as e:
raise MalformedResponseError(self.network, e)
e = doc.getElementsByTagName('lfm')[0]
if e.getAttribute('status') != "ok":
e = doc.getElementsByTagName('error')[0]
status = e.getAttribute('code')
details = e.firstChild.data.strip()
raise WSError(self.network, status, details)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Last.fm sensor platform."""
api_key = config[CONF_API_KEY]
users = config.get(CONF_USERS)
lastfm_api = lastfm.LastFMNetwork(api_key=api_key)
entities = []
for username in users:
try:
lastfm_api.get_user(username).get_image()
entities.append(LastfmSensor(username, lastfm_api))
except WSError as error:
_LOGGER.error(error)
return
add_entities(entities, True)
def _check_response_for_errors(self, response):
"""Checks the response for errors and raises one if any exists."""
try:
doc = minidom.parseString(_string(response))
except Exception as e:
raise MalformedResponseError(self.network, e)
e = doc.getElementsByTagName('lfm')[0]
if e.getAttribute('status') != "ok":
e = doc.getElementsByTagName('error')[0]
status = e.getAttribute('code')
details = e.firstChild.data.strip()
raise WSError(self.network, status, details)
mbid = ET.Element("musicBrainzId")
mbid.text = lf_album.get_mbid()
ainfo.append(mbid)
url = ET.Element("lastFmUrl")
url.text = lf_album.get_url(lang)
ainfo.append(url)
url = ET.Element("smallImageUrl")
url.text = lf_album.get_cover_image(pylast.SIZE_SMALL)
ainfo.append(url)
url = ET.Element("mediumImageUrl")
url.text = lf_album.get_cover_image(pylast.SIZE_MEDIUM)
ainfo.append(url)
url = ET.Element("largeImageUrl")
url.text = lf_album.get_cover_image(pylast.SIZE_EXTRA_LARGE)
ainfo.append(url)
except pylast.WSError as e:
log.error("Error talking to LastFM: %s / %s", str(e), e.get_id())
# wtf is this a string and an int?
if e.get_id() == str(pylast.STATUS_INVALID_PARAMS):
return self.makeResp(status=404)
else:
return self.makeResp(status=504)
except Exception as e:
log.error("Error talking to LastFM: " + str(e))
return self.makeResp(status=504)
return self.makeResp(child=ainfo)
if self.session:
return self.handleReq(self.session)
else:
with Session() as session:
return self.handleReq(session)
else:
return self.handleReq()
except MissingParam as e:
return self.makeResp(status=(Command.E_MISSING_PARAM, str(e)))
except NotFound as e:
return self.makeResp(status=(Command.E_NOT_FOUND, str(e)))
except InternalError as e:
return self.makeResp(status=(Command.E_GENERIC, str(e)))
except NoPerm as e:
return self.makeResp(status=(Command.E_PERM, str(e)))
except pylast.WSError as e:
return self.makeResp(status=(Command.E_LASTFM, str(e)))
except Exception as e:
print(e)
traceback.print_exc()
raise
def _getArt(obj, size=SIZE_EXTRA_LARGE):
try:
return obj.get_cover_image(size)
except WSError:
raise ValueError("{} not found.".format(obj.__class__.__name__))
def scrobble_track(artist, album, track):
""" Scrobble a track to the user's Last.fm account """
if not g.lastfm_network:
return
unix_timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
try:
g.lastfm_network.scrobble(artist=artist, title=track, album=album,
timestamp=unix_timestamp)
except (pylast.WSError, pylast.MalformedResponseError, pylast.NetworkError):
return
def _check_response_for_errors(self, response):
"""Checks the response for errors and raises one if any exists."""
try:
doc = minidom.parseString(_string(response))
except Exception as e:
raise MalformedResponseError(self.network, e)
e = doc.getElementsByTagName('lfm')[0]
if e.getAttribute('status') != "ok":
e = doc.getElementsByTagName('error')[0]
status = e.getAttribute('code')
details = e.firstChild.data.strip()
raise WSError(self.network, status, details)