Skip to content

Instantly share code, notes, and snippets.

@hryk
Last active December 11, 2015 22:49
Show Gist options
  • Save hryk/4672441 to your computer and use it in GitHub Desktop.
Save hryk/4672441 to your computer and use it in GitHub Desktop.
20分~30分くらいの曲を探してきて適当に再生する。
#!/usr/bin/env ruby
require 'logger'
require 'rubygems'
require 'soundcloud'
require 'rainbow'
$: << "./soundcloud2000/lib"
require 'soundcloud2000'
def duration_to_s(mills)
total_sec = mills / 1000
sec = total_sec % 60
min = (total_sec / 60) % 60
hr = (total_sec / 3600) % 60
if hr > 0
"#{hr}hour#{min}min#{sec}sec"
elsif min > 0
"#{min}min#{sec}sec"
else
"#{sec}sec"
end
end
client = Soundcloud2000::Client.new("CLIENT_ID")
tracks = client.get('/tracks', :tags => "Anime", duration:{
from: 20*60*1000,
to: 30*60*1000
})
# Create Player
player = Soundcloud2000::Player.new(Logger.new($stderr))
track = tracks[(rand(tracks.size).to_i-1)]
duration = track.duration.to_i
finish = Time.now + (track.duration.to_i/1000)
banner = <<-TITLE
Start Pomodoro!
---------------
Track: #{track.title}
Duration: #{duration_to_s(duration)}
END: #{finish.hour}:#{finish.min}:#{finish.sec}
TITLE
play = true
puts banner.color(:green)
player.play(track, client.location(track.stream_url))
loop do
exit unless player.playing?
sleep 0.1
end
puts "Finish pomodoro!".color(:green)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment