Last active
May 6, 2019 17:32
-
-
Save igneus/3ea0627a515951a509c7 to your computer and use it in GitHub Desktop.
Koupaliště Petynka - teplota vzduchu, vody a počet návštěvníků bez otvírání webového prohlížeče (Crystal)
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
# coding: utf-8 | |
# petynka.cr | |
# | |
# Extracts temperature and count of visitors from the website | |
# of Koupaliste Petynka, Prague | |
require "http/client" | |
require "http/headers" | |
require "option_parser" | |
def strip_xml_tags(str : String) | |
str.gsub(/<.+?>/, "").gsub(/\s+/, ' ') | |
end | |
options = { | |
:only_visitors => false, | |
:url => "https://www.koupalistepetynka.cz/", | |
} | |
OptionParser.parse! do |parser| | |
parser.on("-V", "--visitors", "Print visitor count only") do | |
options[:only_visitors] = true | |
end | |
parser.on("-u URL", "--url=URL", "URL of the page to scrape (mostly for debugging purposes)") do |url| | |
options[:url] = url | |
end | |
end | |
headers = HTTP::Headers.new.add "User-Agent", "petynka.cr https://gist.github.com/igneus/3ea0627a515951a509c7" | |
response = HTTP::Client.get options[:url].as(String), headers | |
page = response.body | |
searched = [ | |
/<h1>(.+?)<\/h1>\s*<div class="description">teplota vody<\/div>/i, | |
/<h1>(.+?)<\/h1>\s*<div class="description">teplota vzduchu<\/div>/i, | |
/<h1>(.+?)<\/h1>\s*<div class="description text-uppercase">návštěvní(k|ci|ků)<\/div>/i, | |
] | |
if options[:only_visitors] | |
match = page.match searched.last | |
if match.nil? | |
puts "?" | |
else | |
puts match[1] | |
end | |
else | |
puts Time::Format.new("%H:%M").format Time.now.to_local | |
searched.each do |term| | |
m = page.match(term) | |
puts strip_xml_tags(m[0]) unless m.nil? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment