Skip to content

Commit

Permalink
fix the issue of Lastfm keeps changing the date/count JSON data type …
Browse files Browse the repository at this point in the history
…back and forth (string <-> integer); bump to 0.7.2
  • Loading branch information
boonious committed Feb 25, 2019
1 parent a239838 commit 52850b3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changelog

## 0.7.2 (2019-02-25)

* Fix the issue of Lastfm changing the date/count data type in JSON back and forth (string <-> integer).

## 0.7.1 (2019-01-14)

* patches as per Lastfm API JSON data format changes: uts timestamp, play counts info are now returned as integers instead of strings.
* Patches as per Lastfm API JSON data format changes: uts timestamp, play counts info are now returned as integers instead of strings.

## 0.7.0 (2019-01-11)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:lastfm_archive, "~> 0.6.0"}
{:lastfm_archive, "~> 0.7.2"}
]
end
```
Expand Down
27 changes: 24 additions & 3 deletions lib/extract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ defmodule LastfmArchive.Extract do
# find out more about the user (playcount, earliest scrobbles)
# to determine data extraction strategy
@doc false
def info(user) do
def info(user), do: _info(user) |> _format_info

# get playcount for a particular year for a user
@doc false
def info(user, {from, to}), do: _info(user, {from, to}) |> _format_info

# fetching user info
defp _info(user) do
{_status, resp} = get_info(user)

playcount = resp["user"]["playcount"]
Expand All @@ -107,8 +114,7 @@ defmodule LastfmArchive.Extract do
end

# get playcount for a particular year for a user
@doc false
def info(user, {from, to}) do
defp _info(user, {from, to}) do
# pending, with a stop gap until Elixirfm pull requests are sorted out
# this is so that `lastfm_archive` can be published on Hex now
#{_status, resp} = get_recent_tracks(user, limit: 1, page: 1, from: from, to: to)
Expand All @@ -120,4 +126,19 @@ defmodule LastfmArchive.Extract do
resp_body["recenttracks"]["@attr"]["total"]
end

# Lastfm keeps changing the date/count JSON data type back and forth (from string to integer)
# use pattern matching to ensure the return date/count data is always in integer type

defp _format_info(playcount) when is_binary(playcount), do: playcount |> String.to_integer
defp _format_info(playcount) when is_integer(playcount), do: playcount

defp _format_info({playcount, registered}) when is_binary(playcount) or is_binary(registered) do
{playcount |> String.to_integer, registered |> String.to_integer}
end

defp _format_info({playcount, registered}) when is_integer(playcount) and is_integer(registered), do: {playcount, registered}

defp _format_info({nil, nil}), do: {0, 0}
defp _format_info(nil), do: 0

end
2 changes: 1 addition & 1 deletion lib/lastfm_archive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ defmodule LastfmArchive do
per_page = option(options, :per_page)
overwrite = option(options, :overwrite)
daily = option(options, :daily)

padded_page_s = page |> to_string |> String.pad_leading(3, "0")
filename = if daily do
dt
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule LastfmArchive.MixProject do
def project do
[
app: :lastfm_archive,
version: "0.7.1",
version: "0.7.2",
elixir: "~> 1.7",
start_permanent: Mix.env() == :prod,
escript: [main_module: LastfmArchive.Cli, path: "bin/lastfm_archive"],
Expand Down

0 comments on commit 52850b3

Please sign in to comment.