itunes-observer 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +4 -0
- data/README.rdoc +37 -0
- data/Rakefile +110 -0
- data/examples/love_daemon.rb +65 -0
- data/lib/itunes_observer.rb +52 -0
- data/spec/itunes_observer_spec.rb +32 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +10 -0
- metadata +74 -0
data/ChangeLog
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
= itunes-observer
|
3
|
+
|
4
|
+
Observe iTunes and notify when starting playng track
|
5
|
+
|
6
|
+
== Description
|
7
|
+
|
8
|
+
|
9
|
+
== Installation
|
10
|
+
|
11
|
+
=== Archive Installation
|
12
|
+
|
13
|
+
rake install
|
14
|
+
|
15
|
+
=== Gem Installation
|
16
|
+
|
17
|
+
gem sources -a http://gems.github.com/
|
18
|
+
gem install youpy-itunes-observer
|
19
|
+
|
20
|
+
== Features/Problems
|
21
|
+
|
22
|
+
|
23
|
+
== Synopsis
|
24
|
+
|
25
|
+
ITunesObserver.new {|result|
|
26
|
+
puts '%s - %s' % [result['Artist'], result['Name']]
|
27
|
+
}.run
|
28
|
+
|
29
|
+
== See Also
|
30
|
+
|
31
|
+
* http://blog.8-p.info/articles/2006/12/24/rubycocoa-skype-itunes
|
32
|
+
|
33
|
+
== Copyright
|
34
|
+
|
35
|
+
Author:: youpy <youpy@buycheapviagraonlinenow.com>
|
36
|
+
Copyright:: Copyright (c) 2009 youpy
|
37
|
+
License:: MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/clean'
|
4
|
+
require 'rake/testtask'
|
5
|
+
require 'rake/packagetask'
|
6
|
+
require 'rake/gempackagetask'
|
7
|
+
require 'rake/rdoctask'
|
8
|
+
require 'rake/contrib/rubyforgepublisher'
|
9
|
+
require 'rake/contrib/sshpublisher'
|
10
|
+
require 'spec/rake/spectask'
|
11
|
+
require 'fileutils'
|
12
|
+
include FileUtils
|
13
|
+
|
14
|
+
$LOAD_PATH.unshift "lib"
|
15
|
+
require "itunes_observer"
|
16
|
+
|
17
|
+
NAME = "itunes-observer"
|
18
|
+
AUTHOR = "youpy"
|
19
|
+
EMAIL = "youpy@buycheapviagraonlinenow.com"
|
20
|
+
DESCRIPTION = ""
|
21
|
+
RUBYFORGE_PROJECT = "itunes-observer"
|
22
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
23
|
+
BIN_FILES = %w( )
|
24
|
+
VERS = ITunesObserver::VERSION
|
25
|
+
|
26
|
+
REV = File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
|
27
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config']
|
28
|
+
RDOC_OPTS = [
|
29
|
+
'--title', "#{NAME} documentation",
|
30
|
+
"--charset", "utf-8",
|
31
|
+
"--opname", "index.html",
|
32
|
+
"--line-numbers",
|
33
|
+
"--main", "README.rdoc",
|
34
|
+
"--inline-source",
|
35
|
+
]
|
36
|
+
|
37
|
+
task :default => [:spec]
|
38
|
+
task :package => [:clean]
|
39
|
+
|
40
|
+
Spec::Rake::SpecTask.new do |t|
|
41
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
42
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
43
|
+
t.rcov = true
|
44
|
+
end
|
45
|
+
|
46
|
+
spec = Gem::Specification.new do |s|
|
47
|
+
s.name = NAME
|
48
|
+
s.version = VERS
|
49
|
+
s.platform = Gem::Platform::RUBY
|
50
|
+
s.has_rdoc = true
|
51
|
+
s.extra_rdoc_files = ["README.rdoc", "ChangeLog"]
|
52
|
+
s.rdoc_options += RDOC_OPTS + ['--exclude', '^(examples|extras)/']
|
53
|
+
s.summary = DESCRIPTION
|
54
|
+
s.description = DESCRIPTION
|
55
|
+
s.author = AUTHOR
|
56
|
+
s.email = EMAIL
|
57
|
+
s.homepage = HOMEPATH
|
58
|
+
s.executables = BIN_FILES
|
59
|
+
s.rubyforge_project = RUBYFORGE_PROJECT
|
60
|
+
s.bindir = "bin"
|
61
|
+
s.require_path = "lib"
|
62
|
+
s.autorequire = ""
|
63
|
+
s.test_files = Dir["test/test_*.rb"]
|
64
|
+
|
65
|
+
#s.add_dependency('')
|
66
|
+
#s.required_ruby_version = '>= 1.8.2'
|
67
|
+
|
68
|
+
s.files = %w(README.rdoc ChangeLog Rakefile) +
|
69
|
+
Dir.glob("{bin,doc,spec,test,lib,templates,generator,extras,website,script}/**/*") +
|
70
|
+
Dir.glob("ext/**/*.{h,c,rb}") +
|
71
|
+
Dir.glob("examples/**/*.rb") +
|
72
|
+
Dir.glob("tools/*.rb")
|
73
|
+
|
74
|
+
s.extensions = FileList["ext/**/extconf.rb"].to_a
|
75
|
+
end
|
76
|
+
|
77
|
+
Rake::GemPackageTask.new(spec) do |p|
|
78
|
+
p.need_tar = true
|
79
|
+
p.gem_spec = spec
|
80
|
+
end
|
81
|
+
|
82
|
+
task :install do
|
83
|
+
name = "#{NAME}-#{VERS}.gem"
|
84
|
+
sh %{rake package}
|
85
|
+
sh %{sudo gem install pkg/#{name}}
|
86
|
+
end
|
87
|
+
|
88
|
+
task :uninstall => [:clean] do
|
89
|
+
sh %{sudo gem uninstall #{NAME}}
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
Rake::RDocTask.new do |rdoc|
|
94
|
+
rdoc.rdoc_dir = 'html'
|
95
|
+
rdoc.options += RDOC_OPTS
|
96
|
+
rdoc.template = "resh"
|
97
|
+
#rdoc.template = "#{ENV['template']}.rb" if ENV['template']
|
98
|
+
if ENV['DOC_FILES']
|
99
|
+
rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/))
|
100
|
+
else
|
101
|
+
rdoc.rdoc_files.include('README.rdoc', 'ChangeLog')
|
102
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
103
|
+
rdoc.rdoc_files.include('ext/**/*.c')
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
desc "Show information about the gem"
|
108
|
+
task :debug_gem do
|
109
|
+
puts spec.to_ruby
|
110
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
#
|
4
|
+
# OSX only
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'lastfm'
|
8
|
+
require 'pit'
|
9
|
+
|
10
|
+
$KCODE = "UTF8"
|
11
|
+
|
12
|
+
def main
|
13
|
+
config = Pit.get("last.fm", :require => {
|
14
|
+
"api_key" => "your api key in last.fm",
|
15
|
+
"api_secret" => "your api secret in last.fm"
|
16
|
+
})
|
17
|
+
|
18
|
+
lastfm = Lastfm.new(
|
19
|
+
config['api_key'],
|
20
|
+
config['api_secret'])
|
21
|
+
|
22
|
+
unless session = config['session']
|
23
|
+
token = lastfm.auth.get_token
|
24
|
+
auth(config['api_key'], token)
|
25
|
+
session = lastfm.auth.get_session(token)
|
26
|
+
Pit.set('last.fm', :data => {
|
27
|
+
"session" => session
|
28
|
+
}.merge(config))
|
29
|
+
|
30
|
+
puts "Session key was generated."
|
31
|
+
|
32
|
+
return
|
33
|
+
end
|
34
|
+
|
35
|
+
lastfm.session = session
|
36
|
+
|
37
|
+
require 'itunes_observer'
|
38
|
+
|
39
|
+
ITunesObserver.new {|result|
|
40
|
+
name = result['Name']
|
41
|
+
artist = result['Artist']
|
42
|
+
rating = result['Rating']
|
43
|
+
|
44
|
+
if rating.to_i > 80
|
45
|
+
begin
|
46
|
+
lastfm.track.love(artist, name)
|
47
|
+
rescue
|
48
|
+
end
|
49
|
+
end
|
50
|
+
}.run
|
51
|
+
end
|
52
|
+
|
53
|
+
def auth(api_key, token)
|
54
|
+
auth_url = 'http://www.last.fm/api/auth/?api_key=' + api_key + '&token=' + token
|
55
|
+
|
56
|
+
unless system('open', auth_url)
|
57
|
+
print 'open ' + auth_url + ' in your browser and '
|
58
|
+
end
|
59
|
+
|
60
|
+
print 'after authorization, push any key:'
|
61
|
+
STDIN.gets.chomp
|
62
|
+
puts
|
63
|
+
end
|
64
|
+
|
65
|
+
main
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'osx/cocoa'
|
2
|
+
|
3
|
+
class ITunesObserver
|
4
|
+
VERSION = '0.0.2'
|
5
|
+
|
6
|
+
def initialize(&callback)
|
7
|
+
observer = Observer.alloc.init
|
8
|
+
observer.observe(&callback)
|
9
|
+
end
|
10
|
+
|
11
|
+
def run(stop_after = nil)
|
12
|
+
if stop_after
|
13
|
+
OSX::NSRunLoop.currentRunLoop.runUntilDate(Time.now + stop_after)
|
14
|
+
else
|
15
|
+
OSX::NSRunLoop.currentRunLoop.run
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# based on http://blog.8-p.info/articles/2006/12/24/rubycocoa-skype-itunes
|
20
|
+
class Observer < OSX::NSObject
|
21
|
+
def onPlayerInfo(info)
|
22
|
+
if info.userInfo['Player State'] == 'Playing'
|
23
|
+
result = Result.new(info.userInfo)
|
24
|
+
@callback.call(result)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def observe(&callback)
|
29
|
+
@callback = callback
|
30
|
+
center = OSX::NSDistributedNotificationCenter.defaultCenter
|
31
|
+
center.addObserver_selector_name_object_(self,
|
32
|
+
'onPlayerInfo:',
|
33
|
+
'com.apple.iTunes.playerInfo',
|
34
|
+
'com.apple.iTunes.player')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class Result
|
39
|
+
def initialize(attributes)
|
40
|
+
@attributes = attributes
|
41
|
+
end
|
42
|
+
|
43
|
+
def [](key)
|
44
|
+
case value = @attributes[key]
|
45
|
+
when OSX::NSCFString
|
46
|
+
value.to_s
|
47
|
+
else
|
48
|
+
value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__)
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'osx/cocoa'
|
5
|
+
|
6
|
+
include OSX
|
7
|
+
OSX.require_framework 'ScriptingBridge'
|
8
|
+
|
9
|
+
describe ITunesObserver do
|
10
|
+
before do
|
11
|
+
@itunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
|
12
|
+
raise 'iTunes must be running' unless @itunes.isRunning
|
13
|
+
@result = nil
|
14
|
+
@observer = ITunesObserver.new do |result|
|
15
|
+
@result = result
|
16
|
+
end
|
17
|
+
|
18
|
+
@itunes.stop
|
19
|
+
end
|
20
|
+
|
21
|
+
after do
|
22
|
+
@itunes.stop
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should observe playing" do
|
26
|
+
@itunes.playpause
|
27
|
+
@observer.run(1)
|
28
|
+
@result.should_not be_nil
|
29
|
+
@result['Name'].should_not be_nil
|
30
|
+
@result['Player State'].should eql('Playing')
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: itunes-observer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- youpy
|
8
|
+
autorequire: ""
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-10 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: youpy@buycheapviagraonlinenow.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- ChangeLog
|
25
|
+
files:
|
26
|
+
- README.rdoc
|
27
|
+
- ChangeLog
|
28
|
+
- Rakefile
|
29
|
+
- spec/itunes_observer_spec.rb
|
30
|
+
- spec/spec.opts
|
31
|
+
- spec/spec_helper.rb
|
32
|
+
- lib/itunes_observer.rb
|
33
|
+
- examples/love_daemon.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://itunes-observer.rubyforge.org
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --title
|
41
|
+
- itunes-observer documentation
|
42
|
+
- --charset
|
43
|
+
- utf-8
|
44
|
+
- --opname
|
45
|
+
- index.html
|
46
|
+
- --line-numbers
|
47
|
+
- --main
|
48
|
+
- README.rdoc
|
49
|
+
- --inline-source
|
50
|
+
- --exclude
|
51
|
+
- ^(examples|extras)/
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: itunes-observer
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: ""
|
73
|
+
test_files: []
|
74
|
+
|