Skip to content

lfmxtractplus is a library for extracting Last.fm scrobbles along with spotify audio features for use with Pandas

License

Notifications You must be signed in to change notification settings

madhan001/lfmxtractplus

Repository files navigation

lfmxtractplus

Description

lfmxtractplus is a library for extracting Last.fm scrobbles along with spotify audio features for use with Pandas

Working

The user's scrobbles are retrieved using last.fm's API with the user.getRecentTracks endpoint. As last.fm's API doesn't provide a method to directly retrieve Spotify audio features we use the sp.search() method to search Spotify for the track's spotifyID (trackID) and use the spotifyID to retrieve the audio feature of each track using the sp.audio_features() method.

Installation

If you already have Python on your system you can install the library simply by downloading the distribution, unpack it and install in the usual fashion:

Changelog

  • v1.2

    • Encapsulated methods inside a class
    • Added token refreshing for get_playlist()
    • Optimized imports
  • v1.1

    • Added support for outputting logs to a file instead of the console
    • Fixed progress bars
    • Move timezone out of config.yaml
  • v1.0

    • Initial version
python setup.py install

You can also install it using a popular package manager with

pip install lfmxtractplus

or

easy_install lfmxtractplus

Dependencies

Quick Start

To get started,simply install lfmxtractplus, initialize with config.yaml, visit the link displayed and login with your Spotify account, copy and paste the redirect url back into the Python prompt and call methods:

import lfmxtractplus as lxp
import pandas as pd

lf = lxp.lfmxtractplus('config.yaml') #path to config.yaml
scrobbles_dict = lf.generate_dataset(lfusername='madhan_001', pages=0)
scrobbles_df = scrobbles_dict['complete']

config.yaml

This file must contain the API keys for last.fm and spotify.

#spotify api credentials (visit https://developer.spotify.com)
sp_cid:  #spotify client ID
sp_secret:  #spotify client secret
#last.fm api key (visit https://www.last.fm/api)
lf_key:  #last.fm API key
#filepath for log file
log_path: '\logs\\output.log' #path for output.log

Documentation

lfmxtractplus(cfgPath)

Calls functions needed for initialization, handles loading config file, initializing logger object, initializing Spotipy object.

Visit the link displayed and login with your Spotify account, copy and paste the redirect url back into the Python prompt.

To be called before calling other functions.

:param cfgPath: filepath for config.yaml

:return lfmxtractplus: object to call other functions with

lf.generate_dataset(lfusername, timezone='Asia/Kolkata', pages=0)

Gets user's listening history and enriches it with Spotify audio features.

:param lfusername: last.fm username
:param timezone: timezone of the user (must correspond with the timezone in user's settings)
:param pages: number of pages to retrieve, use pages = 0 to retrieve full listening history

:return scrobblesDFdict: dictionary with two dataframes ('complete' with timestamps and 'library' with library contents)

Warning : Does not support multiple timezones for scrobbles

lf.get_playlist(user='billboard.com', playlist_id='6UeSakyzhiEt4NB3UAd6NQ')

Retrieves audio features of a playlist (Billboard Hot 100 is the default playlist)

:param user: username of the playlist owner
:param playlist_id: playlist id (found at the end of a playlist url)

:return: a dataframe with audio features of a playlist

lf.unmapped_tracks(scrobblesDF)

Returns a dataframe tracks that couldn't be mapped to spotify.

:param scrobblesDF: dataframe with scrobbled tracks and trackIDs

:return scrobblesDF: dataframe containing tracks with no trackIDs

Examples

Reporting issues

If you have suggestions, bugs or other issues specific to this library, file an issue on GitHub. Or just send me a pull request.

About

lfmxtractplus is a library for extracting Last.fm scrobbles along with spotify audio features for use with Pandas

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages