Tagged in: shell

Opne data og linux

Joda. Det finst app. Det finst nettside og. Men yr.no i tekstformat, det er og veldig hendig.

Kvar gong eg loggar meg på ei maskin via SSH, eller kvar gong eg startar eit terminalvindu, så får eg opp

siste oppdaterte verdata frå yr.no. Korleis? Eit lite shell-skript;

#!/bin/bash

# URL for the weather data (replace this with the actual URL where you are fetching the data from)
API_OBSERVASJONAR="https://www.yr.no/api/v0/locations/5-42940/observations/day"
API_GJELDANDE="https://www.yr.no/api/v0/locations/5-42940/forecast/currenthour"

# Fetch the data using wget
wget -q -O weather_data.json "$API_OBSERVASJONAR"
wget -q -O currenthour.json "$API_GJELDANDE"

# Check if wget was successful
if [ $? -ne 0 ]; then
    echo "Failed to fetch data"
    exit 1
fi

# Parse the JSON and extract the 'max_temp' value using jq
max_temp=$(jq '.historical.summary.temperature.max.value' weather_data.json)
min_temp=$(jq '.historical.summary.temperature.min.value' weather_data.json)
precipitation=$(jq '.historical.summary.precipitation.total.value' weather_data.json)
wind=$(jq '.historical.summary.wind.maxWind.value' weather_data.json)
windGust=$(jq '.historical.summary.wind.maxGust.value' weather_data.json)
current_temp=$(jq '.temperature.value' currenthour.json)
feelslike_temp=$(jq '.temperature.feelsLike' currenthour.json)

# Check if jq successfully extracted the value
if [ $? -ne 0 ]; then
    echo "Failed to parse JSON"
    exit 1
fi

# Output the result
clear
echo "MÅLEDATA FOR SINNES MÅLESTASJON, METEROLOGISK INSTITUTT"
echo
echo "Gjeldande temperatur: $current_temp°"
echo "Temperatur kjennest som: $feelslike_temp°"
echo
echo "Makstemp siste 24 timer: $max_temp°"
echo "Minimumstemp siste 24 timer: $min_temp°"
echo
echo "Nedbør siste 24 timar: $precipitation mm"
echo
echo "Vind siste døgn: $wind m/s (maks vindkast: $windGust m/s)"
echo

# Clean up by removing the downloaded JSON file
rm weather_data.json
rm currenthour.json