def get_cover_art(self): """Attempt to get the album's cover art from Last.fm.""" try: pylast except NameError: return False album_id = unicodedata.normalize('NFKD', unicode(' '.join([self.artist.name, self.title]))).encode('ascii', 'ignore') album_hash = hashlib.sha1(album_id).hexdigest() lastfm = pylast.get_lastfm_network(api_key=settings.LASTFM_API_KEY) lastfm_album = lastfm.get_album(self.artist.name, self.title) try: cover_image = lastfm_album.get_cover_image() response = urllib2.urlopen(cover_image) data = response.read() # Store the photo on disk and link the photo to the album. filename = os.path.join(settings.MEDIA_ROOT, Album.PHOTO_UPLOAD_DIRECTORY, '%s.jpg' % album_hash) fh = open(filename, 'w') fh.write(data) fh.close() self.cover_art = os.path.join(Album.PHOTO_UPLOAD_DIRECTORY, '%s.jpg' % album_hash) self.save() except (pylast.WSError, AttributeError, urllib2.HTTPError): pass
def clicked(self, *ignore): if self.auth_url: def err(e): logging.error(e) self.set_button_text() get_worker().send(self.sg.get_web_auth_session_key, (self.auth_url,), self.setkey, err) self.button.set_label("Checking...") self.button.set_sensitive(False) self.auth_url = False elif self.enabled: self.setkey(False) else: self.network = pylast.get_lastfm_network(api_key=API_KEY, api_secret=API_SECRET) self.sg = pylast.SessionKeyGenerator(self.network) def callback(url): self.auth_url = url self.set_button_text() webbrowser.open(self.auth_url) get_worker().send(self.sg.get_web_auth_url, (), callback) self.button.set_label("Connecting...") self.button.set_sensitive(False)
def clicked(self, *ignore): if self.auth_url: def err(e): logging.error(e) self.set_button_text() get_worker().send(self.sg.get_web_auth_session_key, (self.auth_url, ), self.setkey, err) self.button.set_label("Checking...") self.button.set_sensitive(False) self.auth_url = False elif self.enabled: self.setkey(False) else: self.network = pylast.get_lastfm_network(api_key=API_KEY, api_secret=API_SECRET) self.sg = pylast.SessionKeyGenerator(self.network) def callback(url): self.auth_url = url self.set_button_text() open_browser(self.auth_url) get_worker().send(self.sg.get_web_auth_url, (), callback) self.button.set_label("Connecting...") self.button.set_sensitive(False)
def lfmInit(user): #initialize lfm objects url = user['lfm_url'] network = pylast.get_lastfm_network(os.environ.get('LFM_APIKEY'), os.environ.get('LFM_SECRET')) sg = pylast.SessionKeyGenerator(network) sg.web_auth_tokens[url] = url[(url.index('token')+6):] sg.api_key = os.environ.get('LFM_APIKEY') sg.api_secret = os.environ.get('LFM_SECRET') #try to authorize token try: session_key = sg.get_web_auth_session_key(user['lfm_url']) print session_key except pylast.WSError: print "Error authorizing user for last.fm" markUser(user, 'LASTFM INIT ERROR') #success, add new session key and remove auth token else: getDatabase().users.update( {'userid': user['userid'], 'version': user['version']}, { '$set':{ 'lfm_session': session_key, 'status': 'working' }, '$unset':{ 'lfm_url': True } })
def get_biography(self): """Import artist briography from Last.fm.""" # Make sure the pylast module is available. try: pylast except NameError: return False # Open a connection to the Last.fm API. lastfm = pylast.get_lastfm_network(api_key=settings.LASTFM_API_KEY) lastfm_artist = lastfm.get_artist(self.name) try: # Get the biography from Last.fm. biography = lastfm_artist.get_bio_content() bio_published_date = lastfm_artist.get_bio_published_date() if bio_published_date: plain_text_biography = strip_tags(biography) # If the biography is different to the saved version or the # artist biography is blank, save the artist's biography. # Note this will overwrite any changes that have been made # to the biography via the admin. if (not plain_text_biography == self.biography or self.biography == ''): self.biography = plain_text_biography self.save() except (pylast.WSError, urllib2.URLError, urllib2.HTTPError): # An error occurs if the artist's name has a character in that # Last.fm doesn't like, or on the occasional dropped connection. pass
def get_cover_art(self): """Attempt to get the album's cover art from Last.fm.""" try: pylast except NameError: return False album_id = unicodedata.normalize( 'NFKD', unicode(' '.join([self.artist.name, self.title]))).encode('ascii', 'ignore') album_hash = hashlib.sha1(album_id).hexdigest() lastfm = pylast.get_lastfm_network(api_key=settings.LASTFM_API_KEY) lastfm_album = lastfm.get_album(self.artist.name, self.title) try: cover_image = lastfm_album.get_cover_image() response = urllib2.urlopen(cover_image) data = response.read() # Store the photo on disk and link the photo to the album. filename = os.path.join(settings.MEDIA_ROOT, Album.PHOTO_UPLOAD_DIRECTORY, '%s.jpg' % album_hash) fh = open(filename, 'w') fh.write(data) fh.close() self.cover_art = os.path.join(Album.PHOTO_UPLOAD_DIRECTORY, '%s.jpg' % album_hash) self.save() except (pylast.WSError, AttributeError, urllib2.HTTPError): pass
def _main(self): """ get's automatically called from Memacs class """ options = { 'api_secret': self._get_config_option('api_secret'), 'api_key': self._get_config_option('api_key'), 'password_hash': pylast.md5(self._get_config_option('password')), 'username': self._get_config_option('username') } try: if 'lastfm' in self._get_config_option('network'): network = pylast.get_lastfm_network(**options) if 'librefm' in self._get_config_option('network'): network = pylast.get_librefm_network(**options) user = network.get_user(options['username']) self._handle_recent_tracks(user.get_recent_tracks(limit=100)) except pylast.WSError, e: logging.error('an issue with the network web service occured: %s' % e) sys.exit(1)
def fetchTopTagStats(self, retries=3): ''' LastFM provides a unified list tags/counts, for the top tags. By fetching these in one call, we can typically avoid a ton of unnecessary network calls for individual tags. ''' tags = {} try: lastfm = pylast.get_lastfm_network(api_key=self.api_key, api_secret=self.api_secret) lastTopTags = lastfm.get_top_tags(10000) for lastTopTag in lastTopTags: if (self.useNamedTuple): key = unicode(lastTopTag.item.name).lower() count = int(lastTopTag.weight) else: key = unicode(lastTopTag['item'].name).lower() count = int(lastTopTag['weight']) if (key in tags): common.safeStderr( 'Duplicate tag retrieved from lastFM, merging counts: ' + lastTopTag) tags[key] += count else: tags[key] = count return tags except Exception, err: if (retries > 0): common.safeStderr('Problem retrieving top tag information, ' + str(retries) + ' retries left: ' + str(err)) time.sleep(5) return self.fetchTopTagStats(retries - 1) else: common.safeStderr('Problem retrieving top tag information, ' + str(retries) + ' skipping: ' + str(err))
def fetchTopTagStats(self, retries=2): ''' LastFM provides a unified list tags/counts, for the top tags. By fetching these in one call, we can typically avoid a ton of unnecessary network calls for individual tags. ''' tags = {} try: lastfm = pylast.get_lastfm_network(api_key=self.api_key, api_secret=self.api_secret) lastTopTags = lastfm.get_top_tags(10000) for lastTopTag in lastTopTags: if (self.useNamedTuple): key = str(lastTopTag.item.name).lower() count = int(lastTopTag.weight) elif type(lastTopTag) == pylast.TopItem: key = str(lastTopTag.item.get_name()).lower() count = int(lastTopTag.weight) else: key = str(lastTopTag['item'].name).lower() count = int(lastTopTag['weight']) if (key in tags): self.outputWrapper.logError('Duplicate tag retrieved from lastFM, merging counts: ' + lastTopTag) tags[key] += count else: tags[key] = count return tags except Exception as err: if (retries > 0): self.outputWrapper.logError('Problem retrieving top tag information, ' + str(retries) + ' retries left: ' + str(err)) time.sleep(5) return self.fetchTopTagStats(retries - 1) else: self.outputWrapper.logError('Problem retrieving top tag information, ' + str(retries) + ' skipping: ' + str(err)) return None
def fetchArtistTags(self, artist, maxTagsToFetch, minWeight, retries=3): ''' Retrieve artist tags from LastFM, filtering out those tags that appear bunky (below the specified weight, longer than the maximum allowable distance, self-referential, etc. ''' try: lastfm = pylast.get_lastfm_network(api_key=self.api_key, api_secret=self.api_secret) tags = self.processSeenTags( lastfm.get_artist(artist).get_top_tags(limit=maxTagsToFetch), minWeight) return filter( lambda pair: pair[0].lower().replace('the', '').strip() != artist.replace('the', '').strip(), tags) except Exception, err: if ('no artist' in str(err).lower()): return [] if (retries > 0): common.safeStderr( 'Problem retrieving artist tag information for [' + artist + '], ' + str(retries) + ' retries left: ' + str(err)) time.sleep(5) return self.fetchArtistTags(artist, maxTagsToFetch, minWeight, retries - 1) else: common.safeStderr( 'Problem retrieving artist tag information for [' + artist + '], skipping: ' + str(err))
def lastfm(inp, reply=None, say=None, nick=''): API_KEY = "30cabd8b57c765a42f78e8f5d329fdc0" API_SECRET = "0ab2d0a46763e71d1ed8877b4ea209cf" username = "******" password_hash = pylast.md5("justpurple") network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash) try: if inp == '': user = network.get_user(nick) else: user = network.get_user(inp) nick = inp except WSError: say("This user doesn't exist.") try: tracks = user.get_recent_tracks() except pylast.WSError: if inp == '': return("It seems like your current nickname does not have a lastFM account. Try '.lastfm username' instead.") else: return("The user '%s' does not exist. Maybe you misspelled it?" % inp) recent = tracks[0][0] artist = recent.get_artist().get_name() title = recent.get_title() url = recent.get_url() bitlyURL = bitly_api.shorten(url) finalise = "\x02%s\x0F's last track - \x02%s\x0f :: Artist - \x02%s\x0f :: Link to Song - \x02%s\x0F" % (nick, title, artist, bitlyURL) say(finalise)
def lfm_artists(username, cutoff=20, period = '12month'): net = pylast.get_lastfm_network(LAST_KEY, LAST_SECRET) user = pylast.User(username, net) artists = user.get_top_artists(period=period) return {'username':user.get_name(), 'top_artists':\ map(lambda artist:(artist.item.name, artist.weight), artists[:20])}
def getNetworkObject(API_KEY, API_SECRET): """Gets the network object needed to get information.""" network = None try: network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET) network.enable_caching() # enables caching for certain calls (see pylast.py) except pylast.WSError, e: irc.error("Houston, we have a problem. %s" % e.get_id())
def getHits(artist_name): network = pylast.get_lastfm_network(api_key = config.LAST_FM_API_KEY, api_secret = config.LAST_FM_API_KEY) artist = network.get_artist(artist_name) try: hits = artist.get_top_tracks() except pylast.WSError: return [] return [hit.item.title for hit in hits]
def crawl(username_filename): network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET) for username in open(username_filename, 'r').readlines(): try: crawl_user_data(username.strip(), network) except: sys.stderr.write('ERROR in processing user %s; continuing on.\n' % username)
def init_thread(self): username = FCBase().lfm_login password = FCBase().lfm_password if not username or username == "l_user_" or not password: logging.debug("No last.fm account provided") return None if not self.controls.net_wrapper.is_internet(): # try again... time.sleep(5) if not self.controls.net_wrapper.is_internet(): return None logging.debug("RUN INIT LAST.FM") password_hash = pylast.md5(password) self.cache = None try: self.network = pylast.get_lastfm_network( api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash) self.cache = Cache(self.network) """scrobbler""" scrobbler_network = pylast.get_lastfm_network( username=username, password_hash=password_hash) self.scrobbler = scrobbler_network.get_scrobbler("fbx", "1.0") except: self.network = None self.scrobbler = None self.controls.statusbar.set_text( "Error last.fm connection with %s/%s" % (username, FCBase().lfm_password)) logging.error( "Either invalid last.fm login or password or network problems") """ val = show_login_password_error_dialog(_("Last.fm connection error"), _("Verify user and password"), username, FC().lfm_password) if val: FC().lfm_login = val[0] FC().lfm_password = val[1] return False """ return True
def main(): config = configparser.ConfigParser() config.read('config.ini') # last.fm initialization last_fm = config['Last.fm'] last_fm_api_key = 'bbad67cabcce9598501d485b701698f1' if not last_fm['username']: print('Last.fm is required to use Tune Saver. Provide your Last.fm username in config.ini') quit() ln = pylast.get_lastfm_network(api_key=last_fm_api_key) user = pylast.User(last_fm['username'], ln) # The order these appear in this list will determine the order they appear # in the selection prompt. # List of (constructor, config) tuples service_constructors = [ # Highest priority: The ability to download the track directly. # Try the services with better search capabilities first. (jamendo.Jamendo, config['Jamendo']), (fma.FMA, config['Free Music Archive']), (soundcloud_download.SoundcloudDownload, config['Soundcloud']), # Failing that, try to save to a streaming music service (spotify.Spotify, config['Spotify']), (soundcloud_playlist.SoundcloudPlaylist, config['Soundcloud']), # Last free resort: maybe it's available in video form (youtube.Youtube, config['YouTube']), # If the track cannot be obtained for free, look for a way to buy it. (last_fm_purchase.LastFmPurchase, config['Last.fm']), ] services = [] for sc, config_section in service_constructors: if not all(value for _,value in config_section.items()): print('To use {}, fill out all of its fields in config.ini'.format( config_section.name)) continue try: services.append(sc(config_section)) except Exception as e: # This service could not be initialized. Print the reason, # but continue attempting to initialize other services. print(str(e)) # Main input loop while True: input('\nPress enter to save song') # Fetch the song from the Last.fm api track = most_current_track(user) if not track: print('Could not get a track from Last.fm.') continue print('\nNow Playing: {} - {}\n'.format( track.artist.name, track.title)) save_track(track, services)
def oauth_callback(self, request): sg = pylast.SessionKeyGenerator(self.network) sg.web_auth_tokens["fake"] = request.args.get("token") sk = sg.get_web_auth_session_key("fake") user = {} for k in pylast.get_lastfm_network(self.api_key, self.api_secret, session_key=sk).get_authenticated_user()._request("user.getInfo", True).getElementsByTagName('*'): if k.firstChild and k.firstChild.nodeValue.strip() != "": user[k.tagName] = k.firstChild.nodeValue return (int(user["id"]), dict(user, session_key=sk))
def plot(artistName, numArtists, API_KEY, sizeX, sizeY): size(sizeX,sizeY) network = pylast.get_lastfm_network(api_key = API_KEY) network.enable_caching() font("Gill Sans MT Pro Medium", 24) text(artistName, WIDTH/10, HEIGHT/10) font("Gill Sans MT Pro Light", 12) artist = network.get_artist(artistName) similares = artist.get_similar(numArtists) posiciones = [] colormode(HSB) for sim in similares: artSimil = sim[0].name angle = 0 noFound = True while noFound: #Brutish collision management angle = random.randint(0,360/numArtists+1) if (angle in posiciones): noFound = True else: posiciones.append(angle) noFound = False angle = angle * (numArtists+1) dist = WIDTH/2 - (sim[1]*WIDTH/2) if (dist < WIDTH/10): dist = dist+WIDTH/10 X = coordinateX(WIDTH/2,HEIGHT/2, dist, angle) Y = coordinateY(WIDTH/2,HEIGHT/2, dist, angle) fill(angle/360.0, 0.8*360, 0.5) if (angle > 90 and angle < 270): angle = angle + 180 align(RIGHT) else: align(LEFT) push() rotate(angle) text(artSimil, X, Y, width=50) pop()
def login(self, query): form = self._deserialize(query) storage = harkfm.Storage() api_key = harkfm.Engine.config['apis']['last.fm']['key'] api_secret = harkfm.Engine.config['apis']['last.fm']['secret'] try: network = pylast.get_lastfm_network(api_key, api_secret) session_key = pylast.SessionKeyGenerator(network).get_session_key(form['username'], pylast.md5(form['password'])) storage.config_set('apis/last.fm/session_key', session_key) interface = harkfm.Interface() interface.index() except Exception as e: harkfm.Interface.logger.error('%s %s', type(e), e)
def connect_to_lastfm(self): print "Connecting to Last.fm", password_hash = pylast.md5( self.param['lastfm_password'] ) try: self.network = pylast.get_lastfm_network( api_key = self.param['lastfm_api'], \ api_secret = self.param['lastfm_secret'], \ username = self.param['lastfm_username'], \ password_hash = password_hash) except: print "\tcouldn't establish connection. Sorry :( '" exit() else: print "successfully! :)"
def handle_last(t, s, p): if not p: s.syntax(t, 'last') else: p = p.strip() try: api = pylast.get_lastfm_network(lastfm_key) seq = api.get_user(p).get_recent_tracks(limit=1) mesg = u'Последний трек пользователя %s:\n' % (p) for item in seq: mesg +='%s - %s (%s)\n' % (item[0].get_artist().get_name(), item[0].get_name(), item[1]) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def album_art(track_id, **kw): tr = Track.objects.get(pk=track_id) try: fm = pylast.get_lastfm_network(api_key=settings.LAST_FM_KEY) alb = fm.get_album(tr.artist, tr.album) (Track.objects.filter(pk=track_id).update( large_art_url=alb.get_cover_image(pylast.COVER_LARGE), medium_art_url=alb.get_cover_image(pylast.COVER_MEDIUM), small_art_url=alb.get_cover_image(pylast.COVER_SMALL))) except pylast.WSError: # Probably album not found log.exception('in album_art') print 'got artwork for %s' % tr
def album_art(track_id, **kw): tr = Track.objects.get(pk=track_id) try: fm = pylast.get_lastfm_network(api_key=settings.LAST_FM_KEY) alb = fm.get_album(tr.artist, tr.album) (Track.objects.filter(pk=track_id) .update(large_art_url=alb.get_cover_image(pylast.COVER_LARGE), medium_art_url=alb.get_cover_image(pylast.COVER_MEDIUM), small_art_url=alb.get_cover_image(pylast.COVER_SMALL))) except pylast.WSError: # Probably album not found log.exception('in album_art') print 'got artwork for %s' % tr
def __init__(self, name, type, username, password, password_cmd, password_hash, submit_url, client_version): self.name = name self.username = username self.type = type if password_cmd: password = os.popen(password_cmd).read() if not password_hash: password_hash = hashlib.md5(password).hexdigest() if type == "lastfm": self.network = pylast.get_lastfm_network(username = username, password_hash = password_hash) elif type == "librefm": self.network = pylast.get_librefm_network(username = username, password_hash = password_hash) elif type == "custom": self.network = pylast.get_lastfm_network(username = username, password_hash = password_hash) self.network.submission_server = submit_url self.scrobbler = self.network.get_scrobbler("sth", client_version) self.cache = []
def handle_last(t, s, p): if not p: s.syntax(t, 'last') else: p = p.strip() try: api = pylast.get_lastfm_network(lastfm_key) seq = api.get_user(p).get_recent_tracks(limit=1) mesg = u'Последний трек пользователя %s:\n' % (p) for item in seq: mesg += '%s - %s (%s)\n' % (item[0].get_artist().get_name(), item[0].get_name(), item[1]) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def connect_to_lastfm(): CONF_FILE = join(getenv("XDG_CONFIG_HOME", expanduser("~/.config")), "mpdt", "config") conf = {} with open(CONF_FILE, "r") as configfile: for i in configfile.readlines(): i = i.split(" ") conf[i[0]] = i[1][:-1] network = pylast.get_lastfm_network( api_key=conf["api_key"], api_secret=conf["api_secret"], username=conf["username"], password_hash=conf["password"], ) return network
def handle_now(t, s, p): if not p: s.syntax(t, 'now') else: p = p.strip() try: api = pylast.get_lastfm_network(lastfm_key) seq = api.get_user(p).get_now_playing() if seq: mesg = u'Пользователь %s сейчас слушает ' % (p) mesg += '%s - %s' % (seq.get_artist().get_name(), seq.get_name()) else: mesg = u'Пользователь %s сейчас ничего не слушает.' % (p) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def populate_release_lastfm(release_group, release_mbid): """ Make sure all required and avaiable last.fm data is present in a CachedReleaseGroup document. @param release_group: a CachedReleaseGroup object @param release_mbid: a string containing a MusicBrainz ID of a release @return: a validated/updated CachedReleaseGroup object """ #if release_group.cache_state['lastfm'][0] == 0: release = release_group.releases[release_mbid] if 'lastfm' not in release_group.cache_state: lastfm = pylast.get_lastfm_network(api_key = settings.LASTFM_API_KEY) lastfm.enable_caching() try: t = mmda_logger('last','request','release-data',release_mbid) lastfm_album = lastfm.get_album_by_mbid(release_mbid) lastfm_abstract = None lastfm_cover = None lastfm_url = lastfm_album.get_url() if 'abstract' not in release_group: lastfm_abstract = Future(lastfm_album.get_wiki_summary) if 'cover' not in release: lastfm_cover = lastfm_album.get_cover_image() # wait for Future if 'abstract' not in release_group: lastfm_abstract() mmda_logger('last','result','release-data',release_mbid,t) except Exception, e: mmda_logger('pylast','ERROR',e) else: if 'urls' not in release: release['urls'] = {} release['urls']['Last.fm'] = [lastfm_url] if lastfm_abstract and lastfm_abstract(): release_group.abstract = {'content':strip_tags(lastfm_abstract()), 'lang':'en', 'provider':'Last.fm', 'url':lastfm_url} if lastfm_cover: release['cover'] = lastfm_cover # TODO: when to save? when failed do we retry? release_group.cache_state['lastfm'] = [1,datetime.utcnow()] release_group.changes_present = True
def handle_noargs(self, **options): """ Use the Last.fm API to link similar artists. The similarity is based in listenership. """ try: pylast except NameError: return False # Create the logger we'll use to store all the output. logging.config.fileConfig("logging.conf") logger = logging.getLogger('RippedRecordsLogger') logger.info('Linking similar artists.') # Create a connection to the Last.fm API. lastfm = pylast.get_lastfm_network(api_key=settings.LASTFM_API_KEY) # Loop through every published artist and look on Last.fm for similar # artists that are also in the system? for artist in Artist.objects.published(): db_similar_artists = artist.similar_artists.published() try: lastfm_artist = lastfm.get_artist(artist.name) similar_artists = lastfm_artist.get_similar() except pylast.WSError: # Couldn't find the artist on Last.fm. continue for similar_artist in similar_artists: # Based on a little bit of research, a match of .25 or greater # seems to be a good benchmark for similarity. if similar_artist["match"] >= 0.25: try: db_similar_artist = Artist.objects.get( name=str(similar_artist["item"])) # If this is a new similar artist, add it to the set. if db_similar_artist not in db_similar_artists: artist.similar_artists.add(db_similar_artist) logger.info("%s similar (%f) to %s." % (db_similar_artist.name, similar_artist["match"], artist)) # Make the reciprocal link if it doesn't exist. if artist not in \ db_similar_artist.similar_artists.published(): db_similar_artist.similar_artists.add(artist) logger.info("%s similar (%f) to %s." % (artist, similar_artist["match"], db_similar_artist.name)) except Artist.DoesNotExist: pass
def handle_noargs(self, **options): """ Use the Last.fm API to link similar artists. The similarity is based in listenership. """ try: pylast except NameError: return False # Create the logger we'll use to store all the output. logging.config.fileConfig("logging.conf") logger = logging.getLogger("RippedRecordsLogger") logger.info("Linking similar artists.") # Create a connection to the Last.fm API. lastfm = pylast.get_lastfm_network(api_key=settings.LASTFM_API_KEY) # Loop through every published artist and look on Last.fm for similar # artists that are also in the system? for artist in Artist.objects.published(): db_similar_artists = artist.similar_artists.published() try: lastfm_artist = lastfm.get_artist(artist.name) similar_artists = lastfm_artist.get_similar() except pylast.WSError: # Couldn't find the artist on Last.fm. continue for similar_artist in similar_artists: # Based on a little bit of research, a match of .25 or greater # seems to be a good benchmark for similarity. if similar_artist["match"] >= 0.25: try: db_similar_artist = Artist.objects.get(name=str(similar_artist["item"])) # If this is a new similar artist, add it to the set. if db_similar_artist not in db_similar_artists: artist.similar_artists.add(db_similar_artist) logger.info( "%s similar (%f) to %s." % (db_similar_artist.name, similar_artist["match"], artist) ) # Make the reciprocal link if it doesn't exist. if artist not in db_similar_artist.similar_artists.published(): db_similar_artist.similar_artists.add(artist) logger.info( "%s similar (%f) to %s." % (artist, similar_artist["match"], db_similar_artist.name) ) except Artist.DoesNotExist: pass
def get_basic_cached_search_result(query_type, query_string): """ Make sure proper CachedSearchResult is present and return its id. Method performs local, then optional remote (MusicBrainz) lookup of query result @param query_type: a string containing query type @param query_string: a string containing query @return: a string containing SHA1 hash of a query string (the ID of a CachedSearchResult document) """ query_id = hashlib.sha1((query_type+query_string).encode('utf-8')).hexdigest() search_result = CachedSearchResult.get_or_create(query_id) search_result.query_string = query_string search_result.query_type = query_type if 'mb' not in search_result.cache_state: #TODO: add 14day window check try: t = mmda_logger('mb','request','search for',query_string) if query_type == 'artist': filter = ws.ArtistFilter(name=query_string,limit=RESULTS_LIMIT) results = mb_query.getArtists(filter) #TODO: add try, or maybe better in 'create_search' as a global wrapper search_result.results = [ {'name':r.artist.name, 'mbid':extractUuid(r.artist.id), 'score':r.score, 'note':r.artist.disambiguation } for r in results ] elif query_type == 'release': filter = ws.ReleaseFilter(title=query_string,limit=RESULTS_LIMIT) results = mb_query.getReleases(filter) #TODO: add try, or maybe better in 'create_search' as a global wrapper search_result.results = [ {'artist':r.release.artist.name, 'title':r.release.title, 'mbid':extractUuid(r.release.id), 'artist_mbid':extractUuid(r.release.artist.id), 'score':r.score, 'tracks_count':r.release.tracksCount, 'year':r.release.getEarliestReleaseEvent().getDate() if r.release.getEarliestReleaseEvent() else None} for r in results ] elif query_type == 'tag': # TODO: refactor to other packages import pylast lastfm = pylast.get_lastfm_network(api_key = settings.LASTFM_API_KEY) lastfm_similar_tags = lastfm.search_for_tag(query_string).get_next_page() search_result.results = [ tag.name for tag in lastfm_similar_tags ] except Exception, e: # TODO: hard error here mmda_logger('search','ERROR',e) raise e else: mmda_logger('mb','result','results',len(search_result.results),t) search_result.cache_state['mb'] = [1,datetime.utcnow()] search_result.save()
def trends_for(username): api = pylast.get_lastfm_network(lastfm_key) user = api.get_user(username) known_artists = get_known_artists(user) friends = user.get_friends() print "Looking for trends among %s's %d friends" % (username, len(friends)) trends = get_trends(friends) for artist in trends.keys(): if artist in known_artists or trends[artist][1] == []: del trends[artist] trendy_artists = trends.keys() trendy_artists.sort(key = lambda a: \ len(trends[a][0]) / float(len(trends[a][1]) + len(trends[a][0]))) print "Trendy artists for %s:" % username for artist in trendy_artists[:3]: print "%s, already known by %s, recently discovered by %s" % \ (artist, trends[artist][0], trends[artist][1])
def __init__(self): BaseActionPlugin.__init__(self) try: parser = SafeConfigParser() parser.read('plugins/lastfm.yapsy-plugin') self._api_key = parser.get('Auth', 'key') except: logging.error("Error when parsing lastfm plugin info.") self.last = pylast.get_lastfm_network(api_key = self._api_key) #TODO: Localize commands self.function_dict = { "user": self.get_user_track, "artist": self.get_artist_tracks, "tag_artists": self.get_tag_artists, "tag_albums": self.get_tag_albums, }
def fetchTrackTags(self, artist, track, maxTagsToFetch, minWeight, retries=2): ''' Retrieve track tags from LastFM, filtering out those tags that appear bunky (below the specified weight, longer than the maximum allowable distance, self-referential, etc. ''' try: lastfm = pylast.get_lastfm_network(api_key=self.api_key, api_secret=self.api_secret) tags = self.processSeenTags(lastfm.get_track(artist, track).get_top_tags(limit=maxTagsToFetch), minWeight) return [pair for pair in tags if pair[0].lower().replace('the', '').strip() != artist.lower().replace('the', '').strip() and pair[0].lower() != track] except Exception as err: if ('not found' in str(err).lower() or 'not be found' in str(err).lower()): return [] if (retries > 0): self.outputWrapper.logError('Problem retrieving track tag information for [' + str(artist) + ':' + str(track) + '], ' + str(retries) + ' retries left: ' + str(err)) time.sleep(5) return self.fetchTrackTags(artist, track, maxTagsToFetch, minWeight, retries - 1) else: self.outputWrapper.logError('Problem retrieving track tag information for [' + str(artist) + ':' + str(track) + '], skipping: ' + str(err)) return None
def setup_lastfm(): global last_fm_network config = ConfigParser.ConfigParser() config.read(os.path.expanduser('~/.mp3tagger.cfg')) if config.has_option("last.fm", "key") and config.has_option("last.fm", "secret"): API_KEY = config.get("last.fm", "key") API_SECRET = config.get("last.fm", "secret") else: new_config = ConfigParser.SafeConfigParser() new_config.add_section("last.fm") new_config.set("last.fm", "key", raw_input("Enter your Last.fm key:")) new_config.set("last.fm", "secret", raw_input("Enter your Last.fm secret:")) API_KEY = new_config.get("last.fm", "key") API_SECRET = new_config.get("last.fm", "secret") print "NOTE: key and secret will be stored in ~/.mp3tagger.cfg" with open(os.path.expanduser('~/.mp3tagger.cfg'), 'w') as configfile: new_config.write(configfile) last_fm_network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET)
def handle_lastneighbours(t, s, p): if not p: s.syntax(t, 'lastneighbours') else: p = reduce_spaces(p.strip()) arr = p.split(' ') user = arr[0] number = int(arr[1]) if (len(arr) >= 2 and arr[1].isdigit()) else 10 try: api = pylast.get_lastfm_network(lastfm_key) if number > 10: number = 10 seq = api.get_user(user).get_neighbours(limit=number) if seq: mesg = u'Соседи пользователя %s:\n' % (user) mesg += ', '.join(item for item in seq) else: mesg = u'Пользователь %s живет в пустыне.' % (user) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def __init__(self, performer): try: API_KEY = settings.LASTFM_API_KEY API_SECRET = settings.LASTFM_API_SECRET username = settings.LASTFM_USERNAME password = settings.LASTFM_PASSWORD except AttributeError: API_KEY = "724bf07f27e43443f07875f3148c28e7" API_SECRET = "f9d0024a7c7c68fe252c6a1376dc7959" username = "******" password = "******" password_hash = pylast.md5(password) network = pylast.get_lastfm_network(api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash) # Artist / Performer Model instance self.performer = performer # Lastfm artist self.artist = network.get_artist(self.performer.name)
def handle_lastneighbours(t, s, p): if not p: s.syntax(t, 'lastneighbours') else: p = reduce_spaces(p.strip()) arr = p.split(' ') user = arr[0] number = int(arr[1]) if (len(arr)>=2 and arr[1].isdigit()) else 10 try: api = pylast.get_lastfm_network(lastfm_key) if number > 10: number=10 seq = api.get_user(user).get_neighbours(limit=number) if seq: mesg = u'Соседи пользователя %s:\n' % (user) mesg += ', '.join(item for item in seq) else: mesg = u'Пользователь %s живет в пустыне.' % (user) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def handle_taste(t, s, p): if not p: s.syntax(t, 'taste') else: p = reduce_spaces(p.lower().encode('utf-8').replace('\\x','%')).split(' ',1) try: (user1, user2) = p except: s.msg(t, u'Необходимо два пользователя') return try: api = pylast.get_lastfm_network(lastfm_key) seq = api.get_user(user1).compare_with_user(user2) percent = float(seq[0]) mesg = u'Cовместимость %s и %s равна %1.2f%%. Их объединяет любовь к: ' % (user1, user2, percent*100.0) list = seq[1] mesg += ', '.join(item.get_name() for item in list) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def handle_topalbums(t, s, p): if not p: s.syntax(t, 'topalbums') else: p = reduce_spaces(p.strip()) arr = p.split(' ') user = arr[0] number = int(arr[1]) if (len(arr) >= 2 and arr[1].isdigit()) else 10 try: api = pylast.get_lastfm_network(lastfm_key) seq = api.get_user(user).get_top_albums() if seq: if len(seq) < number: number = len(seq) mesg = u'Любимые альбомы пользователя %s:\n' % (user) for item in seq[:number]: mesg += u'%s (%s)\n' % (item[0].get_name(), item[1]) else: mesg = u'У пользователя %s нет любимых альбомов.' % (user) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def handle_taste(t, s, p): if not p: s.syntax(t, 'taste') else: p = reduce_spaces(p.lower().encode('utf-8').replace('\\x', '%')).split( ' ', 1) try: (user1, user2) = p except: s.msg(t, u'Необходимо два пользователя') return try: api = pylast.get_lastfm_network(lastfm_key) seq = api.get_user(user1).compare_with_user(user2) percent = float(seq[0]) mesg = u'Cовместимость %s и %s равна %1.2f%%. Их объединяет любовь к: ' % ( user1, user2, percent * 100.0) list = seq[1] mesg += ', '.join(item.get_name() for item in list) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def fetchTagCount(self, tag, retries=3): ''' Return a count/weight, for the specified tag. The API no longer seems to return counts, though, so this is currently deprecated. ''' try: lastfm = pylast.get_lastfm_network(api_key=self.api_key, api_secret=self.api_secret) results = lastfm.search_for_tag(tag) if (results.get_total_result_count() > 0): topmatch = results.get_next_page()[0] if (topmatch is not None): if (self.useNamedTuple): return int(topmatch.weight) return int(topmatch['weight']) except Exception, err: if (retries > 0): common.safeStderr('Problem retrieving tag information, ' + str(retries) + ' retries left: ' + str(err)) time.sleep(5) return self.fetchTopTagStats(retries - 1) else: common.safeStderr('Problem retrieving tag information, ' + str(retries) + ' skipping: ' + str(err))
def getData(): """ gets any lastfm listening statistics for the last ten minutes. and then judges you. wait, no -- that's in a future version of hu. """ try: apiKey = "fd4197bb1bfc8521ced2ba81d70fd812" network = lastfm.get_lastfm_network(api_key = apiKey, username = lastfm_username) user = lastfm.User(lastfm_username, network) recentTracks = user.get_recent_tracks(20) tracksFromLastTenMinutes = [] # get rid of any tracks with a play-time earlier than fifteen minutes ago. for track in recentTracks: now = time.time() if int(track[2]) + 600 > now: # track was played < 10 minutes (600 seconds) ago. tracksFromLastTenMinutes.append(track) return xmlify(tracksFromLastTenMinutes) except: return ""
def handle_lasttracks(t, s, p): if not p: s.syntax(t, 'lasttracks') else: p = reduce_spaces(p.strip()) arr = p.split(' ') user = arr[0] number = int(arr[1]) if (len(arr) >= 2 and arr[1].isdigit()) else 10 try: api = pylast.get_lastfm_network(lastfm_key) seq = api.get_user(user).get_recent_tracks() if seq: if len(seq) < number: number = len(seq) mesg = u'Последние треки пользователя %s:\n' % (user) for item in seq[:number]: mesg += '%s - %s (%s)\n' % (item[0].get_artist().get_name( ), item[0].get_name(), item[1]) else: mesg = u'Пользователь %s в последнее время ничего не слушал.' % ( user) except: mesg = u'Невозможно получить доступ к данным.' s.msg(t, mesg)
def GetCoverArt(artist, album, download_location, size): """Use last.fm for album art fetching""" ## Last.FM API key api_key = '5dde241299a6b25d28769972e966471b' network = pylast.get_lastfm_network(api_key=api_key) # Get the album object using album info we have album_object = network.get_album(artist, album) # Get the url of the cover art try: cover_url = album_object.get_cover_image(size=size) except pylast.WSError: cover_url = None # Get the cover image and put it in the covers directory, if it has been found if cover_url != None: try: coverfile = urllib2.urlopen(cover_url) # Replace disallowed characters with an underscore in the artist and album parts of the filename disallowed = ['\\', '/', ':', '<', '>', '?', '*', '|'] artist_filename = artist album_filename = album for character in disallowed: artist_filename = artist_filename.replace(character, '_') album_filename = album_filename.replace(character, '_') output = open( download_location + '/#%s#%s#.%s' % (artist_filename, album_filename, cover_url.split('.')[-1]), 'wb') output.write(coverfile.read()) output.close() found = True except: print "Unexpected error:" found = False else: found = False time.sleep(5) return found
def get_photo(self): """ Attempt to find a photo for this artist. The Last.fm API is used to find the primary image. This requires the pylast module; if it isn't found nothing will be done. """ # Make sure the pylast module is available. try: pylast except NameError: return False # Create a connection to the Last.fm API. lastfm = pylast.get_lastfm_network(api_key=settings.LASTFM_API_KEY) try: lastfm_artist = lastfm.get_artist(self.name) lastfm_images = lastfm_artist.get_images() primary_image = lastfm_images[0] try: primary_image_url = primary_image.sizes.original except AttributeError: primary_image_url = primary_image['sizes']['original'] # Read the image data from the URL supplied. response = urllib2.urlopen(primary_image_url) data = response.read() # Store the photo on disk. filename = os.path.join(settings.MEDIA_ROOT, Artist.PHOTO_UPLOAD_DIRECTORY, '%s.jpg' % self.slug) fh = open(filename, 'w') fh.write(data) fh.close() # Link the photo to the artist (i.e. save the Artist object). self.photo = os.path.join(Artist.PHOTO_UPLOAD_DIRECTORY, '%s.jpg' % self.slug) self.save() except (pylast.WSError, IndexError): pass
def lastfm(inp, reply=None, say=None, nick=''): API_KEY = "30cabd8b57c765a42f78e8f5d329fdc0" API_SECRET = "0ab2d0a46763e71d1ed8877b4ea209cf" username = "******" password_hash = pylast.md5("justpurple") network = pylast.get_lastfm_network(api_key=API_KEY, api_secret=API_SECRET, username=username, password_hash=password_hash) try: if inp == '': user = network.get_user(nick) else: user = network.get_user(inp) nick = inp except WSError: say("This user doesn't exist.") try: tracks = user.get_recent_tracks() except pylast.WSError: if inp == '': return ( "It seems like your current nickname does not have a lastFM account. Try '.lastfm username' instead." ) else: return ("The user '%s' does not exist. Maybe you misspelled it?" % inp) recent = tracks[0][0] artist = recent.get_artist().get_name() title = recent.get_title() url = recent.get_url() bitlyURL = bitly_api.shorten(url) finalise = "\x02%s\x0F's last track - \x02%s\x0f :: Artist - \x02%s\x0f :: Link to Song - \x02%s\x0F" % ( nick, title, artist, bitlyURL) say(finalise)
def connect(self, session_key): self.network = pylast.get_lastfm_network(api_key=API_KEY, api_secret=API_SECRET, session_key=session_key) self.scrobbler = self.network.get_scrobbler(CLIENT_ID, CLIENT_VERSION)
pass ok = False if current_song is not None: if current_song.has_key("artist"): artist = current_song["artist"] else: if current_song.has_key("performer"): artist = current_song["performer"] else: artist = "Unknown Artist" for i in range(5): try: pylast.get_lastfm_network( api_key=lastfm_api_key, api_secret=lastfm_api_secret, username=lastfm_username, password_hash=pylast.md5(lastfm_password)).get_track( artist, current_song["title"]).love() print "Content-type: text/plain\n" print "1" ok = True break except: pass if not ok: print "Content-type: text/plain\n" print "0"
query = "CREATE TABLE IF NOT EXISTS artists(id INTEGER PRIMARY KEY, artist TEXT)" cur.execute(query) query = "CREATE TABLE IF NOT EXISTS similarity(first INTEGER, second INTEGER, UNIQUE(first, second))" cur.execute(query) query = "SELECT COUNT(*) FROM artists" cur.execute(query) number = cur.fetchone()[0] query = "SELECT artist FROM artists" cur.execute(query) data = cur.fetchall() current_artists = [elt[0] for elt in data] network = pylast.get_lastfm_network(keys.api_key, keys.api_secret, keys.user, keys.password) user = pylast.User(sys.argv[1], network) top_artists = user.get_top_artists(limit=50) query = "INSERT INTO artists VALUES (?,?)" count = 1 insert = [] for artist in top_artists: name = artist.item.get_name() if name not in current_artists: insert.append((count + number, name)) count += 1 cur.executemany(query, insert) query = "SELECT * FROM artists"
import pylast as fm network = fm.get_lastfm_network() dict = {} dict['dictA'] = {} dict['dictB'] = {} dict['dictA']['dictAA'] = {} dict['dictA']['dictAB'] = {} dict['dictA']['dictAA']['key1'] = 'value1' dict['dictA']['dictAB']['key2'] = 'value2' dict['dictB']['dictBA'] = {} dict['dictB']['dictBB'] = {} dict['dictB']['dictBA']['key3'] = 'value3' dict['dictB']['dictBB']['key4'] = 'value4' print dict madonna = network.get_artist("Madonna") albums = madonna.get_top_albums() bio = madonna.get_bio_content() biosum = madonna.get_bio_summary() print madonna print albums for album in albums: print album print bio
import pylast from twitter import * import time ###################PYLAST AUTH############################################################# API_KEY = 'YOUR LASTFM API KEY HERE' API_SECRET = 'YOUR LASTFM API SECRET KEY HERE' username = '******' password_hash = pylast.md5( 'YOUR LASTFM PASSWORD') network = pylast.get_lastfm_network( api_key = API_KEY, api_secret = API_SECRET, username = username, password_hash = password_hash) user = pylast.User(username, network) ###################PYLAST AUTH############################################################# ###################TWITTER AUTH############################################################ t = Twitter( auth=OAuth( 'YOUR TWITTER TOKEN KEY', 'YOUR TWITTER TOKEN SECRET KEY', 'YOUR TWITTER CONSUMER KEY', 'YOUR TWITTER CONSUMER SECRET KEY') ) ###################TWITTER AUTH#############################################################
def lastfm_connect(lastfm_username, lastfm_password): conn = pylast.get_lastfm_network(api_key=API_KEY, api_secret=API_SECRET, username=lastfm_username, password_hash=lastfm_password) return conn
import pylast as pl import numpy as np import pprint as pp import recommendations as recom import recommtest1 as rct # instantiate network network = pl.get_lastfm_network() # get Band band = network.get_artist("J.B.O") topfans = band.get_top_fans(10) group = [a.item for a in topfans] #print band #print topfans #print group userDict = recom.createLastfmUserDict(group) selectedUser = group[1].get_name() print "Selected User: "******"====================================" topMatches = recom.topMatches(userDict, group[1].get_name(), recom.sim_euclid) print "Top Matches: " pp.pprint(topMatches) print "====================================" recommendations = recom.getRecommendations(userDict, group[1].get_name(), recom.sim_euclid)