Last active
December 11, 2015 23:08
-
-
Save hryk/4674649 to your computer and use it in GitHub Desktop.
20分~30分くらいの曲を探してきて適当に再生する。MacRubyで。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
#encoding: utf-8 | |
# | |
# TODO | |
# ---- | |
# | |
# # Output inventories as HTML. | |
# ./pomosound.rb report > report.html | |
# | |
# # Show today's inventory | |
# ./pomosound.rb ls | |
# | |
# # Show yesterday's inventory | |
# ./pomosound.rb ls ~1 | |
# | |
# # Search within inventory. | |
# ./pomosound.rb find 'hogehoge' | |
# | |
# # Add pomodoro to inventory. | |
# ./pomosound.rb < "Task description" | |
# | |
# # Remove pomodoro from inventory | |
# ./pomosound.rb rm [TASKID] | |
# | |
# # Change tag | |
# ./pomosound.rb config --tag House | |
# | |
# # Change duration | |
# ./pomosound.rb config --duration 25min..30min | |
# | |
# # Add disable application | |
# ./pomosound.rb config --disable_apps Skype,夜フクロウ | |
# | |
# # Start pomosound with registered task. | |
# ./pomosound.rb [TASKID] | |
# | |
require 'net/http' | |
require 'uri' | |
require 'rubygems' | |
require 'multi_json' | |
require 'soundcloud' | |
require 'rainbow' | |
framework 'AppKit' | |
class Pomosound | |
include Dispatch | |
attr_accessor :disable_apps, :todo | |
attr_reader :player | |
def initialize(client_id) | |
@client_id = client_id | |
@client = Soundcloud.new(:client_id => client_id) | |
@todo = "Something to-do" | |
@disable_apps = [] | |
end | |
def init_player(track) | |
track_url = track_location(track) | |
location = NSURL.URLWithString(track_url) | |
@player = NSSound.alloc.initWithContentsOfURL(location, | |
byReference: false) | |
end | |
def run | |
track = select_track | |
init_player(track) | |
Signal.trap(Signal.list["INT"], "IGNORE") | |
viewq = Queue.concurrent | |
progress = Source.timer(1, 1, 0.1, viewq) {|s| | |
left_time = ((player.duration - player.currentTime) * 1000).to_i | |
print "\033[2K\033[1G#{duration_to_s(left_time).color(:red)}" | |
} | |
Queue.main.after(TIME_NOW){ | |
terminate_apps | |
player.play | |
preexit = lambda { | |
player.stop | |
progress.cancel! | |
relaunch_apps | |
exit 0 | |
} | |
Queue.current.after(self.player.duration){ preexit.call } | |
Source.new( | |
Source::PROC,$$, | |
Source::PROC_SIGNAL,Queue.current | |
){ | |
preexit.call | |
} | |
viewq.after(TIME_NOW){ | |
show_start_banner(track) | |
Queue.current.after(self.player.duration){ | |
show_finish_banner | |
} | |
} | |
} | |
Queue.main.run | |
end | |
def show_finish_banner | |
banner = "\n---------------------------------------\n" + | |
"Finish pomodoro." | |
puts banner.color(:green) | |
end | |
def show_start_banner(track) | |
duration = track.duration.to_i | |
finish_time = Time.now + (track.duration.to_i/1000) | |
banner = <<-TITLEBANNER | |
--------------------------------------- | |
Start Pomodoro! | |
======================================= | |
TODO: #{self.todo} | |
======================================= | |
Track: #{track.title} | |
Duration: #{duration_to_s(duration)} | |
Finish: #{finish_time.hour}:#{finish_time.min}:#{finish_time.sec} | |
--------------------------------------- | |
TITLEBANNER | |
puts banner.color(:green) | |
end | |
def select_track | |
print "Selecting BGM...".color(:green) | |
tracks = begin | |
@client.get('/tracks', | |
:tag_list => "Anime", | |
:duration => { | |
:from => 20*60*1000, | |
:to => 30*60*1000 | |
}) | |
rescue Soundcloud::ResponseError => e | |
puts "Soundcloud seems to be dead.." | |
puts "Error: #{e.message}, #{e.response.code}" | |
abort | |
end | |
puts "done.".color(:green) | |
tracks[(rand(tracks.size).to_i-1)] | |
end | |
def terminate_apps | |
ws = NSWorkspace.sharedWorkspace | |
apps = ws.runningApplications | |
@applist = {} | |
apps.each do |app| | |
@applist[app.localizedName] = app.executableURL.path.lastPathComponent | |
if disable_apps.include? app.localizedName | |
app.terminate | |
end | |
end | |
end | |
def relaunch_apps | |
disable_apps.each do |localized_name| | |
NSWorkspace.sharedWorkspace. | |
launchApplication(@applist[localized_name]) | |
end | |
end | |
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 | |
def track_location(track) | |
uri = URI.parse(track.stream_url + '?client_id=' + @client_id) | |
Net::HTTP.get_response(uri) do |res| | |
if res.code == '302' | |
return res.header['Location'] | |
end | |
end | |
end | |
end | |
pomo = Pomosound.new("CLIENT_ID") | |
pomo.disable_apps << "夜フクロウ" | |
pomo.disable_apps << "Skype" | |
pomo.disable_apps << "iTunes" | |
pomo.disable_apps << "LINE" | |
pomo.todo = ARGV[0] if ARGV.size > 0 | |
pomo.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment