You are not logged in.

#1 2019-06-21 16:03:10

Nili
Member
From: $HOME/♫♪
Registered: 2015-09-30
Posts: 921
Website

tint2-weather - a small modification

I'm using tint2-weather script executor by
addy-dclxv

#!/bin/bash

# I take this script from Anachron's i3blocks
# I only slightly modify this script to add an option to show icon
# I also remove the i3blocks specify script
# To make this works with tint2 executor, polybar custom script, dzen2 feeder, conkybar, lemonbar feeder, dunst notify, etc.
# 'weather -i' = with icon, 'weather' = text only
# Cheers!
# Addy

# Open Weather Map API code, register to http://openweathermap.org to get one ;)
API_KEY="365a6f3a274bba21cabdbee0aa44exxx"

# Check on http://openweathermap.org/find
CITY_ID="1650xxx"

ICON_SUNNY=" Clear"
ICON_CLOUDY=" Cloudy"
ICON_RAINY=" Rainy"
ICON_STORM=" Storm"
ICON_SNOW=" Snow"
ICON_FOG=" Fog"
ICON_MISC=" "

TEXT_SUNNY="Clear"
TEXT_CLOUDY="Cloudy"
TEXT_RAINY="Rainy"
TEXT_STORM="Storm"
TEXT_SNOW="Snow"
TEXT_FOG="Fog"

SYMBOL_CELSIUS="˚C"

WEATHER_URL="http://api.openweathermap.org/data/2.5/weather?id=${CITY_ID}&appid=${API_KEY}&units=metric"

WEATHER_INFO=$(wget -qO- "${WEATHER_URL}")
WEATHER_MAIN=$(echo "${WEATHER_INFO}" | grep -o -e '\"main\":\"[A-Za-z]*\"' | awk -F ':' '{print $2}' | tr -d '"')
WEATHER_TEMP=$(echo "${WEATHER_INFO}" | grep -o -e '\"temp\":\-\?[0-9]*' | awk -F ':' '{print $2}' | tr -d '"')

if [[ "${WEATHER_MAIN}" = *Snow* ]]; then
	if  [[ $1 = "-i" ]]; then
    echo "${ICON_SNOW} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	else
    echo "${TEXT_SNOW} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	fi
elif [[ "${WEATHER_MAIN}" = *Rain* ]] || [[ "${WEATHER_MAIN}" = *Drizzle* ]]; then
	if  [[ $1 = "-i" ]]; then
    echo "${ICON_RAINY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	else
    echo "${TEXT_RAINY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	fi
elif [[ "${WEATHER_MAIN}" = *Cloud* ]]; then
	if  [[ $1 = "-i" ]]; then
    echo "${ICON_CLOUDY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	else
    echo "${TEXT_CLOUDY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	fi
elif [[ "${WEATHER_MAIN}" = *Clear* ]]; then
	if  [[ $1 = "-i" ]]; then
    echo "${ICON_SUNNY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	else
    echo "${TEXT_SUNNY} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	fi
elif [[ "${WEATHER_MAIN}" = *Fog* ]] || [[ "${WEATHER_MAIN}" = *Mist* ]]; then
	if  [[ $1 = "-i" ]]; then
    echo "${ICON_FOG} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	else
    echo "${TEXT_FOG} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	fi
else
	if  [[ $1 = "-i" ]]; then
    echo "${ICON_MISC} ${WEATHER_MAIN} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	else
    echo "${WEATHER_MAIN} ${WEATHER_TEMP}${SYMBOL_CELSIUS}"
	fi	
fi

So far, it goes smoothly with the exception of if my internet connection goes down. I would like to add a message there is no connection.
Could someone set if eth0 present (weather-url) else if no connection (con-down)

I know how to do on conky, but i'm not that good on bash.

Thanks in advance!
Nili


OS: FreeBSD // CWM & Devuan // Fluxbox (lean & mean).
I only play 96kHz Hi-Res audio WAV or FLAC via DAC player.
Chuck Norris doesn't approve systemd.

Offline

#2 2019-06-21 17:31:22

damo
....moderator....
Registered: 2015-08-20
Posts: 5,269

Re: tint2-weather - a small modification

The bunsenweather script uses a connection test, which you could adapt I guess

connectiontest() {
    local -i i attempts=${1-0}
    for (( i=0; i < attempts || attempts == 0; i++ )); do
        if wget -O - 'http://ftp.debian.org/debian/README' &> /dev/null; then
            return 0
        fi
        if (( i == attempts - 1 )); then # if last attempt
            return 1
        fi
    done
}

A code snippet which might work is

if wget -O - 'http://ftp.debian.org/debian/README' &> /dev/null; then
    return 0
else
    notify-send "No internet connection"
    return 1        
fi

You could probably also parse the output from a ping command instead.


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Online

#3 2019-06-22 05:57:38

Nili
Member
From: $HOME/♫♪
Registered: 2015-09-30
Posts: 921
Website

Re: tint2-weather - a small modification

Thank you damo, I have difficult to adapt those two scripts you showed, but the ping output you gave me an idea because i already have a little snippets.

ping -qc1 8.8.8.8 2>&1 | awk -F/ '/^rtt/ { printf "%.2f", $5; ok = 1 } END { if (!ok) print "eth0 FAIL" }'

Edit:

I think i fixed in a way, but I'm not sure it's the right code

if ping -qc1 8.8.8.8 >/dev/null; then
 WEATHER_URL="http://api.openweathermap.org/data/2.5/weather?id=${CITY_ID}&appid=${API_KEY}&units=metric"
else
  echo "Link Down"
fi

Thank you

Last edited by Nili (2019-06-22 07:12:32)


OS: FreeBSD // CWM & Devuan // Fluxbox (lean & mean).
I only play 96kHz Hi-Res audio WAV or FLAC via DAC player.
Chuck Norris doesn't approve systemd.

Offline

Board footer

Powered by FluxBB