Skip to content

Instantly share code, notes, and snippets.

@VR2ZDX

VR2ZDX/wx.pl Secret

Last active August 9, 2016 04:02
Show Gist options
  • Save VR2ZDX/511570cea19d189e3d6c2bd47aae7b21 to your computer and use it in GitHub Desktop.
Save VR2ZDX/511570cea19d189e3d6c2bd47aae7b21 to your computer and use it in GitHub Desktop.
# 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");
# Extract temperature from HTML (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 = ".....";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment