Skip to content

Instantly share code, notes, and snippets.

@mendelgusmao

mendelgusmao/wa.random.rb

Last active Jul 10, 2017
Embed
What would you like to do?
Random Winamp playlist using ActiveWinamp, Ruby and Win32OLE
require "win32ole"
count = 100
ttl = 86400
cache_file = "wa.random.cache"
query = [
'type = "0"',
'length > "2:00"',
'title NOTHAS "intro"',
'filename NOTHAS "~"'
].join(" AND ")
winamp = WIN32OLE.connect "ActiveWinamp.Application"
winamp.playlist.clear
cache = File.open(cache_file, "r") { |io| io.read }.to_i
if cache > 0 and File.mtime(cache_file) + ttl < Time.now
(0..cache).to_a.sample(count).each do |index|
winamp.medialibrary.item(index).enqueue
end
else
items = winamp.medialibrary.runqueryarray(query)
items.sample(count).map &:enqueue
File.open(cache_file, "w") { |io| io.write(items.size) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment