Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zombie-bear opened this issue Jul 20, 2023 · 3 comments
Open

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

zombie-bear opened this issue Jul 20, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@zombie-bear
Copy link

zombie-bear commented Jul 20, 2023

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 zombie-bear added the bug Something isn't working label Jul 20, 2023
@zombie-bear
Copy link
Author

zombie-bear commented Jul 20, 2023

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
Copy link
Author

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
Copy link

HeNeos commented Mar 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants