Last active
July 12, 2017 16:18
-
-
Save albb0920/918a13f8aa9259d90b07949142d8851d to your computer and use it in GitHub Desktop.
等公車快來再下樓
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
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 |
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
# 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