From d53b97c35dba123fd97d8b45917572cd9daf0773 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Sat, 23 Feb 2019 08:41:41 +0100 Subject: [PATCH] initial --- Gemfile | 8 ++++++++ Gemfile.lock | 17 +++++++++++++++++ bvg.rb | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100755 bvg.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..4c74602 --- /dev/null +++ b/Gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } + +gem "nokogiri" +gem "rainbow" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..3096d2e --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,17 @@ +GEM + remote: https://rubygems.org/ + specs: + mini_portile2 (2.4.0) + nokogiri (1.10.1) + mini_portile2 (~> 2.4.0) + rainbow (3.0.0) + +PLATFORMS + ruby + +DEPENDENCIES + nokogiri + rainbow + +BUNDLED WITH + 1.16.2 diff --git a/bvg.rb b/bvg.rb new file mode 100755 index 0000000..f1a55ff --- /dev/null +++ b/bvg.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby + +require "open-uri" +require "nokogiri" +require "rainbow" + +Departure = Struct.new(:time, :line, :terminus) do + def self.from_row(row) + Departure.new(row[0].text.strip, row[1].text.strip.gsub(/\s+/, " "), row[2].text.strip) + end + + def log + time_string = if time[-1] == "*" then Rainbow(time[0..4]).inverse else time end + line_string = { + 'B' => Rainbow(line).magenta, + 'R' => Rainbow(line).white, + 'U' => Rainbow(line).blue, + 'S' => Rainbow(line).green, + 'T' => Rainbow(line).red, + }[line[0]] || line + + printf("%s %-20s %s\n", time_string, line_string, terminus) + end +end + +def request_url(input) + "http://mobil.bvg.de/Fahrinfo/bin/stboard.bin/dox?" + URI.encode_www_form( + "input" => URI.encode_www_form_component(input), + "start" => "yes", + "boardType" => "depRT", + ) +end + +url = request_url(ARGV[0]) + +document = Nokogiri::HTML(open(url)) + +title = document.xpath("//div[@id=\"ivu_overview_input\"]/strong") +puts Rainbow(title.text).bright if title + +document.xpath("//span[@class=\"select\"]/a").each do |option| + station_name = option.text.strip + station_code = option.xpath("@href").text[/input=(\d+)/, 1] + print station_code, " ", Rainbow(station_name).yellow, "\n" +end + +document.xpath("//table/tbody/tr").each do |row| + columns = row.xpath("td") + Departure.from_row(columns).log unless columns.length < 3 +end