Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at


hpr4266 :: What's the weather?

Lee writes a script to check what the weather is like

<< First, < Previous, , Latest >>

Hosted by Lee on Monday, 2024-12-09 is flagged as Clean and is released under a CC-BY-SA license.
php. 1.
The show is available on the Internet Archive at: https://archive.org/details/hpr4266

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:17:26

general.

One web service that can find out the weather on the command line is:

$ curl wttr.in

Inspired by this is the following script in PHP that uses data from 7timer.info

Available at https://gist.github.com/macsplit

<?php

date_default_timezone_set('UTC');

$lon = "-0.19759370325694858";
$lat = "51.35956063540873";

$url = "https://www.7timer.info/bin/civil.php?lon=$lon&lat=$lat&unit=metric&ac=0&output=json";

$weathers = [
    'tsday' => "☀️ Hot Day",
    'clearday' => "☀️ Clear Day",
    'clearnight' => "🌙 Clear Night",
    'pcloudyday' => "⛅ Partially Cloudy Day",
    'pcloudynight' => "☁️ Partially Cloudy Night",
    'mcloudyday' => "🌥️ Moderately Cloudy Day",
    'mcloudynight' => "☁️ Moderately Cloudy Night",
    'cloudyday' => "☁️ Cloudy Day",
    'cloudynight' => "☁️ Cloudy Night",
    'humidday' => "🌡️ Humid Day",
    'humidnight' => "🌡️ Humid Night",
    'lightrainday' => "🌧️ Lightly Rainy Day",
    'lightrainnight' => "🌧️ Lightly Rainy Night",
    'oshowerday' => "🌧️ Overcast Showery Day",
    'oshowernight' => "🌧️ Overcast Showery Night",
    'ishowerday' => "🌧️ Showery Day",
    'ishowernight' => "🌧️ Showery Night",
    'lightsnowday' => "❄️ Lightly Snowy Day",
    'lightsnownight' => "❄️ Lightly Snowy Night",
    'rainday' => "🌧️ Rainy Day",
    'rainnight' => "🌧️ Rainy Night",
    'snowday' => "❄️ Snowy Day",
    'snownight' => "❄️ Snowy Night",
    'rainsnowday' => "🌨️ Rainy and Snowy Day",
    'rainsnownight' => "🌨️ Rainy and Snowy Night"
];

$json = file_get_contents($url);
if (!$json) exit();
$obj = json_decode($json);
$init = DateTime::createFromFormat('YmdH', $obj->init)->getTimestamp();
$series = $obj->dataseries;
$now = time();
foreach ($series as $k => $point) {
    $timepoint = $point->timepoint;
    $timestamp = $init + ($timepoint * 3600);
    $diff = abs($timestamp - $now);
    $data[$diff] = $point;
}
$key = min(array_keys($data));
$measure = $data[$key];
$temperature = $measure->{'temp2m'};
$weather_desc = $weathers[$measure->weather];

echo "$weather_desc $temperature"."°C\n";

?>

Editor's Note: Modified 2024-11-13 in line with comment #1


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2024-10-21 09:53:44 by Lee

Errata

Having tested for different cities I'd advise setting the default timezone to 'UTC' in this script regardless of actual location.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the letter P in HPR stand for?