LOLastfm 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,7 +32,7 @@ This allows for some neat features, like easily scrobbling from a radio, the fol
32
32
  how to scrobble songs from trance.fm.
33
33
 
34
34
  ```ruby
35
- %w(listened love now_playing).each {|name|
35
+ %w(listened love unlove now_playing).each {|name|
36
36
  on name do |song|
37
37
  next if song.artist || !song.stream?
38
38
 
data/bin/LOLastfm CHANGED
@@ -27,6 +27,11 @@ EM.run {
27
27
  d = LOLastfm.load(ARGV.first || '~/.LOLastfm/config')
28
28
  d.start
29
29
 
30
+ EM.error_handler {|e|
31
+ $stderr.puts e.message
32
+ $stderr.puts e.backtrace
33
+ }
34
+
30
35
  %w[INT KILL].each {|sig|
31
36
  trap sig do
32
37
  puts 'LOLastfm stopping...'
data/bin/LOLastfm-lyrics CHANGED
@@ -1,21 +1,19 @@
1
1
  #! /usr/bin/env ruby
2
- require 'json'
3
- require 'socket'
2
+ require 'LOLastfm/client'
4
3
 
5
- socket = UNIXSocket.new(File.expand_path(ARGV.empty? ? '~/.LOLastfm/socket' : ARGV.first))
6
- socket.puts [:commands, :glyr].to_json
4
+ client = LOLastfm::Client.new(ARGV.empty? ? '~/.LOLastfm/socket' : ARGV.first)
5
+ client.send_command :commands, :glyr
7
6
 
8
7
  if STDOUT.tty?
9
8
  require 'ncursesw'
10
9
 
11
10
  abort 'ncurses UI not implemented yet, redirect the output to something'
12
11
  else
13
- socket.puts [:lyrics?].to_json
14
-
15
- response = JSON.parse(socket.readline)
12
+ client.send_command :lyrics?
13
+ response = client.read_response
16
14
 
17
15
  if response.first
18
- puts response.first['data']
16
+ puts response.first['content']
19
17
  else
20
18
  exit 1
21
19
  end
@@ -1,14 +1,35 @@
1
1
  #! /usr/bin/env ruby
2
- require 'json'
3
- require 'socket'
2
+ require 'optparse'
3
+ require 'LOLastfm/client'
4
4
 
5
- socket = UNIXSocket.new(File.expand_path(ARGV.empty? ? '~/.LOLastfm/socket' : ARGV.first))
6
- socket.puts [:now_playing?].to_json
5
+ options = {}
7
6
 
8
- response = JSON.parse(?[ + socket.readline + ?]).first
7
+ OptionParser.new do |o|
8
+ options[:socket] = '~/.LOLastfm/socket'
9
9
 
10
- exit 1 if response.nil?
10
+ o.on '-t', '--template TEMPLATE', 'the template to use' do |value|
11
+ options[:template] = value
12
+ end
13
+ end.parse!
11
14
 
12
- %w[track title artist album].each {|name|
13
- puts "#{name} #{response[name]}" if response[name]
14
- }
15
+ if ARGV.first
16
+ options[:socket] = ARGV.first
17
+ end
18
+
19
+ client = LOLastfm::Client.new(options[:socket])
20
+ client.send_command :now_playing?
21
+
22
+ exit 1 if (response = client.read_response).nil?
23
+
24
+ if options[:template]
25
+ track = response['track']
26
+ title = response['title']
27
+ artist = response['artist']
28
+ album = response['album']
29
+
30
+ puts eval("%Q{#{options[:template]}}")
31
+ else
32
+ %w[track title artist album].each {|name|
33
+ puts "#{name} #{response[name]}" if response[name]
34
+ }
35
+ end
data/bin/LOLastfm-send CHANGED
@@ -1,7 +1,6 @@
1
1
  #! /usr/bin/env ruby
2
2
  require 'optparse'
3
- require 'socket'
4
- require 'json'
3
+ require 'LOLastfm/client'
5
4
 
6
5
  options = {}
7
6
 
@@ -21,6 +20,14 @@ OptionParser.new do |o|
21
20
  options[:love] = true
22
21
  end
23
22
 
23
+ o.on '--unlove', 'enable unlove sending' do
24
+ options[:unlove] = true
25
+ end
26
+
27
+ o.on '-c', '--current', 'work on the current song' do
28
+ options[:current] = true
29
+ end
30
+
24
31
  o.on '-t', '--title TITLE', 'title of the song' do |value|
25
32
  options[:song][:title] = value
26
33
  end
@@ -62,21 +69,20 @@ if ARGV.first
62
69
  options[:socket] = ARGV.first
63
70
  end
64
71
 
65
- socket = UNIXSocket.new(File.expand_path(options[:socket]))
72
+ client = LOLastfm::Client.new(options[:socket])
66
73
 
67
74
  if options[:use]
68
- socket.puts [:use, options[:use].split(':')].to_json
75
+ client.send_command :use, options[:use].split(':')
69
76
  elsif options[:commands]
70
- socket.puts [:commands, options[:commands].split(':')].to_json
77
+ client.send_command :commands, options[:commands].split(':')
71
78
  else
72
79
  if options[:now_playing]
73
- socket.puts [:now_playing, options[:song]].to_json
80
+ client.send_command :now_playing, options[:song]
74
81
  elsif options[:love]
75
- socket.puts [:love, options[:song].empty? ? nil : options[:song]].to_json
82
+ client.send_command :love, options[:song].empty? ? (options[:current] ? :current : nil) : options[:song]
83
+ elsif options[:unlove]
84
+ client.send_command :unlove, options[:song].empty? ? (options[:current] ? :current : nil) : options[:song]
76
85
  else
77
- socket.puts [:listened, options[:song]].to_json
86
+ client.send_command :listened, options[:song]
78
87
  end
79
88
  end
80
-
81
- socket.flush
82
- socket.close
@@ -20,6 +20,7 @@ class Cache
20
20
 
21
21
  @listened = []
22
22
  @loved = []
23
+ @unloved = []
23
24
  end
24
25
 
25
26
  def listened (song)
@@ -34,8 +35,14 @@ class Cache
34
35
  @loved << song
35
36
  end
36
37
 
38
+ def unlove (song)
39
+ return if @unloved.member? song
40
+
41
+ @unloved << song
42
+ end
43
+
37
44
  def empty?
38
- @listened.empty? && @loved.empty?
45
+ @listened.empty? && @loved.empty? && @unloved.empty?
39
46
  end
40
47
 
41
48
  def flush!
@@ -50,6 +57,12 @@ class Cache
50
57
 
51
58
  @loved.shift
52
59
  end
60
+
61
+ until @unloved.empty?
62
+ break unless fm.unlove! @unloved.first
63
+
64
+ @unloved.shift
65
+ end
53
66
  end
54
67
 
55
68
  def load (path)
@@ -57,15 +70,23 @@ class Cache
57
70
 
58
71
  data['listened'].each {|song|
59
72
  listened(Song.new(song))
60
- }
73
+ } if data['listened']
61
74
 
62
75
  data['loved'].each {|song|
63
76
  love(Song.new(song))
64
- }
77
+ } if data['loved']
78
+
79
+ data['unloved'].each {|song|
80
+ unlove(Song.new(song))
81
+ } if data['unloved']
65
82
  end
66
83
 
67
84
  def to_yaml
68
- { 'listened' => @listened.map(&:to_hash), 'loved' => @loved.map(&:to_hash) }.to_yaml
85
+ {
86
+ 'listened' => @listened.map(&:to_hash),
87
+ 'loved' => @loved.map(&:to_hash),
88
+ 'unloved' => @unloved.map(&:to_hash)
89
+ }.to_yaml
69
90
  end
70
91
  end
71
92
 
@@ -64,7 +64,6 @@ LOLastfm.define_checker :moc do
64
64
  end
65
65
 
66
66
  song = nil
67
- elsif e == :tags
68
67
  elsif e == :audio_start
69
68
  now_playing song = moc.status(true).to_song
70
69
  elsif e == :state && moc.status(true) == :paused
@@ -0,0 +1,47 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ require 'socket'
12
+ require 'json'
13
+
14
+ class LOLastfm
15
+
16
+ class Client
17
+ def initialize (host, port = nil)
18
+ @socket = port ? TCPSocket.new(host, port) : UNIXSocket.new(File.expand_path(host))
19
+ end
20
+
21
+ def respond_to_missing? (id, include_private = false)
22
+ @socket.respond_to?(id, include_private)
23
+ end
24
+
25
+ def method_missing (id, *args, &block)
26
+ if @socket.respond_to? id
27
+ return @socket.__send__ id, *args, &block
28
+ end
29
+
30
+ super
31
+ end
32
+
33
+ def send_command (type, *arguments)
34
+ @socket.puts [type, arguments].to_json
35
+ end
36
+
37
+ def read_response
38
+ JSON.parse(?[ + @socket.readline.chomp + ?]).first
39
+ end
40
+
41
+ def close
42
+ @socket.flush
43
+ @socket.close
44
+ end
45
+ end
46
+
47
+ end
@@ -21,7 +21,11 @@ module Glyr
21
21
 
22
22
  class Result
23
23
  def to_hash
24
- { source: source, url: url, data: data }
24
+ if respond_to? :to_str
25
+ { source: source, url: url, content: to_str }
26
+ else
27
+ { source: source, url: url }
28
+ end
25
29
  end
26
30
  end
27
31
 
@@ -37,6 +41,6 @@ LOLastfm.define_command :lyrics? do
37
41
  EM.defer -> {
38
42
  Glyr.query(title: song.title, artist: song.artist, album: song.album).lyrics
39
43
  }, -> results {
40
- send_line results.map(&:to_hash).to_json
44
+ send_response results.map(&:to_hash)
41
45
  }
42
46
  end
@@ -12,6 +12,10 @@ require 'json'
12
12
 
13
13
  class LOLastfm
14
14
 
15
+ define_command :version? do
16
+ send_response LOLastfm.version
17
+ end
18
+
15
19
  define_command :use do |*args|
16
20
  use *args
17
21
  end
@@ -36,12 +40,16 @@ define_command :love do |song|
36
40
  love song
37
41
  end
38
42
 
43
+ define_command :unlove do |song|
44
+ unlove song
45
+ end
46
+
39
47
  define_command :hint do |*args|
40
48
  hint *args
41
49
  end
42
50
 
43
51
  define_command :now_playing? do
44
- send_line now_playing?.to_json
52
+ send_response now_playing?
45
53
  end
46
54
 
47
55
  define_command :next? do
@@ -61,17 +69,11 @@ class Connection < EventMachine::Protocols::LineAndTextProtocol
61
69
  if block = LOLastfm.commands[command.to_sym.downcase]
62
70
  instance_exec *arguments, &block
63
71
  end
64
- rescue => e
72
+ rescue Exception => e
65
73
  $stderr.puts e.to_s
66
74
  $stderr.puts e.backtrace
67
75
  end
68
76
 
69
- def send_line (line)
70
- raise ArgumentError, 'the line already has a newline character' if line.include? "\n"
71
-
72
- send_data "#{line}\r\n"
73
- end
74
-
75
77
  def respond_to_missing? (id)
76
78
  @fm.respond_to?(id)
77
79
  end
@@ -83,6 +85,16 @@ class Connection < EventMachine::Protocols::LineAndTextProtocol
83
85
 
84
86
  super
85
87
  end
88
+
89
+ def send_line (line)
90
+ raise ArgumentError, 'the line already has a newline character' if line.include? "\n"
91
+
92
+ send_data line.dup.force_encoding('BINARY') << "\r\n"
93
+ end
94
+
95
+ def send_response (*arguments)
96
+ send_line (arguments.length == 1 ? arguments.first : arguments).to_json
97
+ end
86
98
  end
87
99
 
88
100
  end
@@ -10,6 +10,6 @@
10
10
 
11
11
  class LOLastfm
12
12
  def self.version
13
- '0.0.2'
13
+ '0.0.3'
14
14
  end
15
15
  end
data/lib/LOLastfm.rb CHANGED
@@ -157,6 +157,7 @@ class LOLastfm
157
157
 
158
158
  def love (song = nil)
159
159
  song = @last_played or return unless song
160
+ song = now_playing? or return if song == 'current' || song == :current
160
161
  song = Song.new(song) unless song.is_a? Song
161
162
  song = song.dup
162
163
 
@@ -177,6 +178,29 @@ class LOLastfm
177
178
  false
178
179
  end
179
180
 
181
+ def unlove (song = nil)
182
+ song = @last_played or return unless song
183
+ song = now_playing? or return if song == 'current' || song == :current
184
+ song = Song.new(song) unless song.is_a? Song
185
+ song = song.dup
186
+
187
+ return false unless fire :unlove, song
188
+
189
+ return false if song.nil?
190
+
191
+ unless unlove! song
192
+ @cache.unlove(song)
193
+ end
194
+
195
+ true
196
+ end
197
+
198
+ def unlove! (song)
199
+ @session.track.unlove(song.artist, song.title)
200
+ rescue
201
+ false
202
+ end
203
+
180
204
  def stopped_playing!
181
205
  @now_playing = nil
182
206
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: LOLastfm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-26 00:00:00.000000000 Z
12
+ date: 2012-06-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lastfm
@@ -67,6 +67,7 @@ files:
67
67
  - lib/LOLastfm/checkers/cmus.rb
68
68
  - lib/LOLastfm/checkers/moc.rb
69
69
  - lib/LOLastfm/checkers/process.rb
70
+ - lib/LOLastfm/client.rb
70
71
  - lib/LOLastfm/commands/glyr.rb
71
72
  - lib/LOLastfm/connection.rb
72
73
  - lib/LOLastfm/song.rb