Skip to content

Instantly share code, notes, and snippets.

@livibetter
Created May 18, 2012 03:09
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 livibetter/2722912 to your computer and use it in GitHub Desktop.
Save livibetter/2722912 to your computer and use it in GitHub Desktop.
Tracks recently obsessed with using Last.fm library
#!/bin/bash
# Written by Yu-Jie Lin
# Public Domain
#
# Check Last.fm library to see what recent tracks has been played consecutively
# for three times at least.
#
# Usage: obs.sh APIKEY USERNAME
if (( $# != 2 )); then
echo 'obs.sh APIKEY USERNAME' >&2
exit 1
fi
MIN_COUNT=3
API_URL="http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=$2&api_key=$1&limit=200"
cat <(wget -q -O - "$API_URL&page=1") \
<(wget -q -O - "$API_URL&page=2") \
<(wget -q -O - "$API_URL&page=3") \
<(wget -q -O - "$API_URL&page=4") \
<(wget -q -O - "$API_URL&page=5") |
sed -n '/url/ s/ *<url>\(.*\)<\/url>/\1/p' |
awk -v min_count=$MIN_COUNT '
BEGIN {
url = "";
count = 0;
}
{
if (url == $0) {
count++;
} else {
// only show the url when more than min_count consecutive play
if (count > min_count)
printf("%3d %s\n", count, url);
url = $0;
count = 1;
}
}
END {
if (count > min_count)
printf("%3d %s\n", count, url);
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment