-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
executable file
·213 lines (176 loc) · 5.78 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/ruby
require 'rubygems'
require 'scrobbler'
require 'pp'
# http://www.last.fm/api/submissions
# http://scrobbler.rubyforge.org/docs/
#############################
# Configuration
# authentication file
AUTHFILE='authfile'
# poll interval
POLL_INTERVAL=1
# debug (don't do the actual scrobbling)
DEBUG = true
# log to file
# OUTPUT = 'mocp-scrobbler.log'
#############################
def log(str)
if defined? OUTPUT
@logfile.write(str+"\n")
else
puts str
end
end
class Track
attr_reader :file, :artist, :track, :album, :length, :track_number, :time, :currentsec, :submittable
def initialize(track_hash)
@file = track_hash[:file]
@artist = track_hash[:artist] || ''
@track = track_hash[:track] || ''
@album = track_hash[:album] || ''
@length = track_hash[:length]
@track_number = track_hash[:track_number] || ''
@time = Time.now
@currentsec = track_hash[:currentsec]
end
def submittable?
if !has_tags?
log "#{@file} has no tags"
return false
end
totalsec = @length.to_i
currentsec = @currentsec.to_i
log "TotalSec: #{totalsec.to_s}, CurrentSec: #{currentsec.to_s}, HalfSec: #{(totalsec/2).to_s}"
if totalsec <= 30
log "song shorter that 30s"
return false
end
if currentsec >= 240 or currentsec >= totalsec/2
return true
else
log "you have not reached 240s or half of the song"
return false
end
end
def has_tags?
tags = [@artist, @album, @track]
tags.each {|t| return false if t.empty? }
true
end
def update_currentsec(track)
@currentsec = track.currentsec
end
def ==(track)
@file == track.file
end
def display
info = @file + "\n"
info += "Artist: " + @artist + "\n"
info += "Album: " + @album + "\n"
info += "Track: " + @track + "\n"
puts info
end
end
module ScrobblerClient
def initialize
@config = get_config
connect if !@config[:debug]
@logfile = File.open(OUTPUT,'a') if @config[:output]
end
def get_config
config = Hash.new
config[:debug] = defined? DEBUG ? DEBUG : false
config[:output] = defined? OUTPUT ? OUTPUT : false
config[:poll_interval] = POLL_INTERVAL
config[:authfile] = AUTHFILE
config
end
def connect
log "Connecting!"
(login,password) = File.new(@config[:authfile]).readlines[0].chomp.split(':')
@auth = Scrobbler::SimpleAuth.new(:user => login, :password => password)
@auth.handshake!
end
def submit_now_playing(track)
log "actually submitting the now_playing!"
#playing = Scrobbler::Playing.new(:session_id => @auth.session_id,
# :now_playing_url => @auth.now_playing_url,
# :artist => @fileinfo['Artist'],
# :track => @fileinfo['SongTitle'],
# :album => @fileinfo['Album'],
# :length => @fileinfo['TotalSec'],
# :track_number => '')
#playing.submit!
end
def submit_scrobble(track)
log "actually doing the scroblle!"
#scrobble = Scrobbler::Scrobble.new(:session_id => @auth.session_id,
# :submission_url => @auth.submission_url,
# :artist => @fileinfo['Artist'],
# :track => @fileinfo['SongTitle'],
# :album => @fileinfo['Album'],
# :time => @time_start,
# :length => @fileinfo['TotalSec'],
# :track_number => '')
#scrobble.submit!
end
def poll
if server_running?
track = get_track
if !@currentTrack
@currentTrack = track
if @currentTrack.has_tags?
log "updating now playing: #{@currentTrack.track}"
submit_now_playing if !@config[:debug] and
else
log "artist
end
else
if @currentTrack == track
@currentTrack.update_currentsec(track)
else
if @currentTrack.submittable?
log "scrobbling #{@currentTrack.track}"
end
@currentTrack = track
log "now playing #{@currentTrack.track}"
end
end
else
log "server stopped"
end
end
end
class MocpScrobbler
include ScrobblerClient
def get_track
fileinfo = Hash.new
`mocp -i`.split("\n").each do |l|
(k,v) = l.split(': ')
if v.class != String
v = String.new
end
fileinfo[k.intern] = v.strip
end
track_hash = {
:file => fileinfo[:File],
:artist => fileinfo[:Artist],
:track => fileinfo[:SongTitle],
:album => fileinfo[:Album],
:length => fileinfo[:TotalSec],
:track_number => '',
:currentsec => fileinfo[:CurrentSec]
}
Track.new(track_hash)
end
def server_running?
state = `mocp -i`.split("\n").select{|el| el =~ /^State: /}.first.split(': ')[1]
state != 'STOP'
end
end
mocp = MocpScrobbler.new
while true
mocp.poll
sleep POLL_INTERVAL
end