da-rth / yasb

A highly configurable cross-platform (Windows) status bar written in Python.
MIT License
1.29k stars 78 forks source link

[BUG] Trying to make the weather widget work #103

Open zombie-bear opened 1 year ago

zombie-bear commented 1 year ago

Expected Behavior

should display weather information collected by API

Current Behavior

display "{data[main][temp]}" instead

Possible Solution

Steps to Reproduce

add to config.yaml : weather: type: "yasb.custom.CustomWidget" options: label: "\uf0c2 {data[main][temp]}\u00b0c" label_alt: "\uf0c2 {data[weather][0][description]}" class_name: "weather-widget" exec_options: run_cmd: "curl.exe "http://api.weatherapi.com/v1/forecast.json?key=0244eac47d8c4bbcb7b205535231907&q=Brussels&days=1&aqi=no&alerts=no""

run every hour

    run_interval: 3600000
    return_format: "json"

Context (Environment)

line 480 = run_cmd: "curl.exe "http://api.weatherapi.com/v1/forecast.json?key=0244eac47d8c4bbcb7b205535231907&q=Brussels&days=1&aqi=no&alerts=no"" log: 2023-07-20 12:33:39 INFO log.py: Yasb - Yet Another Status Bar 2023-07-20 12:33:39 ERROR config.py: The file 'C:\yasb-main\src\config.yaml' contains Parser Error(s). Please fix: while parsing a block mapping in "C:\yasb-main\src\config.yaml", line 480, column 9 expected , but found '' in "C:\yasb-main\src\config.yaml", line 480, column 29 2023-07-20 12:33:40 ERROR config.py: User config file could not be loaded. Exiting Application.

when use alone in another terminale [ curl.exe http://api.weatherapi.com/v1/forecast.json?key=0244eac47d8c4bbcb7b205535231907&q=Brussels&days=1&aqi=no&alerts=no ] : {"location":{"name":"Brussels","region":"","country":"Belgium","lat":50.83,"lon":4.33,"tz_id":"Europe/Brussels","localtime_epoch":1689844265,"localtime":"2023-07-20 11:11"}, .......(too long to paste here)

zombie-bear commented 1 year ago

temporary fix : in "config.yaml" : weather: type: "yasb.custom.CustomWidget" options: label: "\uf0c2 {data[current_condition][0][FeelsLikeC]}\u00b0C, {data[current_condition][0][weatherDesc][0][value]}" label_alt: "\uf0c2 Sunrise: {data[weather][0][astronomy][0][sunrise]} | Sunset: {data[weather][0][astronomy][0][sunset]}, {data[weather][0][description]}"
class_name: "weather-widget" exec_options: run_cmd: "curl.exe wttr.in/Ixelles?format=j1"

run every hour

    run_interval: 6000
    return_format: "json"

but can't display "label_alt" items

zombie-bear commented 1 year ago

fix 2.0 : weather: type: "yasb.custom.CustomWidget" options: label: "\uf0c2 {data[weather][0][hourly][0][FeelsLikeC]}\u00b0C, {data[weather][0][hourly][0][weatherDesc][0][value]}" label_alt: "\uf0c2 Sunrise: {data[weather][0][astronomy][0][sunrise]} | Sunset: {data[weather][0][astronomy][0][sunset]}" class_name: "weather-widget" exec_options: run_cmd: 'curl.exe wttr.in/Ixelles?format=j1'

run every hour

    run_interval: 6000
    return_format: "json"

but when i try to acces the french information in 'curl.exe "wttr.in/Ixelles?format=j1&lang=fr"' (whit correct path in label and label_alt) nothing work

HeNeos commented 4 months ago

I have solved this issue using an external python script to get the data from the api:

  weather:
    type: "yasb.custom.CustomWidget"
    options:
      label: "\uf0c2 {data[current][temp]}\u00b0C|\uf773{data[current][humidity]}%"
      label_alt: "\uf0c2 {data[current][weather][0][description]}"
      class_name: "weather-widget"
      exec_options:
        run_cmd: python.exe <Path_to_script>\weather.py
        run_interval: 3600000
        return_format: "json"

and the script is something like:

import requests
import json

def get_location():
    url = "https://ipinfo.io"
    try:
        response = requests.get(url)
        response.raise_for_status()  # Raise an HTTPError for bad responses (4xx and 5xx)
        data = response.json()
        location = data["loc"]
        location = location.split(',')
        return location
    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")

def get_weather_data(api_key):
    location = get_location()
    url = "https://api.openweathermap.org/data/3.0/onecall"
    params = {
        "lat": float(location[0]),
        "lon": float(location[1]),
        "units": "metric",
        "appid": api_key
    }

    try:
        response = requests.get(url, params=params)
        response.raise_for_status()  # Raise an HTTPError for bad responses (4xx and 5xx)

        # Print the JSON response
        print(json.dumps(response.json(), indent=2))

    except requests.exceptions.RequestException as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    api_key = YOUR_API_KEY
    get_weather_data(api_key)

image