Last active
December 11, 2015 01:49
-
-
Save pajp/4526199 to your computer and use it in GitHub Desktop.
convert last.fm "recent tracks" XML into CSV format
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
NUMPAGES=800 | |
USERNAME=pajp | |
APIKEY=DEADBEEF | |
# may need to run this several times because last.fm sometimes returns 403 | |
page=1; while [ $page -le $NUMPAGES ] ; do if [ -s page.$page ] ; then echo "Skipping page $page" ; page=$(($page + 1)); continue ; fi ; echo "Doing page $page"; curl -f "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=${USER}&api_key=$APIKEY&page=$page" > page.$page && echo "Page $page OK"; page=$(($page + 1)); done | |
# convert the downloaded XML to CSV using xmlstarlet. Assumes a certain order of the XML elements to may break at any time. | |
for page in `seq 1 $NUMPAGES`; do count=0; xml sel -t -v '//track/name | //track/album | //track/artist | //track/date' < page.$page|while read line; do case $(( $count % 4 )) in 0) artist="$line" ;; 1) track="$line" ;; 2) album="$line";; 3) ts="$line"; echo \"$track\"\;\"$album\"\;\"$artist\"\;\"$ts\" ;; esac ; count=$(($count + 1));done;done > tracks.csv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment