Last active
May 27, 2023 22:20
-
-
Save sentriz/db94a8678272c039a0eeb402f38761e8 to your computer and use it in GitHub Desktop.
last.fm favourites to gonic playlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env fish | |
if ! test (count $argv) -eq 2 | |
echo 'usage: find /path/to/music -type f \( -name \'*.mp3\' -o -name \'*.flac\' \) | ./last-fm-to-gonic USER API_KEY >/gonic/playlists/<user>/lastfm.m3u' >&2 | |
exit 1 | |
end | |
set user $argv[1] | |
set api_key $argv[2] | |
set per_page 100 | |
set buff (mktemp /tmp/gonic.XXX) | |
cat >$buff | |
trap 'rm $buff >/dev/null' EXIT | |
function req_page | |
curl -s "http://ws.audioscrobbler.com/2.0/" \ | |
--data-urlencode "method=user.getlovedtracks" \ | |
--data-urlencode "format=json" \ | |
--data-urlencode "user=$user" \ | |
--data-urlencode "api_key=$api_key" \ | |
--data-urlencode "page=$argv[1]" \ | |
--data-urlencode "limit=$per_page" | |
end | |
function req_all_pages | |
set page 1 | |
while true | |
req_page $page | jq -e -r '.lovedtracks.track | .[] | "\(.artist.name)\t\(.name)"' | |
or break | |
set page (math $page + 1) | |
end | |
end | |
function find_track_path | |
sed 's/[^[:alnum:]]/ /g' | unidecode | while read line | |
fzf -i --filter="$line" <$buff | head -n 1 | |
end | |
end | |
printf '#GONIC-NAME:"lastfm favourites %s"\n' (date +'%Y.%m.%d') | |
printf '#GONIC-COMMENT:""\n' | |
printf '#GONIC-IS-PUBLIC:"false"\n' | |
req_all_pages | find_track_path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment