Updating iTunes when listening to Pianobar

For the past few years, I've been using the WSP Music System for iTunes to help me listen to things in a better manner. But that means I need to keep my play information in sync.

Occasionally, I listen to music via Pandora. I never really got in to Spotify or some of the others, and Pandora has broadened my musical horizons a great deal.

Pianobar is an awesome console application: it lets you listen to Pandora from a command-line, which means it's very lean. I'm a minimalist at heart, even if I don't always follow "minimalist" trends; this is one situation where I do. Pianobar makes my life so much easier.

The fact that Pianobar has a script that it will run with information about the currently playing song is awesome. It lets me submit those plays to Last.fm, among other things. After getting Pianobar to build on my Mac, I realized that I could update iTunes' play counts with the command script from Pianobar. I've created the following scripts in my ~/.config/pianobar/ directory:

UpdateItunesPlayCount.scpt

on run argv    tell application "iTunes"        set theArtist to (item 1 of argv)        set theTitle to (item 2 of argv)        set results to (every file track of playlist "Library"            whose artist contains theArtist and name contains theTitle)        repeat with t in results            tell t                set playedCount to get played count of t                set played count of t to playedCount + 1                set played date of t to (current date)            end tell        end repeat    end tellend run

UpdateItunesSkipCount.scpt

on run argv    tell application "iTunes"        set theArtist to (item 1 of argv)        set theTitle to (item 2 of argv)        set results to (every file track of playlist "Library"            whose artist contains theArtist and name contains theTitle)        repeat with t in results            tell t                set skippedCount to get skipped count of t                set skipped count of t to skippedCount + 1                set skipped date of t to (current date)            end tell        end repeat    end tellend run

UpdateItunesRating.scpt

on run argv    tell application "iTunes"        set theArtist to (item 1 of argv)        set theTitle to (item 2 of argv)        set results to (every file track of playlist "Library"            whose artist contains theArtist and name contains theTitle)        repeat with t in results            tell t                set trackRating to get rating of t                set rating of t to trackRating + 5            end tell        end repeat    end tellend run

Then, in my pianobar command script, I have the following:

songfinish)    # scrobble if 75% of song have been played, but only if the song hasn't    # been banned    if [ -n "$songDuration" ] && [ "$songDuration" -ne 0 ] &&            [ $(echo "scale=4; ($songPlayed/$songDuration*100)>50" | bc) -eq 1 ] &&            [ "$rating" -ne 2 ]; then        scrobbler-helper -P pia -V 1.0 "$title" "$artist" "$album" "" "" "" "$((songDuration/1000))" &        osascript /Users/gsolsberry/.config/pianobar/UpdateItunesPlayCount.scpt "$artist" "$title"    else        if [ "$songPlayed" -lt 30000 ]; then            osascript /Users/gsolsberry/.config/pianobar/UpdateItunesSkipCount.scpt "$artist" "$title"        fi    fi    ;;songlove)    osascript /Users/gsolsberry/.config/pianobar/UpdateItunesRating.scpt "$artist" "$title"    ;;

If the song played to conclusion, we scrobble it with Last.fm, and update it's play count in iTunes. If we skipped to the next song within the first 30 seconds, we update the skip count. If we marked the song as "loved", we updating the songs rating by 5 points.

In this way, if we ever play a song that's in our iTunes library, the counts are updated properly, which keeps the WSP Music System data accurate.