polybar天气预报插件

自己手写一个,你可以直接抄作业。

获取天气预报api

首先需要自己注册一个api,我这里用的 openweathermap,注册账号后打开 https://home.openweathermap.org/api_keys,查看自己的 api_key
openweathermap 的api_key有延迟,界面显示可用的有时候好像需要半个多小时才可以使用,否则接口一直返回401错误
openweathermap 虽然有诸多不方便,比如英文,速度慢,但是天气预报很准确 而且接口稳定,并分钟级实时预报,天气云图,国内很多免费的接口不稳定。

获取城市id

openweathermap 支持城市名称搜索查询,我这里为了简化直接用城市id了。 打开openweathermap网站 输入你所在地城市的拼音/英文名搜索,查看天气,地址栏的例如 https://openweathermap.org/city/1793724 1793724就是城市id了

手动测试api

访问地址: https://api.openweathermap.org/data/2.5/weather?id={城市ID}&appid={api_key}&units=Metric&lang=zh_cn 后面units是单位公制或者英制之类的 lang 就是语言了
正常应该可以返回json数据,如果是401错误,那么就是api_key错误,或者没生效,或者请求太频繁(1分钟上限60次)

本地准备

因为是polybar用,那么铁定是linux下日用,也肯定有pyhone了。我抄的别人的(原作者忘记)直接用了pyhone。
需要先引用requests, 没有安装。提前安装一下

1
2
3
sudo pacman -S python-pip
pip3 install requests
sudo pacman -Rns python-pip #是否卸载掉看你自己

创建脚本

把下面脚本的apt_key和城市id(1793724)换成你自己的

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 需要requests
# sudo pacman -S python-pip
# pip3 install requests
cat > ~/.config/polybar/weather-plugin.py<< \EOF
#!/bin/python
# -*- coding: utf-8 -*-
import requests
REQ = requests.get("https://api.openweathermap.org/data/2.5/weather?id=1793724&appid=XXXXXXXXXXXX&units=Metric&lang=zh_cn")
try:
    # HTTP CODE = OK
    if REQ.status_code == 200:
        ICON_ID = REQ.json()["weather"][0]["icon"]
	# Set the weather icon
        if ICON_ID == "01d":
            ICON = "☀"  # 晴天 Clear Sky
        elif ICON_ID == "01n":
            ICON = "⭐"  # Night, Clear Sky
        elif ICON_ID == "02d":
            ICON = "🌤"  # Day,少云 Few Clouds
        elif ICON_ID == "02n":
            ICON = "🌤"  # Night, 少云 Few Clouds
        elif ICON_ID == "03d":
            ICON = "🌤"  # Day, 少云 Scattered Clouds
        elif ICON_ID == "03n":
            ICON = "🌤"  # Night, 少云 Scattered Clouds
        elif ICON_ID == "04d":
            ICON = "多云"  # Day, 多云 Broken Clouds
        elif ICON_ID == "04n":
            ICON = "多云"  # Night, 多云 Broken Clouds
        elif ICON_ID == "09d":
            ICON = "🌧"  # Day, 小雨 Shower Rain 
        elif ICON_ID == "09n":
            ICON = "🌧"  # Night,小雨 Shower Rain
        elif ICON_ID == "10d":
            ICON = "🌧"  # Day, Rain
        elif ICON_ID == "10n":
            ICON = "🌧"  # Night, Rain
        elif ICON_ID == "11d":
            ICON = "⛈"  # Day, 雷雨 Thunderstorm
        elif ICON_ID == "11n":
            ICON = "⛈"  # Night, 雷雨  Thunderstorm
        elif ICON_ID == "13d":
            ICON = "❄"  # Day,雪 Snow
        elif ICON_ID == "13n":
            ICON = "❄"  # Night, 雪 Snow
        elif ICON_ID == "50d":
            ICON = "🌫"  # Day, 雾 Mist
        elif ICON_ID == "50n":
            ICON = "🌫"  # Night,雾  Mist
        else:
            ICON = "天气 "  #  其他 Just  Weather Icon

        CURRENT = REQ.json()["weather"][0]["description"].capitalize()
        TEMP = int(float(REQ.json()["main"]["temp"]))
        WIND = int(float(REQ.json()["wind"]["speed"]))
	# 输出格式
        print("{} {}, {} °C 🌬 {}m/s".format(ICON, CURRENT, TEMP, WIND))
    else:
        print("Error: BAD HTTP STATUS CODE " + str(REQ.status_code))
except (ValueError, IOError):
    print("Error: Unable print the data")
EOF

测试一下,正常可以在终端输出天气

1
python3 ~/.config/polybar/weather-plugin.py

polybar引用

编辑配置文件 ~/.config/polybar/config.ini 添加一行

1
2
3
4
5
6
7
; ===天气预报 需要单独的pyhtone插件===
[module/weather]
type = custom/script
exec = python3 ~/.config/polybar/weather-plugin.py
interval = 960
tail = true
click-left= exec firefox https://openweathermap.org/city/1793724

完毕

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计