Sha256: 57eaa607870132470cb4ba5bc706f85eedd826b78de3b7fe5949739233a2e08c

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# OSX only

require 'rubygems'
require 'itunes_observer'
require 'lastfm'
require 'pit'

$KCODE = "UTF8"

def main
  config = Pit.get("last.fm", :require => {
      "api_key" => "your api key in last.fm",
      "api_secret" => "your api secret in last.fm"
    })

  lastfm = Lastfm.new(
    config['api_key'],
    config['api_secret'])

  unless session = config['session']
    token = lastfm.auth.get_token
    auth(config['api_key'], token)
    session = lastfm.auth.get_session(token)['key']
    Pit.set('last.fm', :data => {
        "session" => session
      }.merge(config))

    puts "Session key was generated."

    exit
  end

  lastfm.session = session

  ITunesObserver.new {|result|
    name = result['Name']
    artist = result['Artist']
    rating = result['Rating']

    if rating.to_i > 80
      begin
        lastfm.track.love(artist, name)
      rescue
      end
    end
  }.run
end

def auth(api_key, token)
  auth_url = 'http://www.last.fm/api/auth/?api_key=' + api_key + '&token=' + token

  unless system('open', auth_url)
    print 'open ' + auth_url + ' in your browser and '
  end

  print 'after authorization, push any key:'
  STDIN.gets.chomp
  puts
end

main

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
itunes-observer-0.1.1 examples/love.rb