View Source LastfmArchive (lastfm_archive v0.10.1)

lastfm_archive is a tool for creating local Last.fm scrobble file archive, Solr archive and analytics.

The software is currently experimental and in preliminary development. It should eventually provide capability to perform ETL and analytic tasks on Lastfm scrobble data.

Current usage:

Link to this section Summary

Functions

Returns the total playcount and registered, i.e. earliest scrobble time for a user.

Load all CSV data from the archive into Solr for a Lastfm user.

Read scrobbles from an archive of a Lastfm user.

Read scrobbles data from a CSV archive created via transform/2

Read scrobbles data from an Apache Parquet archive created via transform/2.

Transform downloaded file archive into CSV or Apache Parquet formats for a Lastfm user.

Link to this section Types

Link to this section Functions

Returns the total playcount and registered, i.e. earliest scrobble time for a user.

@spec load_archive(binary(), solr_url()) :: :ok | {:error, Hui.Error.t()}

Load all CSV data from the archive into Solr for a Lastfm user.

The function finds CSV files from the archive and sends them to Solr for ingestion one at a time. It uses Hui client to interact with Solr and the Hui.URL.t/0 struct for Solr endpoint specification.

example

Example

  # define a Solr endpoint with %Hui.URL{} struct
  headers = [{"Content-type", "application/json"}]
  url = %Hui.URL{url: "http://localhost:8983/solr/lastfm_archive", handler: "update", headers: headers}

  LastfmArchive.load_archive("a_lastfm_user", url)

CSV files must be pre-created before the loading - see transform/2.

Link to this function

read(user \\ LastfmClient.default_user(), options)

View Source
@spec read(binary(), LastfmArchive.Archive.FileArchive.read_options()) ::
  {:ok, Explorer.DataFrame} | {:error, term()}

Read scrobbles from an archive of a Lastfm user.

This returns scrobbles for a single day or month period in a lazy Explorer.DataFrame for further data manipulation and visualisation.

example

Example

  # read a single-day scrobbles from the configured
  # archive (FileArchive) and default user
  LastfmArchive.read(day: ~D[2022-12-31])

  # read a single-month scrobbles for a user
  LastfmArchive.read("a_lastfm_user",  month: ~D[2022-12-31])

Options:

  • :day - read scrobbles for this particular date (Date.t())
  • :month - read scrobbles for this particular month (Date.t())
Link to this function

read_csv(user \\ LastfmClient.default_user(), list)

View Source
@spec read_csv(binary(), DerivedArchive.read_options()) ::
  {:ok, Explorer.DataFrame} | {:error, term()}

Read scrobbles data from a CSV archive created via transform/2

Returns a data frame containing scrobbles spanning a single year.

example

Example

  # read a year of scrobbles for a user
  LastfmArchive.read_csv("a_lastfm_user", year: 2023)

Options:

  • :year - read scrobbles for this particular year
Link to this function

read_parquet(user \\ LastfmClient.default_user(), list)

View Source
@spec read_parquet(binary(), DerivedArchive.read_options()) ::
  {:ok, Explorer.DataFrame} | {:error, term()}

Read scrobbles data from an Apache Parquet archive created via transform/2.

Returns a data frame containing scrobbles spanning a single year.

example

Example

  # read a year of scrobbles for a user
  LastfmArchive.read_parquet("a_lastfm_user", year: 2023)

Options:

  • :year - read scrobbles for this particular year
Link to this function

sync(user \\ LastfmClient.default_user(), options \\ [])

View Source
@spec sync(
  binary(),
  keyword()
) :: {:ok, metadata()} | {:error, :file.posix()}

Sync scrobbles for a Lastfm user.

example

Example

  LastfmArchive.sync("a_lastfm_user")

You can also specify a default user is in configuration, for example user_a in config/config.exs:

  config :lastfm_archive,
    user: "user_a",
    ... # other archiving options

And run:

  LastfmArchive.sync

The first sync downloads all daily scrobbles in 200-track (gzip compressed) chunks that are written into a local file archive. Subsequent syncs extract further scrobbles starting from the date of latest downloaded scrobbles.

The data is currently in raw Lastfm recenttracks JSON format, chunked into 200-track (max) gzip compressed pages and stored within directories corresponding to the days when tracks were scrobbled.

Options:

  • :interval - default 1000(ms), the duration between successive Lastfm API requests. This provides a control for request rate. The default interval ensures a safe rate that is within Lastfm's term of service: no more than 5 requests per second

  • :overwrite - default false (not available currently), if sets to true the system will (re)fetch and overwrite any previously downloaded data. Use this option to refresh the file archive. Otherwise (false), the system will not be making calls to Lastfm to check and re-fetch data if existing data chunks / pages are found. This speeds up archive updating

  • :per_page - default 200, number of scrobbles per page in archive. The default is the max number of tracks per request permissible by Lastfm

  • :data_dir - default lastfm_data. The file archive is created within a main data directory, e.g. ./lastfm_data/a_lastfm_user/.

These options can be configured in config/config.exs:

  config :lastfm_archive,
    ...
    data_dir: "./lastfm_data/"
Link to this function

transform(user \\ LastfmClient.default_user(), options \\ [format: :csv])

View Source
@spec transform(binary(), options()) :: any()

Transform downloaded file archive into CSV or Apache Parquet formats for a Lastfm user.

example

Example

  LastfmArchive.transform("a_lastfm_user", format: :csv)

  # transform archive of the default user into CSV files
  LastfmArchive.transform()

Current output transform formats: :csv, :parquet.

The function only transforms downloaded archive data on local filesystem. It does not fetch data from Lastfm, which can be done via sync/2.

The transformed files are created on a yearly basis and stored in gzip compressed format. They are stored in a csv or parquet directory within either the default ./lastfm_data/ or the directory specified in config/config.exs (:lastfm_archive, :data_dir).