Skip to content

Instantly share code, notes, and snippets.

@albb0920
Last active July 12, 2017 16:18
Show Gist options
  • Save albb0920/918a13f8aa9259d90b07949142d8851d to your computer and use it in GitHub Desktop.
Save albb0920/918a13f8aa9259d90b07949142d8851d to your computer and use it in GitHub Desktop.
等公車快來再下樓
require "rubygems"
require "bundler"
Bundler.require(:default)
require "json"
ROUTE="672"
MY_STOP="博仁醫院"
NOTIFY_RANGE=-9..-6
def fetch_data(dataset)
hash = JSON.parse `curl -L https://tcgbusfs.blob.core.windows.net/blobbus/#{dataset}.gz | gzip -d`
Hashie::Mash.new(hash).BusInfo
end
all_routes = fetch_data "GetRoute"
route = all_routes.find {|r| r.pathAttributeName == ROUTE }
stops = fetch_data("GetStop").select{|s| s.routeId == route.Id }.sort_by(&:seqNo)
my_stop_idx = stops.index{|s| s.nameZh == MY_STOP }
alert_stops_ids = stops[(NOTIFY_RANGE.begin + my_stop_idx)..(NOTIFY_RANGE.end+my_stop_idx)].map(&:Id)
loop do
if fetch_data("GetBusEvent").any? {|b| alert_stops_ids.include? b.StopID.to_i }
`notify-send "公車要來了!!"`
exit
end
sleep 10
end
# frozen_string_literal: true
source "https://rubygems.org"
gem "hashie"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment