Sha256: be8a46bf01d67d2a7097c9e0ce26a98926ca5bc4484c93eca8266da97ff8906a

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

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

require 'rubygems'
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)
    Pit.set('last.fm', :data => {
        "session" => session
      }.merge(config))

    puts "Session key was generated."

    return
  end

  lastfm.session = session

  require 'itunes_observer'

  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.0.2 examples/love_daemon.rb