Last active
February 3, 2016 23:40
-
-
Save numinit/00b1c4f0ffc7f41b57ba to your computer and use it in GitHub Desktop.
Requires ruby 2.1+. Released under the MIT license. `colorize` gem is optional, but adds colors. Example output: https://i.imgur.com/BmtAoXc.png
This file contains 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 -*- | |
####################################################################### | |
# woomy.rb prints the current Splatoon map rotations. # | |
# If the `colorize` gem is installed, it will print them with colors. # | |
####################################################################### | |
=begin | |
Copyright (c) 2015+ Morgan Jones | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
=end | |
require 'uri' | |
require 'net/http' | |
require 'json' | |
begin | |
require 'colorize' | |
rescue LoadError => e | |
STDERR.puts "#$0: error loading colorize, your terminal output won't be as fresh" if $VERBOSE | |
end | |
module Woomy | |
BOOYAH_BASE = URI('https://splatoon.ink/schedule.json').freeze | |
module Refinements | |
refine String do | |
def method_missing method, *args, &block | |
self | |
end | |
end | |
refine Numeric do | |
def ms | |
Time.at self / 1000, (self % 1000) * 1000 | |
end | |
def duration | |
ret = [] | |
val = self | |
hours = (val / 3600).floor | |
val %= 3600 | |
minutes = (val / 60).floor | |
val %= 60 | |
seconds = val.floor | |
ret << "#{hours} #{hours == 1 ? 'hour'.freeze : 'hours'.freeze}" if hours > 0 | |
ret << "#{minutes} #{minutes == 1 ? 'minute'.freeze : 'minutes'.freeze}" | |
ret << "#{seconds} #{seconds == 1 ? 'second'.freeze : 'seconds'.freeze}" | |
ret.join ', '.freeze | |
end | |
end | |
refine Time do | |
def format | |
self.strftime '%F %T'.freeze | |
end | |
end | |
end | |
module Version | |
VERSION = '0.2'.freeze | |
NAME = Woomy.to_s.freeze | |
IDENT = "#{NAME} #{VERSION}".freeze | |
AGENT = '3'.freeze | |
end | |
include Version | |
end | |
# Activate the refinements | |
using Woomy::Refinements | |
begin | |
# Make the request | |
req = Net::HTTP::Get.new Woomy::BOOYAH_BASE | |
req['User-Agent'.freeze] = Woomy::AGENT | |
req['Accept'.freeze] = 'application/json; charset=UTF-8'.freeze | |
req['X-Requested-With'.freeze] = Woomy::IDENT | |
res = Net::HTTP.start(Woomy::BOOYAH_BASE.hostname, Woomy::BOOYAH_BASE.port, | |
use_ssl: Woomy::BOOYAH_BASE.scheme == 'https'.freeze) {|h| h.request(req)} | |
if res.is_a? Net::HTTPSuccess | |
# Parse the schedule | |
schedule = JSON.parse res.body, symbolize_names: true | |
now = Time.now | |
update_time = schedule[:updateTime].ms | |
cur_start, cur_end = nil, nil | |
# Print each item | |
language = ENV['LANG'].to_s =~ /ja/ ? :jp : :en | |
schedule[:schedule].each do |item| | |
# Get start and end time | |
start_time = item[:startTime].ms | |
end_time = item[:endTime].ms | |
STDOUT.print "From #{start_time.format.yellow} to #{(end_time - 1).format.blue}:" | |
if now >= start_time and now < end_time | |
cur_start, cur_end = start_time, end_time | |
STDOUT.print " #{'<:='.white.on_magenta}" | |
end | |
STDOUT.puts | |
# Print each mode for this slot | |
item[:modes].each do |mode| | |
maps = mode[:maps].map {|a| a[:name][language]} | |
rules = mode[:rules][language] | |
color = case mode[:rules][:en] | |
when /Splatfest/i then :light_blue | |
when /Turf War/i then :green | |
when /Rainmaker/i then :blue | |
when /Tower Control/i then :magenta | |
when /Splat Zones/i then :cyan | |
else :white | |
end | |
STDOUT.puts " >> #{rules.colorize(color)}: #{maps.join '; '.freeze}" | |
end | |
end | |
# Print the footer | |
STDOUT.puts "It's #{now.format.light_magenta}. Updated at #{update_time.format.light_magenta}." | |
if !cur_start.nil? and !cur_end.nil? | |
STDOUT.puts "#{(cur_end - now).duration.magenta} left in map rotation. " | |
end | |
exit 0 | |
else | |
# Problem with the response | |
raise "got HTTP #{res.code} from #{Woomy::BOOYAH_BASE}" | |
end | |
rescue => e | |
# Issue with something; catch it and exit with a meaningful error code | |
STDERR.puts "#$0: #{e.class}: #{e.message}" | |
STDERR.puts e.backtrace | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment