Skip to content

Instantly share code, notes, and snippets.

@pajp
Last active December 11, 2015 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pajp/4526199 to your computer and use it in GitHub Desktop.
Save pajp/4526199 to your computer and use it in GitHub Desktop.
convert last.fm "recent tracks" XML into CSV format
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