Skip navigation

I’ve had a free spotify account for some time now, and I’ve used it actually mostly to listen to music that I already own, so good is the client. So when Spotify announced they’d be limiting you to 5 plays per track, and a maximum of 10 hours per month, I was a bit sad about that.

What was more irritating was that much of the music I’ve already got is sitting on CD in lofts in buildings where I’m currently not. Much of it is also on YouTube of course, but it’s not as pleasant to access.

What to do? Well, it’s well known that Audio Hijack Pro can rip the audio from Spotify. But who wants one giant 10-hour MP3 to deal with?

Sitting around with some time on my hands due to a broken hip, I cobbled together a script when I found that groovy was capable of invoking AppleScript scripts. Taking 240mg of codeine / day was not a time to start learning how AppleScript does file handing; this is all pretty hacky stuff and could be improved on, I’m sure. Think of it as proof of concept.

Steps:

1) Get groovy installed

2) Turn the debug logging on in growl

3) Start hijacking in Audio Hijack Pro

4) run the following script (e.g in groovyConsole)

5) Play music

The script will tag the music you play, and split into individual files. It probably won’t be happy with things like “s in track names, but fixing that is left as an exercise for the reader. Please bear in mind this was written literally on drugs.

import javax.script.*;

class Processor {
    def f = "/Users/magnayn/Library/Logs/Growl.log";
    def process;
    def engine;
    def run() {
        def mgr = new ScriptEngineManager();
        engine = mgr.getEngineByName("AppleScript")
        startRip();
        process = "tail -0 -f $f".execute();
        def lastLine = "";
        def inSpotifyGrowl = false;
        def spotifyLine;
        process.in.eachLine { line ->
            def idx = line.indexOf(" Spotify: ");
            if( idx > -1 ) {
                spotifyLine = line.substring(idx + 10);
                inSpotifyGrowl = true;
            } else {
                if( inSpotifyGrowl ) {
                   def line2 = "";
                   if( !lastLine.substring(0,10).equals( line.substring(0,10)) ) {
                        line2 = line;
                   }
                   trackChange(spotifyLine, line2);
                }
                inSpotifyGrowl = false;
            }
            lastLine = line;
        }
    }

    def trackChange(line1, line2) {
        try {
            def album = line2.substring(0, line2.lastIndexOf(")") );
            def artist = line1.substring(line1.indexOf("(")+1);
            def title = line1.substring(0, line1.indexOf("(") ).trim();
            rename(title,artist,album);
            println "Title: " + title;
            println "Artist: " + artist;
            println "Album: " + album;
        }
        catch(Exception ex) {
           ex.printStackTrace();
        }
    }

    def startRip() {
def scr = """
tell application "Audio Hijack Pro"
  set theSession to first session whose hijacked is true
  start recording theSession
end tell
""";

engine.eval(scr);

    }

    def rename(title,artist,album)
    {
def scr = """
tell application "Audio Hijack Pro"
  set theSession to first session whose hijacked is true
  split recording theSession
  tell theSession
    set title tag to "$title"
    set artist tag to "$artist"
    set album tag to "$album"
  end tell
end tell
""";

engine.eval(scr);    

    }
}

def p = new Processor();
p.run();


Advertisement

3 Comments

  1. Hi there,

    I’m not really a programmer, in fact, I’m absolutely not. Amazingly enough I got groovy installed, I have growl and spotify. I can’t find growl debugging. When I copy and paste the script into Groovy I get “null exception” at the end.

    I am amazingly desperate to get this to work. Can you help?

    • Well, it seems my previous comment was premature. The “silence monitor” method isn’t really reliable. So here’s how you use the authors script which works flawlessly:

      - If your on Lion, uninstall Growl 1.3 and install Growl 1.2 Fork instead(Lion compatible version 1.2) http://www.macupdate.com/app/mac/41038/growl-fork
      - enable Growl debugging with this script: http://dl.dropbox.com/u/3560558/GrowlLoggingScripts.zip
      (unzip and run enable.command)
      - open Audio Hijack Pro, hijack Spotify, don’t record yet
      - in Terminal type “grooyconsole”
      - paste the entire script
      - and click the “Execute groovy script” button
      - now play music in Spotify ;)

  2. Hi, OR you could simply use the “silence montior” feature in Audio Hijack Pro and set it to custom: “start new file, -60dB, 1.0 seconds”. This way you’ll get untaged but properly splitted files, then use MusicBrainz Picard to scan and tag them automatically. Awesomeness! ( I recommend this combined with a Spotify Premium subscribtion which allows you to receive 320 kbs streams!) The recording settings I use: MP3, CBR, 320 kbps, Joint Stereo. Greetings and hopfully your hip cures well!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.