Create a gist now

Instantly share code, notes, and snippets.

@VR2ZDX /wx.pl Secret
Last active Aug 9, 2016

#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
# Variables for temperature, humidity, wind direction, speed, gust and pressure
my $temperature;
my $humidity;
my $winddirection;
my $windspeed;
my $windgust;
my $pressure;
# RegEx match count
my $count;
# Convert month into 2 digit format
my $month = sprintf("%02d", ((localtime)[4] + 1) % 100);
# Convert day into 2 digit format
my $day = sprintf("%02d", (localtime)[3] % 100);
# Convert hour into 2 digit format
my $hour = sprintf("%02d", (localtime)[2] % 100);
# Convert minute into 2 digit format
my $minute = sprintf("%02d", (localtime)[1] % 100);
# URL of Regional Weather in Hong Kong (updates every 10 minutes)
my $html = get("http://data.weather.gov.hk/wxinfo/ts/text_readings_e.htm");
# Check if the page was loaded successfully and with right content
if (($html) && (index($html, "Regional Weather in Hong Kong") > -1)) {
# Extract temperature (3 bytes)
$count = () = $html =~ m{Shau Kei Wan[ ]+([\d\.?]+)};
if ($count > 0) {
$temperature = sprintf("%03d", ($1 * 9/5 + 32) % 1000);
} else {
$temperature = "...";
}
# Extract humidity (2 bytes)
$count = () = $html =~ m{HK Observatory[ ]+[\S]+[ ]+([\d]+)};
if ($count > 0) {
$humidity = sprintf("%02d", $1 % 100);
} else {
$humidity = "..";
}
# Extract wind direction (3 bytes)
my $pattern = "(km/hour)";
my $offset = index($html, $pattern) + length($pattern) + 1;
$html = substr($html, $offset);
$count = () = $html =~ m{Kai Tak[ ]+([^\s]+)};
if ($count > 0) {
if (lc $1 eq "north") { $winddirection = "000"; }
elsif (lc $1 eq "northeast") { $winddirection = "045"; }
elsif (lc $1 eq "east") { $winddirection = "090"; }
elsif (lc $1 eq "southeast") { $winddirection = "135"; }
elsif (lc $1 eq "south") { $winddirection = "180"; }
elsif (lc $1 eq "southwest") { $winddirection = "225"; }
elsif (lc $1 eq "west") { $winddirection = "270"; }
elsif (lc $1 eq "northwest") { $winddirection = "315"; }
elsif (lc $1 eq "variable") { $winddirection = "..."; }
else { # When wind direction is either "Calm" or "N/A"
$winddirection = "...";
$windspeed = "...";
$windgust = "...";
}
} else {
$winddirection = "...";
$windspeed = "...";
$windgust = "...";
}
if ((!defined $windspeed) || (!defined $windgust)){
# Extract wind speed (3 bytes)
$count = () = $html =~ m{Kai Tak[ ]+[\S]+[ ]+([\d]+)};
if ($count > 0) {
$windspeed = sprintf("%03d", $1 * 0.621371192 % 1000);
} else {
$windspeed = "...";
}
# Extract wind gust (3 bytes)
$count = () = $html =~ m{Kai Tak[ ]+[\S]+[ ]+[\S]+[ ]+([\d]+)};
if ($count > 0) {
$windgust = sprintf("%03d", $1 * 0.621371192 % 1000);
} else {
$windgust = "...";
}
}
# Extract pressure (5 bytes)
$pattern = "(hPa)";
$offset = index($html, $pattern) + length($pattern) + 1;
$html = substr($html, $offset);
$count = () = $html =~ m{HK Observatory[ ]+([\d\.?]+)};
if ($count > 0) {
$pressure = sprintf("%05d", $1 * 10 % 100000);
} else {
$pressure = ".....";
}
# Output weather in the formation _MMDDHHMMcCCCsSSSgGGGtTTThHHbBBBBB
print "_" . $month . $day . $hour . $minute . "c" . $winddirection . "s" . $windspeed . "g" . $windgust . "t" . $temperature . "h" . $humidity . "b" . $pressure;
} else {
# Return blank values when there is problem loading URL
print "_" . $month . $day . $hour . $minute . "c...s...g...t...h..b.....";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment