LOLastfm 0.0.3.1 → 0.0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/LOLastfm.rb CHANGED
@@ -10,6 +10,7 @@
10
10
 
11
11
  require 'eventmachine'
12
12
  require 'lastfm'
13
+ require 'stringio'
13
14
 
14
15
  class LOLastfm
15
16
  def self.load (path)
@@ -40,6 +41,7 @@ class LOLastfm
40
41
  @events = Hash.new { |h, k| h[k] = [] }
41
42
 
42
43
  cache_at '~/.LOLastfm/cache'
44
+ logs_at '~/.LOLastfm/logs'
43
45
  end
44
46
 
45
47
  def load (path)
@@ -66,6 +68,7 @@ class LOLastfm
66
68
  end
67
69
 
68
70
  @checker.start
71
+ @cache.flush!
69
72
  end
70
73
 
71
74
  def stop
@@ -87,6 +90,19 @@ class LOLastfm
87
90
  end
88
91
  end
89
92
 
93
+ def logs_at (path)
94
+ @logs_at = File.expand_path(path)
95
+ end
96
+
97
+ def server_at (host, port = nil)
98
+ if port
99
+ @host = host
100
+ @port = port
101
+ else
102
+ @path = host
103
+ end
104
+ end
105
+
90
106
  def save
91
107
  if @cache_at
92
108
  File.open(@cache_at, 'w') {|f|
@@ -95,13 +111,25 @@ class LOLastfm
95
111
  end
96
112
  end
97
113
 
98
- def server_at (host, port = nil)
99
- if port
100
- @host = host
101
- @port = port
114
+ def log (what, group = nil)
115
+ io = StringIO.new
116
+
117
+ io.print "[#{Time.now}#{" (#{group})" if group}] "
118
+
119
+ if what.is_a? Exception
120
+ io.puts "#{what.class.name}: #{what.message}"
121
+ io.puts e.backtrace
102
122
  else
103
- @path = host
123
+ io.puts what
104
124
  end
125
+
126
+ io.seek 0
127
+
128
+ io.read.tap {|text|
129
+ $stderr.puts text
130
+
131
+ File.open(@logs_at, 'a') { |f| f.print text }
132
+ }
105
133
  end
106
134
 
107
135
  def session (key)
@@ -143,15 +171,17 @@ class LOLastfm
143
171
  unless listened! song
144
172
  @cache.listened(song)
145
173
  end
174
+
175
+ @last_played = song
146
176
 
147
177
  true
148
178
  end
149
179
 
150
180
  def listened! (song)
151
- @session.track.scrobble(song.artist, song.title, song.listened_at.to_time.to_i, song.album, song.track, song.id, song.length).tap {
152
- @last_played = song
153
- }
154
- rescue
181
+ @session.track.scrobble(song.artist, song.title, song.listened_at.to_time.to_i, song.album, song.track, song.id, song.length)
182
+ rescue Exception => e
183
+ log e, :listened
184
+
155
185
  false
156
186
  end
157
187
 
@@ -174,7 +204,9 @@ class LOLastfm
174
204
 
175
205
  def love! (song)
176
206
  @session.track.love(song.artist, song.title)
177
- rescue
207
+ rescue Exception => e
208
+ log e, :love
209
+
178
210
  false
179
211
  end
180
212
 
@@ -197,7 +229,9 @@ class LOLastfm
197
229
 
198
230
  def unlove! (song)
199
231
  @session.track.unlove(song.artist, song.title)
200
- rescue
232
+ rescue Exception => e
233
+ log e, :unlove
234
+
201
235
  false
202
236
  end
203
237
 
@@ -24,19 +24,19 @@ class Cache
24
24
  end
25
25
 
26
26
  def listened (song)
27
- return if @loved.member? song
27
+ return if song.nil? || @listened.member?(song)
28
28
 
29
29
  @listened << song
30
30
  end
31
31
 
32
32
  def love (song)
33
- return if @loved.member? song
33
+ return if song.nil? || @loved.member?(song)
34
34
 
35
35
  @loved << song
36
36
  end
37
37
 
38
38
  def unlove (song)
39
- return if @unloved.member? song
39
+ return if song.nil? || @unloved.member?(song)
40
40
 
41
41
  @unloved << song
42
42
  end
@@ -47,19 +47,19 @@ class Cache
47
47
 
48
48
  def flush!
49
49
  until @listened.empty?
50
- break unless fm.listened! @listened.first
50
+ break unless fm.listened! Song.new(@listened.first)
51
51
 
52
52
  @listened.shift
53
53
  end
54
54
 
55
55
  until @loved.empty?
56
- break unless fm.love! @loved.first
56
+ break unless fm.love! Song.new(@loved.first)
57
57
 
58
58
  @loved.shift
59
59
  end
60
60
 
61
61
  until @unloved.empty?
62
- break unless fm.unlove! @unloved.first
62
+ break unless fm.unlove! Song.new(@unloved.first)
63
63
 
64
64
  @unloved.shift
65
65
  end
@@ -68,24 +68,24 @@ class Cache
68
68
  def load (path)
69
69
  data = YAML.parse_file(path).transform
70
70
 
71
- data['listened'].each {|song|
71
+ data[:listened].each {|song|
72
72
  listened(Song.new(song))
73
- } if data['listened']
73
+ } if data[:listened]
74
74
 
75
- data['loved'].each {|song|
75
+ data[:loved].each {|song|
76
76
  love(Song.new(song))
77
- } if data['loved']
77
+ } if data[:loved]
78
78
 
79
- data['unloved'].each {|song|
79
+ data[:unloved].each {|song|
80
80
  unlove(Song.new(song))
81
- } if data['unloved']
81
+ } if data[:unloved]
82
82
  end
83
83
 
84
84
  def to_yaml
85
85
  {
86
- 'listened' => @listened.map(&:to_hash),
87
- 'loved' => @loved.map(&:to_hash),
88
- 'unloved' => @unloved.map(&:to_hash)
86
+ listened: @listened.map(&:to_hash),
87
+ loved: @loved.map(&:to_hash),
88
+ unloved: @unloved.map(&:to_hash)
89
89
  }.to_yaml
90
90
  end
91
91
  end
@@ -86,8 +86,7 @@ LOLastfm.define_checker :moc do
86
86
  end
87
87
  }
88
88
  rescue Exception => e
89
- $stderr.puts e.message
90
- $stderr.puts e.backtrace
89
+ log e, 'checker: moc'
91
90
  end
92
91
 
93
92
  if song.stream?
@@ -11,24 +11,25 @@
11
11
  LOLastfm.define_checker :process do
12
12
  settings.default[:every] = 5
13
13
 
14
- set_interval settings[:every] do
15
- if status == :stopped
16
- if @last == :playing
17
-
18
- end
19
- else
14
+ ENV['PATH'].split(':').each {|path|
15
+ if File.executable?("#{path}/lsof")
16
+ break settings.default[:lsof] = "#{path}/lsof"
20
17
  end
18
+ }
21
19
 
22
- @last = status
20
+ unless settings[:name]
21
+ raise 'I need the name of the process to check'
23
22
  end
24
23
 
25
- hint do |path|
26
- if @hint && path != @hint
27
- next unless listened path: path
28
- end
24
+ unless settings[:lsof]
25
+ raise 'I need the path to lsof to work'
26
+ end
29
27
 
30
- next unless now_playing path: path
28
+ set_interval settings[:every] do
29
+
30
+ end
31
31
 
32
- @hint = path
32
+ hint do |path|
33
+ listened path: path
33
34
  end
34
35
  end
@@ -70,8 +70,7 @@ class Connection < EventMachine::Protocols::LineAndTextProtocol
70
70
  instance_exec *arguments, &block
71
71
  end
72
72
  rescue Exception => e
73
- $stderr.puts e.to_s
74
- $stderr.puts e.backtrace
73
+ f.log e, "command: #{command}"
75
74
  end
76
75
 
77
76
  def respond_to_missing? (id)
data/lib/LOLastfm/song.rb CHANGED
@@ -53,17 +53,25 @@ class Song
53
53
 
54
54
  if @listened_at
55
55
  @listened_at = @listened_at.is_a?(String) ? DateTime.parse(@listened_at) : @listened_at.to_datetime
56
- elsif @length
57
- @listened_at = (Time.now - @length).to_datetime
58
- else
59
- @listened_at = DateTime.now
60
56
  end
61
57
  end
62
58
 
63
- def fill!
64
- return unless @path
59
+ def listened_at
60
+ return @listened_at if @listened_at
61
+
62
+ unless nil? || @listened_at
63
+ if @length && !stream?
64
+ @listened_at = (Time.now - @length).to_datetime
65
+ else
66
+ @listened_at = DateTime.now
67
+ end
68
+ end
65
69
 
66
- return if @track && @title && @album && @artist && @comment && @length && @id
70
+ @listened_at
71
+ end
72
+
73
+ def fill!
74
+ return if !@path || (@track && @title && @album && @artist && @comment && @length && @id)
67
75
 
68
76
  TagLib::FileRef.open(@path) {|f|
69
77
  if f.tag
@@ -140,6 +148,22 @@ class Song
140
148
  def to_yaml
141
149
  to_hash.to_yaml
142
150
  end
151
+
152
+ def inspect
153
+ header = ''
154
+ header << "#{id} " if id
155
+ header << "listened at #{listened_at} " if listened_at
156
+ header << "#{length} seconds " if length
157
+ header << "found at #{path} " if path
158
+
159
+ parts = ''
160
+ parts << "track=#{track} " if track
161
+ parts << "title=#{title} " if title
162
+ parts << "artist=#{artist} " if artist
163
+ parts << "album=#{album} " if album
164
+
165
+ "#<LOLastfm::Song#{"(#{header[0 .. -2]})" unless header.empty?}:#{" #{parts[0 .. -2]}" if parts}>"
166
+ end
143
167
  end
144
168
 
145
169
  end
@@ -10,6 +10,6 @@
10
10
 
11
11
  class LOLastfm
12
12
  def self.version
13
- '0.0.3.1'
13
+ '0.0.3.2'
14
14
  end
15
15
  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.3.1
4
+ version: 0.0.3.2
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-07-01 00:00:00.000000000 Z
12
+ date: 2012-07-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: lastfm