Zren / plasma-applets

This monolithic repo has been broken up into individual repos for each widget.
84 stars 23 forks source link

Weather Icon / Current Temp in Panel #4

Open Zren opened 8 years ago

Zren commented 8 years ago

http://kde-look.org/content/show.php?content=175591&forumpage=0#c484110

AndydeCleyre commented 5 years ago

FWIW for anyone else here, I'm currently using Zren's other applet, Command Output, to get weather icon and temp in the panel, using DarkSky API, and my fontconfig set to fall back to Symbols Nerd Font for the icons.

Here's what it looks like:

weather

And here's the script itself (depends on requests and plumbum, and you'll need to provide your own vault.py):

#!/usr/bin/env python3
import sys
from time import sleep

from plumbum.cmd import notify_send
from requests import get
from requests.exceptions import ConnectionError

from vault import DARKSKY_API_KEY, LAT, LONG

API_BASE = f'https://api.darksky.net/forecast/{DARKSKY_API_KEY}/{LAT},{LONG}'
ICONS = {
    'rain':                '',
    'snow':                '',
    'sleet':               '',
    'clear-day':           '',
    'clear-night':         '',
    'wind':                '',
    'fog':                 '',
    'cloudy':              '',
    'partly-cloudy-day':   '',
    'partly-cloudy-night': ''
}

def colorize(text, colorhex='#B8BB26'):
    return f"<font color=\"#{colorhex.lstrip('#')}\">{text}</font>"

def notify():
    try:
        r = get(
            API_BASE,
            params={
                'exclude': 'currently,daily,alerts,flags',
                'units': 'uk2'
            },
            timeout=6
        )
    except ConnectionError as e:
        notify_send('-a', "Weather", e)
    else:
        hourly, minutely = r.json()['hourly'], r.json()['minutely']
        body = f"{minutely['summary']}\n{hourly['summary']}"
        title_icon = ICONS[hourly['icon']]
        summary = ""
        for hour in hourly['data']:
            i = ICONS[hour['icon']]
            if not summary or i != summary[-1]:
                summary += i
        notify_send('-a', f"Weather {title_icon}", summary, body)

def display():
    for attempt in range(3):
        try:
            r = get(
                f"{API_BASE}",
                params={
                    'exclude': 'minutely,hourly,daily,alerts,flags',
                    'units': 'uk2'
                },
                timeout=6
            )
        except ConnectionError:
            sleep(6)
        else:
            current = r.json()['currently']
            temp = round(current['temperature'])
            icon = ICONS[current['icon']]
            print(colorize(f"{temp}°{icon}"))
            break
    else:
        print(colorize("~~~"))

if __name__ == '__main__':
    if '--click' in sys.argv[1:]:
        notify()
    else:
        display()